sFire PHP Framework

Cache

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.

In this section we will handle:

  • Setting the cache adapter
  • Cache as a service
  • Storing items in the cache
  • Retrieving items from the cache
  • Removing items from the cache
  • Renew expiration
  • Check if an item exists
  • Types of data

Setting the cache adapter

There are two caching adapters you can use for caching data.

Filesystem

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.

APCu

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.

Cache as a service

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

Storing items in the 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.

Parameters:
$cache -> set(string $key, mixed $value, [int $expiration = 300]);
Example:
$cache -> set('foo', 'bar', 10);

Retrieving items from the cache

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.

Parameters:
$cache -> get(string $key, [mixed $default]);
Example:
$data = $cache -> get('foo');
print_r($data);

Removing items from the cache

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.

Parameters:
$cache -> expire(string $key);
Example:
$cache -> expire('foo');

Renew expiration

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.

Parameters:
$cache -> touch(string $key, [int $expiration]);
Example:
$cache -> touch('foo');

Check if a item exists

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.

Parameters:
$cache -> exists(string $key);
Example:
$exists = $cache -> exists('foo');
var_dump($exists); //Boolean true or false

Types of data

The following data is supported:

  • Strings - Character strings of arbitrary size in any PHP-compatible encoding.
  • Integers - All integers of any size supported by PHP, up to 64-bit signed.
  • Floats - All signed floating point values.
  • Boolean - True and False.
  • Null - The actual null value.
  • Arrays - Indexed, associative and multidimensional arrays of arbitrary depth.
  • Object - Any object that supports lossless serialization and deserialization such that $o == unserialize(serialize($o)). Objects may leverage PHP's Serializable interface, __sleep() or __wakeup() magic methods.