# Cache [SensioLabsInsight](https://insight.sensiolabs.com/projects/5f139261-1ac1-4559-846a-723e09319a88) A simple cache library. Implements different adapters that you can use and change easily by a manager or similar. [![Latest version][ico-version]][link-packagist] [![Latest version][ico-pre-release]][link-packagist] [![Software License][ico-license]][link-license] [![Build Status][ico-travis]][link-travis] [![Coverage Status][ico-coveralls]][link-coveralls] [![Quality Score][ico-code-quality]][link-code-quality] [![Sensiolabs Insight][ico-sensiolabs]][link-sensiolabs] [![Total Downloads][ico-downloads]][link-downloads] [![Today Downloads][ico-today-downloads]][link-downloads] [![Gitter][ico-gitter]][link-gitter] ## Installation ### With Composer It is best installed it through [packagist](http://packagist.org/packages/desarrolla2/cache) by including `desarrolla2/cache` in your project composer.json require: ``` json "require": { // ... "desarrolla2/cache": "~2.0" } ``` ### Without Composer 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. ## Usage ``` php set('key', 'myKeyValue', 3600); // later ... echo $cache->get('key'); ``` ## Adapters ### Apcu Use it if you will you have APC cache available in your system. ``` php setOption('ttl', 3600); $cache = new Cache($adapter); ``` ### File 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 setOption('ttl', 3600); $cache = new Cache($adapter); ``` ### Memcache Use it if you will you have mencache available in your system. ``` php setOption('ttl', 3600); $adapter->setOption('limit', 200); $cache = new Cache($adapter); ``` ### Mongo Use it to store the cache in a Mongo database. Requires the [(legacy) mongo extension](http://php.net/mongo) or the [mongodb/mongodb](https://github.com/mongodb/mongo-php-library) library. You may pass either a database or collection object to the constructor. If a database object is passed, the `items` collection within that DB is used. ``` php selectDatabase($dbname); $adapter = new Mongo($database); $adapter->setOption('ttl', 3600); $cache = new Cache($adapter); ``` ``` php selectDatabase($dbName); $collection = $database->selectCollection($collectionName); $adapter = new Mongo($collection); $adapter->setOption('ttl', 3600); $cache = new Cache($adapter); ``` _Note that expired cache items aren't automatically deleted. To keep your database clean, you should create a [ttl index](https://docs.mongodb.org/manual/core/index-ttl/)._ ``` db.items.createIndex( { "ttl": 1 }, { expireAfterSeconds: 30 } ) ``` ### Mysqli You need to create Mysql table as follow ```mysql CREATE TABLE cache ( k varchar(255) NOT NULL, v text NOT NULL, t int(10) UNSIGNED NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8; ALTER TABLE cache ADD PRIMARY KEY (k) USING BTREE; ``` ``` php setOption('ttl', 3600); $cache = new Cache($adapter); ``` ``` php