simplesoftwareio/simple-cache

An easy to use caching trait for Laravel's Eloquent Models.

1.0.0 2016-12-21 00:54 UTC

This package is not auto-updated.

Last update: 2024-04-11 02:29:34 UTC


README

Build Status Latest Stable Version Latest Unstable Version License Total Downloads

Try our dead simple, free file transfer service keep.sh

Configuration

Composer

First, add the Simple Cache package to your require in your composer.json file:

"require": {
	"simplesoftwareio/simple-cache": "~1"
}

Next, run the composer update command.

Usage

The cacheable trait may be used by adding the trait to the Eloquent model of your choice.

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use SimpleSoftwareIO\Cache\Cacheable;

class User extends Model
{
    use Cacheable;
}

Yes, it really is that simple to use. The settings will use the default Cache store set up in your Laravel application. Further, models will be cached for 30 minutes by default.

Properties

cacheLength

You may adjust the default cache length by modifying the cacheLength property on the model.

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use SimpleSoftwareIO\Cache\Cacheable;

class User extends Model
{
    use Cacheable;
    protected $cacheLength = 60; //Will cache for 60 minutes
}

cacheStore

The configured cache store may also be adjusted by modifying the cacheStore property. The cache store will need to be set up in your application's config/cache.php configuration file.

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use SimpleSoftwareIO\Cache\Cacheable;

class User extends Model
{
    use Cacheable;
    protected $cacheStore = 'redis'; //Will use the configured `redis` store set up in your `config/cache.php` file.
}

cacheBusting

Cache busting will automatically invalid the cache when an insert/update/delete command is ran by your models. You can enable this feature by setting the cacheBusting property to true on your Eloquent model. By default this feature is disabled.

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use SimpleSoftwareIO\Cache\Cacheable;

class User extends Model
{
    use Cacheable;
    protected $cacheBusting = true;

}

Be careful! Eloquent Model's with a high amount of insert/update/delete traffic should not use the cache busting feature. The large amount of changes will invalid the model too often and cause the cache to be useless. It is better to set a lower cache length to invalid the results frequently if up to date data is required.

Methods

flush()

The flush method will flush the cache for a model.

(new User)->flush()  //Cache is flushed for the `User` model.

isBusting()

isBusting will return the current status of the cacheBusting property.

if((new User)->isBusting()) {
    // Is cache busting
}

remember($length)

remember will set the length of time in minutes to remember an Eloquent query.

User::remember(45)->where('id', 4')->get();

rememberForever()

rememberForever will remember a query forever. Well, technically 10 years but lets pretend it is forever eh?

User::rememberForever()->where('id', 4')->get();

dontRemember()

And lastly, dontRemember will not cache a query result.

User::dontRemember(0)->where('id', 4')->get();