Aplus Framework Docs


Aplus Framework Cache Library

Aplus Framework Cache Library.


The installation of this library can be done with Composer:

composer require aplus/cache

Getting Started

The logic for caching values is similar to the example below:

use Framework\Cache\FilesCache;

$cache = new FilesCache([
    'directory' => '/tmp/cache/'

$data = $cache->get('data'); // Data value or null

if  ($data !== null) { // If data is cached, return now
    return $data;

$data = ['foo', 'bar']; // Else, set again
$cache->set('data', $data, 15); // Cache for 15 seconds

return $data;

If the value of "data" is cached it returns that value.

Otherwise, the value is added to the cache to be responded to in the next request, within the 15 second TTL.

Set Values

Items can be cached individually or several at a time.

// Set the value of "key" for 10 seconds
$cache->set('key', 'value', 10); // bool

// Set the values of "key" and "foo" for 10 seconds
    'key'=> 'value',
    'foo'=> 'bar',
], 10); // array of booleans

The TTL can be set as the third argument of the set method or directly in the defaultTtl property.

$cache->defaultTtl = 60;

Get Values

Get values can also be individually or multiple at once:

// Data is the value of "key" or null
$data = $cache->get('key'); 

// Data is an array with the keys "key", "foo" and "baz"
// Items not found have null value
$data = $cache->getMulti(['key', 'foo', 'baz']);

Increment and Decrement

Some items may be simpler and only need to save increment or decrement values. Example below:

$data = $cache->increment('foo'); // $data is 1
$data = $cache->increment('foo'); // $data is 2
$data = $cache->increment('foo', 3); // $data is 5
$data = $cache->decrement('foo'); // $data is -1
$data = $cache->decrement('foo'); // $data is -2
$data = $cache->decrement('foo', 3); // $data is -5

Cache Handlers

There are 3 cache handlers in the library and they are the following:

All handlers receive configs, prefix, serializer and logger through the constructor.


The FilesCache config must have the value of directory. The other configs already have default values:

use Framework\Cache\FilesCache;

$configs = [
    'directory' => '/patch/to/cache/directory',
    'files_permission' => 0644,
    'gc' => 1,
$cache = new FilesCache($configs);


The Memcached handler already comes with the configs set to connect to Memcache on localhost.

If you want to set different configs, do as follows:

use Framework\Cache\MemcachedCache;

$configs = [
     'servers' => [
            'host' => '',
            'port' => 11211,
            'weight' => 0,
            'host' => '',
            'port' => 11211,
            'weight' => 0,
    'options' => [
        Memcached::OPT_BINARY_PROTOCOL => true,
$cache = new MemcachedCache($configs);


The Redis handler is also already configured to work on localhost.

If it is necessary to define another address, do as in the example below:

use Framework\Cache\RedisCache;

$configs = [
    'host' => '',
    'port' => 6379,
    'timeout' => 0.0,
$cache = new RedisCache($configs);


Aplus Cache Library is an easy-to-use tool for, beginners and experienced, PHP developers.
With it you can optimize the performance of your applications.
The more you use it, the more you will learn.

Did you find something wrong?
Be sure to let us know about it with an issue.
Thank you!

Search results