Quick Cache
A quick and easy to use PSR-2 driver based caching library that will cache simple key/value pairs or call methods and cache their results., (*1)
, (*2)
Includes Drivers For
- Redis (using the fantastic predis library)
- APC (requires APC installation / extension) *
- Filesystem
* APC driver doesn't support clearing class/method caches manually via $cache->clear()
., (*3)
Author
License
MIT License, (*4)
Usage
First install it via composer by placing this in a composer.json file, (*5)
{
"require": {
"quick/cache": "v1.0.0"
}
}
and running composer.phar install
, (*6)
Now you can use it in your code by calling the class:, (*7)
$cache = new Quick\Cache;
$cache->set('name', 'Jerel Unruh', $ttl = 3600); /* will expire in 3600 seconds */
$name = $cache->get('name');
$cache->forget('name');
Let it call your methods for you and handle the results:, (*8)
// you can pass any config items in an array
$cache = new Quick\Cache(array('driver' => 'redis'));
// pass any arguments as an array
$cache->method($this->UserModel, 'getUsersByGroup', array('admin', 'desc'), 3600);
$cache->method('Project\Model\UserModel', 'getUsersByGroup');
// clear all the data cached for this class
$cache->clear('Project\Model\UserModel');
// or just this class + method
$cache->clear('Project\Model\UserModel', 'getUsersByGroup');
Flush all cached items for this driver. Don't use this in production as it's expensive. Use it when taking an app from staging to live or etc., (*9)
$cache = new Quick\Cache;
$cache->flush();
Configuration
All configuration details can be set in the config files in vendor/quick/cache/config. There is also a config class that allows you to set details programmatically. The only thing that cannot be changed with the config class is the driver. It must be set in the global config file or when instantiating the class;, (*10)
$cache = new Quick\Cache(array('driver' => 'file'));
$config = $cache->config_instance();
$config->set('cache_path', 'project/cache/');
At the same time we can run another instance with different configurations, (*11)
$redis_cache = new Quick\Cache(array('driver' => 'redis'));
$redis_config = $redis_cache->config_instance();
$redis_config->set_many(array(
'redis_connection' => array(
'host' => '127.0.0.1',
'port' => 6379,
),
'redis_prefix' => 'cache',
));
// if you set the connection details manually you must refresh the connection
$redis_cache->connect();
// Create an APC driver instance
$apc_cache = new Quick\Cache(array('driver' => 'apc'));
$apc_cache->set('name', 'Jerel Unruh', 3600);
Other methods, (*12)
$cache_path = $config->get('cache_path');
$config_items = $config->get_all();
$config->load('custom_driver');
Testing
Quick Cache is unit tested using phpUnit. I use Guard to run my tests while I work, if you have it installed you can test like this:, (*13)
cd ./vendor/quick/cache
guard
If not you can run them via phpUnit, (*14)
cd ./vendor/quick/cache
phpunit
Do not run these tests on a production environment! It will FLUSH your database!, (*15)
Errors
If Quick Cache encounters a serious error that it needs to tell you about (such as unwriteable directories) it will throw a QuickCacheException
, (*16)