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.

v1.0.0 2022-07-14 10:19 UTC

This package is auto-updated.

Last update: 2024-11-14 15:22:03 UTC


README

Build Status Style CI Total Downloads Latest Stable Version License

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
  • 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
  • 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 call forgetCall based on the settings you choose here.
    • Env variable: CACHEABLE_TAGS
    • Default value: false
  • prefix if you would like to set a default prefix to all cacheable items
    • Env variable: CACHEABLE_PREFIX
    • Default value: ``
  • suffix if you would like to set a default suffix to all cacheable items
    • Env variable: CACHEABLE_SUFFFIX
    • Default value: ``

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'])

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.