Git Product home page Git Product logo

laravel-eloquent-cache's Introduction

Laravel Eloquent cache

Total Downloads Latest Stable Version Latest Unstable Version License

Laravel's Eloquent models caching

Installation

Install via composer :

composer require alex433/laravel-eloquent-cache

How it works

When Eloquent fetches models by primary key, the SQL query result are cached. Subsequently, when eloquent fetches a model by primary key, the cached result will be used. The cache entry will be flushed when you create, update, or delete a model instance.

Usage

Use the Cachable trait in the models you want to cache.

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Alex433\LaravelEloquentCache\Cachable;

class Post extends Model
{
    use Cachable;
}

In next cases cache queries will be executed instead SQL queries. Also it do the trick for "belongs To" relations.

Post::find($id); // findOrFail(), findOrNew()
Post::where('id', $id)->first(); // firstOrFail(), firstOrNew(), firstOrCreate(), firstOr()
Post::whereId($id)->first();
Post::where('id', $id)->get();

You can optionally define the following properties to change default trait behavior.

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

use Alex433\LaravelEloquentCache\Cachable;

class User extends Authenticatable
{
    use Notifiable,
        Cachable;

    /**
     * Cache TTL in seconds. Defaults indefinitely
     *
     * @var int $cacheTtl
     */
    public $cacheTtl = 3600;

    /**
     * Cache store name. Defaults default cache connection
     *
     * @var string $cacheStore
     */
    public $cacheStore = 'redis';

    /**
     * Cache tags. Defaults no tags
     *
     * @var array $cacheTags
     */
    public $cacheTags = ['users'];
}

To invalidate the cache entry for a model instance, use forget method.

User::find($id)->forget();

// or

User::find($id)->forget()->refresh();

When cache tags is used, you can flush the cache for a model, use the flushCache method.

User::flushCache();

// or

User::find($id)->flushCache();

laravel-eloquent-cache's People

Contributors

alex433 avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.