sFire\Cache\Cache can be used to store and retrieve data. It can delete data automatically after a defined time and.
Caching is useful to optimize performance of applications.
There are two caching adapters you can use for caching data.
sFire\Cache\Adapter\Filesystem cache is based on the file system to store and retrieve data. By default, it will store all the cache in the "data/cache/shared/" folder.
use sFire\Cache\Cache;
$cache = new Cache('Filesystem');
Click here to view more information about the Filesystem cache.
sFire\Cache\Adapter\APCu cache is based on the APCu extension to store and retrieve data. APCu is APC stripped of opcode caching.
use sFire\Cache\Cache;
$cache = new Cache('APCu');
Click here to view more information about the APCU cache.
For using the cache, we need to be able to obtain the cache instance from almost everywhere in our code. To do so, you need to edit the app.php and add a new record to the services Array:
//Service providers
Application :: add('services', [
'cache' => function() {
return sFire\Cache\Cache('APCu'); //Using the APCu driver
},
];
In this case we used the APCu cache adapter. Our cache instance is called "cache" as a key for obtaining this instance in the controllers and models which we can call like:
$cache = $this -> service('cache');
It is simple to store data in the cache. Storing data will always require a key for retrieval of the data after storing. Data is serialized for data type dependency. If you store an integer, it will come out as an integer. See the "Types of data" section for compatible data types.
The default expiration is 300 seconds (5 minutes). Expiration will always be the amount of seconds from the moment of store.
$cache -> set(string $key, mixed $value, [int $expiration = 300]);
$cache -> set('foo', 'bar', 10);
To retrieve data, you have to use the get method. If the data exists and not expired, it will return the data type save. If the item does not (longer) exists, it will return null as default. See the "Types of data" section for compatible data types. You can use the second parameter to overwrite the default value to return when the key does not exists.
$cache -> get(string $key, [mixed $default]);
$data = $cache -> get('foo');
print_r($data);
You can manually expire keys by calling the expire method. The data is no longer available due to permanent deletion and thus cannot be recovered.
$cache -> expire(string $key);
$cache -> expire('foo');
The touch method can renew the expiration time of a key. You may set the new expiration time or leave it blank so the key will get the original time as the new amount of seconds it will expire.
$cache -> touch(string $key, [int $expiration]);
$cache -> touch('foo');
To check if an item still exists in the cache, you may call the exists method. This method will return a Boolean true or false if an item exists or not.
$cache -> exists(string $key);
$exists = $cache -> exists('foo');
var_dump($exists); //Boolean true or false
The following data is supported: