adamcrampton/object-cache is a Laravel package for redis object cache for laravel.
It currently has 0 GitHub stars and 1.287 downloads on Packagist (latest version v4.2.6).
Install it with composer require adamcrampton/object-cache.
Discover more Laravel packages by adamcrampton
or browse all Laravel packages to compare alternatives.
Last updated
This Laravel package will allow you to set and forget objects in your Redis cache, with automated Middleware and convenient pre-bundled TTL settings. All you need to supply is a TTL, key name, and method for regenerating the value.
If upgrading from v3, you will need to make the following changes to your implementation:
$this->objects array and your cache methods across. Be sure to move across any dependencies to the new class.methodClass key to your config/object_cache.php file, and set that value to the fully namespaced class name you created in the previous step.app/Http/Kernel.php to use the package Middleware: \AdamCrampton\ObjectCache\Middleware\CheckObjectCache::classcomposer require adamcrampton/object-cache in your project directoryAdamCrampton\ObjectCache\ObjectCacheServiceProvider::class and facade 'ObjectCache' => AdamCrampton\ObjectCache\ObjectCacheFacade::class to config/app.phpAdamCrampton\ObjectCache\ObjectCache\AdamCrampton\ObjectCache\Middleware\CheckObjectCache::class to app/Http/Kernel.phpphp artisan vendor:publish - this will add a object_cache.php file to your project's config directoryConfiguration can be found in the published config/object_cache.php file.
parameters and options values for the Redis connection - see https://github.com/nrk/predis/wiki/Connection-ParametersmethodClass keyfallback relate to the Get Helper (details further down). If you would like the library to manually fetch the data if the Redis get fails, set this to true. The passRequest option forces the helper to pass the request object into the constructor of your cache method class (only set to true if this is necessary for your application)logErrors to true will enable the Get Helper to log any failed attempts to get values from Redis and/or fallback attemptsTo initialise a connection:
use AdamCrampton\ObjectCache\ObjectCache to the class$redis = ObjectCache::init()Once initialised, you can use all Predis commands. See:
The CheckObjectCache Middleware that ships with the package allows you to add cache objects to be automatically checked on each request, and if missing, set. You will need to add an $this->objects array (configuration) and each corresponding method to your cache store class (mentioned above in installation).
An array of 3 values are required for each item in the $this->objects array. They are:
An example of an implementation using a cache store class:
use App\Models\PartPrice;
use AdamCrampton\ObjectCache\ObjectCache;
class CacheMethodStore
{
public $objects;
public function __construct()
{
$this->objectCache = new ObjectCache();
$this->objects = [
[
'cacheKey' => 'exampleKey',
'cacheTtl' => $this->objectCache->ttl['hours']['twentyFour'],
'cacheMethod' => 'setExample'
]
];
}
public function setExample()
{
return PartPrice::select(['part_id', 'price'])
->get()
->toJson();
}
}
Once initialised, you can fetch an object from the cache using the provided helper. The two parameters you need to pass in are the key and JSON decode option.
If you are passing in the decode option, set this to 'array' or 'object' for the correct return format. Example:
/**
* Fetch part data for the front end.
*
* @return array
*/
protected function buildPartData(Request $request)
{
return ObjectCache::get('dealer_123456_part_data', 'array');
}
A convenience array of TTL values are included with the package. To use one of the values, initalise the Redis connection as mentioned above, and call the ttl property with the appropriate key name. Structure of this TTL array is as follows:
'default' => [
'default' => 86400
],
'apc' => [
'default' => 86400
],
'array' => [
'default' => 86400
],
'database' => [
'default' => 86400
],
'file' => [
'default' => 86400
],
'memcached' => [
'default' => 86400
],
'redis' => [
'default' => 86400
],
'minutes' => [
'one' => 60,
'five' => 300,
'ten' => 600,
'fifteen' => 900,
'twenty' => 1200,
'thirty' => 1800,
'forty' => 2400,
'fortyFive' => 2700,
'fifty' => 3000,
'sixty' => 3600
],
'hours' => [
'one' => 3600,
'two' => 7200,
'three' => 10800,
'four' => 14400,
'five' => 18000,
'six' => 21600,
'seven' => 25200,
'eight' => 28800,
'nine' => 32400,
'ten' => 36000,
'eleven' => 39600,
'twelve' => 43200,
'twentyFour' => 86400
],
'days' => [
'one' => 86400,
'two' => 172800,
'three' => 259200,
'four' => 345600,
'five' => 432000,
'six' => 518400,
'seven' => 604800,
'fourteen' => 1209600,
'twentyEight' => 2419200
],
'weeks' => [
'one' => 604800,
'two' => 1209600,
'three' => 1814400,
'four' => 2419200,
'five' => 3024000,
'six' => 3628800
]
];