stefangabos / zebra_cache
A lightweight and flexible PHP caching library with support for file, Redis, and Memcached storage backends.
Requires
- php: >=7
Requires (Dev)
- phpstan/phpstan: ^1.8
- phpunit/phpunit: ^8
- squizlabs/php_codesniffer: ^3
README
Zebra Cache
A lightweight and flexible PHP caching library with support for file, Redis, and Memcached storage backends.
This library provides a unified interface for caching data, allowing the use of various storage backends like file-based caching, Redis, Memcached, or custom implementations.
It supports common caching operations such as storing, retrieving and deleting data, as well as checking for the existence of a cache entry.
Features
- pluggable architecture for using different storage mechanisms
- flexible key-value caching
- automatic expiration of cache items based on a specified time-to-live (TTL) value
- extensible design allowing developers to integrate new storage backends
- easy-to-use API with consistent behavior across different backends
- support for multiple instances, allowing you to use different cache configurations for different parts of your application
📔 Documentation
Check out the awesome documentation!
🎂 Support the development of this project
Your support is greatly appreciated and it keeps me motivated continue working on open source projects. If you enjoy this project please star it by clicking on the star button at the top of the page. If you're feeling generous, you can also buy me a coffee through PayPal or become a sponsor. Thank you for your support! 🎉
Requirements
PHP 7.0.0+
Use version 1.3.2 if you need support for PHP 5.3.0+
Installation
Install via Composer
# get the latest stable release composer require stefangabos/zebra_cache # get the latest commit composer require stefangabos/zebra_cache:dev-master
Initializing the storage engine
Initializing file-based storage:
// make sure you have this at the top of your script use stefangabos\Zebra_Cache\Zebra_Cache; use stefangabos\Zebra_Cache\Storage\Storage_File; // initialize file-based storage $storage = new Storage_File('/path/to/cache/folder');
Initializing Redis-based storage:
// make sure you have this at the top of your script use stefangabos\Zebra_Cache\Zebra_Cache; use stefangabos\Zebra_Cache\Storage\Storage_Redis; // connect to a Redis server $redis = new Redis(); $redis->connect('127.0.0.1', 6379); // pass the $redis instance as argument to initialize the Redis-based storage $storage = new Storage_Redis($redis); // finally, instantiate the caching library using the storage engine configured above $cache = new stefangabos\Zebra_Cache\Zebra_Cache($storage);
Initializing Memcached-based storage:
There are two PHP extensions for working with Memcached: the memcache extension, which is older and less commonly used, and memcached which is generally preferred for better features and compatibility. This library supports both.
// make sure you have this at the top of your script use stefangabos\Zebra_Cache\Zebra_Cache; use stefangabos\Zebra_Cache\Storage\Storage_Memcached; // connect to a Memcached server (using the `memcached` extension) $memcache = new Memcached(); $memcache->addServer('localhost', 11211); // OR using the `memcache` extension $memcache = new Memcache(); $memcache->addServer('localhost', 11211); // pass the $memcache instance as argument to initialize the Memcached-based storage $storage = new Storage_Memcached($memcache);
Initializing the main library and setting/getting values
Once the storage engine is initialized:
// instantiate the caching library using the chosen storage engine $cache = new Zebra_Cache($storage); // if a cached, non-expired value for the sought key does not exist if (!($my_data = $cache->get('my-key'))) { // do whatever you need to retrieve data $my_data = 'my data'; // cache the values for one 10 minutes (10 x 60 seconds) $cache->set('my-key', $my_data, 10 * 600); } // at this point $my_data will always contain data, either from cache, or fresh
Getting information about cached data
if ($info = $cache->has('my-key')) { print_r('<pre>'); print_r($info); // for file-based storage the output will look something like // [ // 'path' => '', // path to the cache file // 'timeout' => '', // the number of seconds the cache was supposed to be valid // 'ttl' => '', // number of seconds remaining until the cache expires // ] }
Deleting cached data
$cache->delete('my-key');