|
| 1 | +Simple Cache |
| 2 | +======================== |
| 3 | + |
| 4 | +[](https://travis-ci.org/SimpleSoftwareIO/simple-cache) |
| 5 | +[](https://packagist.org/packages/simplesoftwareio/simple-cache) |
| 6 | +[](https://packagist.org/packages/simplesoftwareio/simple-cache) |
| 7 | +[](https://packagist.org/packages/simplesoftwareio/simple-cache) |
| 8 | +[](https://packagist.org/packages/simplesoftwareio/simple-cache) |
| 9 | + |
| 10 | +## This is pre-released software. Use at your own risk. |
| 11 | + |
| 12 | +- [Introduction](#docs-introduction) |
| 13 | +- [Configuration](#docs-configuration) |
| 14 | +- [Usage](#docs-usage) |
| 15 | + |
| 16 | +<a id="docs-configuration"></a> |
| 17 | +## Configuration |
| 18 | + |
| 19 | +#### Composer |
| 20 | + |
| 21 | +First, add the Simple Cache package to your `require` in your `composer.json` file: |
| 22 | + |
| 23 | + "require": { |
| 24 | + "simplesoftwareio/simple-cache": "dev-master" |
| 25 | + } |
| 26 | + |
| 27 | +Next, run the `composer update` command. |
| 28 | + |
| 29 | +<a id="docs-usage"></a> |
| 30 | +## Usage |
| 31 | + |
| 32 | +The cacheable trait may be used by adding the trait to the Eloquent model of your choice. |
| 33 | + |
| 34 | + <?php |
| 35 | + |
| 36 | + namespace App; |
| 37 | + |
| 38 | + use Illuminate\Database\Eloquent\Model; |
| 39 | + use App\Cache\Cacheable; |
| 40 | + |
| 41 | + class User extends Model |
| 42 | + { |
| 43 | + use Cacheable; |
| 44 | + } |
| 45 | + |
| 46 | +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. |
| 47 | + |
| 48 | +### Properties |
| 49 | + |
| 50 | +#### cacheLength |
| 51 | + |
| 52 | +You may adjust the default cache length by modifying the `cacheLength` property on the model. |
| 53 | + |
| 54 | + <?php |
| 55 | + |
| 56 | + namespace App; |
| 57 | + |
| 58 | + use Illuminate\Database\Eloquent\Model; |
| 59 | + use App\Cache\Cacheable; |
| 60 | + |
| 61 | + class User extends Model |
| 62 | + { |
| 63 | + use Cacheable; |
| 64 | + protected $cacheLength = 60; //Will cache for 60 minutes |
| 65 | + } |
| 66 | + |
| 67 | +#### cacheStore |
| 68 | + |
| 69 | +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. |
| 70 | + |
| 71 | + <?php |
| 72 | + |
| 73 | + namespace App; |
| 74 | + |
| 75 | + use Illuminate\Database\Eloquent\Model; |
| 76 | + use App\Cache\Cacheable; |
| 77 | + |
| 78 | + class User extends Model |
| 79 | + { |
| 80 | + use Cacheable; |
| 81 | + protected $cacheStore = 'redis'; //Will use the configured `redis` store set up in your `config/cache.php` file. |
| 82 | + } |
| 83 | + |
| 84 | +#### cacheBusting |
| 85 | + |
| 86 | +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. |
| 87 | + |
| 88 | + <?php |
| 89 | + |
| 90 | + namespace App; |
| 91 | + |
| 92 | + use Illuminate\Database\Eloquent\Model; |
| 93 | + use App\Cache\Cacheable; |
| 94 | + |
| 95 | + class User extends Model |
| 96 | + { |
| 97 | + use Cacheable; |
| 98 | + protected $cacheBusting = true; |
| 99 | + |
| 100 | + } |
| 101 | + |
| 102 | +>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. |
| 103 | +
|
| 104 | +### Methods |
| 105 | + |
| 106 | +#### bust() |
| 107 | + |
| 108 | +The `bust` method will enable cache busting for the model |
| 109 | + |
| 110 | + User::bust(); //Cache busting is enabled. |
| 111 | + |
| 112 | +#### dontBust() |
| 113 | + |
| 114 | +The `dontBust` method will disable cache busting for the model. |
| 115 | + |
| 116 | + User::dontBust(); //Cache busting is disabled. |
| 117 | + |
| 118 | +#### isBusting() |
| 119 | + |
| 120 | +`isBusting` will return the current status of the `cacheBusting` property. |
| 121 | + |
| 122 | + if(User::isBusting()) { |
| 123 | + // Is cache busting |
| 124 | + } |
| 125 | + |
| 126 | +#### remember($length) |
| 127 | + |
| 128 | +`remember` will set the length of time in minutes to remember an Eloquent query. |
| 129 | + |
| 130 | + User::remember(45)->where('id', 4')->get(); |
| 131 | + |
| 132 | +#### rememberForever() |
| 133 | + |
| 134 | +`rememberForever` will remember a query forever. Well, technically 10 years but lets pretend it is forever eh? |
| 135 | + |
| 136 | + User::rememberForever()->where('id', 4')->get(); |
| 137 | + |
| 138 | +#### dontRemember() |
| 139 | + |
| 140 | +And lastly, `dontRemember` will not cache a query result. |
| 141 | + |
| 142 | + User::dontRemember(0)->where('id', 4')->get(); |
0 commit comments