Download the PHP package adamcrampton/object-cache without Composer
On this page you can find all versions of the php package adamcrampton/object-cache. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download adamcrampton/object-cache
More information about adamcrampton/object-cache
Files in adamcrampton/object-cache
Package object-cache
Short Description Redis object cache for Laravel
License MIT
Homepage https://github.com/adamcrampton/object-cache
Informations about the package object-cache
object-cache
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.
Upgrading from v3.x.x
If upgrading from v3, you will need to make the following changes to your implementation:
- Create a PHP class for storing your methods and method configs, and migrate the array and your cache methods across. Be sure to move across any dependencies to the new class.
- Add a key to your file, and set that value to the fully namespaced class name you created in the previous step.
- Delete your object cache Middleware.
- Change the Middleware settings in to use the package Middleware:
- See the Configuration section below for additional config options you may not have set.
Installation
- Run in your project directory
- Add library service provider and facade to
- Create a class for storing your methods, adding use statement for
- Add the package Middleware to
- Configure your routes to use middleware - see https://laravel.com/docs/5.8/middleware#assigning-middleware-to-routes
- Run - this will add a file to your project's config directory
- Add your Redis host to the app's .env (or localhost for local dev - ensure your environment has Redis installed)
Configuration
Configuration can be found in the published file.
- Edit the and values for the Redis connection - see https://github.com/nrk/predis/wiki/Connection-Parameters
- Add the class name with full namespacing to the key
- The settings under 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 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)
- Setting to true will enable the Get Helper to log any failed attempts to get values from Redis and/or fallback attempts
Usage
To initialise a connection:
- Add to the class
- Declare a variable to use for your connection, e.g.
Once initialised, you can use all Predis commands. See:
Middleware
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 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 array. They are:
- cacheKey: Name of the key for your object in the Redis store.
- cacheTtl: Using either a pre-defined named TTL (see below), or a TTL in seconds.
- cacheMethod: The name of the method to regenerate the cache data if not found (i.e. if it is missing or expired).
An example of an implementation using a cache store class:
Get Helper
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:
Pre-configured TTL
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 property with the appropriate key name. Structure of this TTL array is as follows:
```$this->ttl = [ '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 ] ];
All versions of object-cache with dependencies
laravel/framework Version >=5.8
predis/predis Version ^1.1
illuminate/support Version >=5.5