Download the PHP package chrgriffin/laravel-cacheable without Composer
On this page you can find all versions of the php package chrgriffin/laravel-cacheable. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download chrgriffin/laravel-cacheable
More information about chrgriffin/laravel-cacheable
Files in chrgriffin/laravel-cacheable
Package laravel-cacheable
Short Description A GOAOP implementation for easily caching method calls.
License MIT
Informations about the package laravel-cacheable
Laravel Cacheable
laravel-cacheable makes it easy to automagically cache the results of any arbitrary class method, using your configured Laravel cache driver, by simply adding a docblock annotation to that method.
Installation
Install in your Laravel project via composer:
If your version of Laravel supports auto-discovery (5.5 and up), you're done!
For older versions of Laravel, you'll need to edit your config/app.php file to include the service provider in your providers array:
Usage
Configuration (optional)
laravel-cacheable uses GOAOP to build cached versions of your classes which then have a method interceptor applied to it. The package will, by default, use your Laravel framework cache directory as its cache. It will also, by default, search for Cache annotations in your entire app directory. Both of these configs can be overridden. First, copy the package config:
Now, in config/laravel-cacheable.php, you can override the default package configs:
Caching Method Returns
Now that the package is installed, caching the results of a method call is as easy as making sure your Laravel cache is set up and then adding an annotation to your method:
Specifying a Custom Cache Time
By default, laravel-cacheable will cache a method return for 30 minutes. If you want to cache a method for a different length of time, you can add a seconds property to the annotation:
Advanced Behaviours
The docblock alone is enough to automatically cache the results of a method. For more advanced behaviours, you will also need to use the Cacheable trait:
Bypassing the Cache
Once your class is using the Cacheable trait, you can bypass the trait by chaining ->withoutCache() before your method call:
Note that this will bypass both getting and setting the cache -- the return of this method will not be cached.
Notes
laravel-cacheable checks both the method name and the passed arguments to determine if the return should be pulled from cache or not. If different arguments are passed, a new cache index will be created.