Package Data | |
---|---|
Maintainer Username: | SimplyCorey |
Maintainer Contact: | support@simplesoftware.io (Simple Software LLC) |
Package Create Date: | 2016-12-19 |
Package Last Update: | 2022-05-17 |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2024-11-20 03:01:17 |
Package Statistics | |
---|---|
Total Downloads: | 10,268 |
Monthly Downloads: | 4 |
Daily Downloads: | 0 |
Total Stars: | 23 |
Total Watchers: | 5 |
Total Forks: | 0 |
Total Open Issues: | 0 |
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.
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.
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
}
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.
}
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.
The flush
method will flush the cache for a model.
(new User)->flush() //Cache is flushed for the `User` model.
isBusting
will return the current status of the cacheBusting
property.
if((new User)->isBusting()) {
// Is cache busting
}
remember
will set the length of time in minutes to remember an Eloquent query.
User::remember(45)->where('id', 4')->get();
rememberForever
will remember a query forever. Well, technically 10 years but lets pretend it is forever eh?
User::rememberForever()->where('id', 4')->get();
And lastly, dontRemember
will not cache a query result.
User::dontRemember(0)->where('id', 4')->get();