A simple cache library. Implements different adapters that you can use and change easily by a manager or similar.
It is best installed it through packagist
by including desarrolla2/cache
in your project composer.json require:
"require": {
// ...
"desarrolla2/cache": "~2.0"
}
You can also download it from [Github] (https://github.com/desarrolla2/Cache), but no autoloader is provided so you'll need to register it with your own PSR-0 compatible autoloader.
<?php
use Desarrolla2\Cache\Cache;
use Desarrolla2\Cache\Adapter\NotCache;
$cache = new Cache(new NotCache());
$cache->set('key', 'myKeyValue', 3600);
// later ...
echo $cache->get('key');
Use it if you will you have APC cache available in your system.
<?php
use Desarrolla2\Cache\Cache;
use Desarrolla2\Cache\Adapter\Apcu;
$adapter = new Apcu();
$adapter->setOption('ttl', 3600);
$cache = new Cache($adapter);
Use it if you will you have dont have other cache system available in your system or if you like to do your code more portable.
<?php
use Desarrolla2\Cache\Cache;
use Desarrolla2\Cache\Adapter\File;
$cacheDir = '/tmp';
$adapter = new File($cacheDir);
$adapter->setOption('ttl', 3600);
$cache = new Cache($adapter);
Use it if you will you have mencache available in your system.
<?php
use Desarrolla2\Cache\Cache;
use Desarrolla2\Cache\Adapter\Memcache;
$adapter = new Memcache();
$cache = new Cache($adapter);
You can config your connection before
<?php
use Desarrolla2\Cache\Cache;
use Desarrolla2\Cache\Adapter\Memcache;
use \Memcache as Backend
$backend = new Backend();
// configure it here
$cache = new Cache(new Memcache($backend));
Is the same like mencache adapter.
This is the fastest cache type, since the elements are stored in memory. Cache Memory such is very volatile and is removed when the process terminates. Also it is not shared between different processes.
Memory cache have a option "limit", that limit the max items in cache.
<?php
use Desarrolla2\Cache\Cache;
use Desarrolla2\Cache\Adapter\Memory;
$adapter = new Memory();
$adapter->setOption('ttl', 3600);
$adapter->setOption('limit', 200);
$cache = new Cache($adapter);
Use it if you will you have mysqlnd available in your system.
<?php
use Desarrolla2\Cache\Cache;
use Desarrolla2\Cache\Adapter\Mysqli;
$adapter = new Mysqli();
$adapter->setOption('ttl', 3600);
$cache = new Cache($adapter);
<?php
use Desarrolla2\Cache\Cache;
use Desarrolla2\Cache\Adapter\Mysqli;
use \mysqli as Backend
$backend = new Backend();
// configure it here
$cache = new Cache(new Mysqli($backend));
Use it if you will not implement any cache adapter is an adapter that will serve to fool the test environments.
Use it if you will you have redis available in your system.
You need to add predis as dependency in your composer file.
"require": {
//...
"predis/predis": "~1.0.0"
}
other version will have compatibility issues.
<?php
use Desarrolla2\Cache\Cache;
use Desarrolla2\Cache\Adapter\Predis;
$adapter = new Predis();
$cache = new Cache($adapter);
If you need to configure your predis client, you will instantiate it and pass it to constructor.
<?php
use Desarrolla2\Cache\Cache;
use Desarrolla2\Cache\Adapter\Predis;
use Predis\Client as Backend
$adapter = new Predis(new Backend($options));
$cache = new Cache($adapter);
This library implements other adapters as soon as possible, feel free to send new adapters if you think it appropriate.
This can be a list of pending tasks.
- Cleaning cache
- MemcachedAdapter
- Other Adapters
You can contact with me on @desarrolla2.