hmones / laravel-cacheable
A simple package to automatically cache the returns of certain methods in a given class and forgets them as well on request.The package is highly configurable and let you manage naming of the cache keys with ease.
Requires
- php: ^7.4|^8.0
- ext-json: *
- illuminate/support: ~7|~8|~9
Requires (Dev)
- orchestra/testbench: ~5|~6|~7
- phpunit/phpunit: ~9.0
This package is auto-updated.
Last update: 2024-11-14 15:22:03 UTC
README
A simple package to automatically cache the returns of certain methods in a given class and forgets them as well on request. The package is highly configurable and let you manage naming of the cache keys with ease.
Installation
Via Composer
composer require hmones/laravel-cacheable
Configuration
To publish the package configuration
php artisan vendor:publish --tag=laravel-cacheable-config
The configuration file contains the following parameters:
ttl
: the default time to live for caching functions- Env variable:
CACHEABLE_TTL
- Default value:
3600
- Env variable:
param-keys-enabled
whether you would like to add function parameters to keys of the stored cache items. Note: Make sure that the parameters are strings or integers, otherwise adding the parameters wouldn't work.- Env variable:
CACHEABLE_PARAM_KEYS_ENABLED
- Default value:
false
- Env variable:
cache-tags-enabled
whether you would like to add tags as the method name to the cached keys, this will allow you to store multiple instances of the calls if you choose to add parameters to it, and then when you want to delete them all you can simply delete everything with this tag. The application takes care of that on it's own if you callforgetCall
based on the settings you choose here.- Env variable:
CACHEABLE_TAGS
- Default value:
false
- Env variable:
prefix
if you would like to set a default prefix to all cacheable items- Env variable:
CACHEABLE_PREFIX
- Default value: ``
- Env variable:
suffix
if you would like to set a default suffix to all cacheable items- Env variable:
CACHEABLE_SUFFFIX
- Default value: ``
- Env variable:
Usage
- Make sure your class extend
\Hmones\LaravelCacheable\Cacheable
- Instead of calling the method in the class directly, call
CLASS::cacheCall('methodName', ['argument1' => 'value', 'argument2' => 'value'])
- You can remove the cached method call as follows:
- If you have redis and/or any cache driver that supports tagging, even if you have enabled the parameters' addition to cache keys, you can simply call
CLASS::forgetCall('methodName)
- If you don't have a cache driver that supports tags, and you have enabled adding functions parameters to the cache, you must include the arguments in the forget call as follows:
CLASS::forgetCall('methodName', ['argument1' => 'value', 'argument2' => 'value'])
- If you have redis and/or any cache driver that supports tagging, even if you have enabled the parameters' addition to cache keys, you can simply call
Example: Sending a digest email every time 10 new users register on the website with a summary of their names.
Change log
Please see the changelog for more information on what has changed recently.
Testing
composer test
Contributing
Please see contributing.md for details and a todolist.
Security
If you discover any security related issues, please email author email instead of using the issue tracker.
Credits
License
Please see the license file for more information.