Caching Data
Caching Data
Hazaar has a full featured caching system with multiple frontends and backends that are designed to work in any combination. It is possible to cache just about anything using the caching classes. Using a cache system to store frequently retrieved data can drastically increase the performance of your application. Most cache backends also support inter-session caching so data can be cached in one session and accessed via another, so that cached data can benefit the application as a whole and not just the current client session.
The cache system is implemented as a collection of frontends and backends. Frontends dictate the way in which the cache system is accessed by the application and enforce data lifetime expiration. For example, the basic frontend exposes the get() and set() methods for loading and saving data, while the func frontend exposes a call() method. Backends on the other hand, dictate how and where the data is actually stored and the mechanism used to expire stale data.
Caching Concepts
There are three elements to consider when using the Hazaar caching system.
- Cache key: A unique string identifier used to identify cache records.
- Cache lifetime: Specifies how long the cached data is considered 'fresh'.
- Application execution path: Allows parts of the application to be skipped entirely if cached data is available, which is key to boosting performance.
Frontends are designed to return false if no cached data is available and this can be used in test conditions by the application to determine if cached data is available and code execution should be skipped. If no cached data is available it is up to the application to generate the data itself and then store the data in the cache before carrying on.
Available Frontends
Hazaar\Cache\Adapter- The basic frontend is for storing key/value pairs. Keys are alpha-numeric but values can be anything that can be serialized, including class objects (as long as they are loaded in the correct context). (Supports timeout)Hazaar\Cache\Func- The function frontend caches the result of a function call. You make a consistent function call via the caching object and the function will only be executed if cached data is unavailable or expired. If the function returns an object, then the object can only be stored if it is serializable. (Supports timeout)Hazaar\Cache\Output- The output frontend captures content from an output buffer based on a key. If the key is cached, it returns the cached content and you can skip rendering. If not, it starts buffering and returns false so you can generate content and persist it. (Supports timeout)
Available Backends
Hazaar\Cache\Backend\Session- The session backend will store caching data in the current users session using the $_SESSION global variable. The session cache backend is considered the simplest backend for caching data but has the drawback that cached data is only accessible by the current user session and can not be shared amongst sessions.Hazaar\Cache\Backend\File- The file backend will store caching data in the system temporary directory (usually /tmp). Files are plain text serialized data which stores cache data along with caching information like a timestamp used to calculate the expiration timeout. The file cache backend is considered the simplest backend for caching data that is accessible between sessions.Hazaar\Cache\Backend\Apc- This backend uses the Advanced PHP Caching module available for PHP. APC has a 'user cache' function that this backend takes advantage of to store data. The APC module supports automatic cache expiration, stores information in active memory and is considered the fastest backend to use for caching.Hazaar\Cache\Backend\Shm- The shared memory backend stores cached values in a SysV shared memory segment. It is the fastest backend when available, but data is lost on restart.Hazaar\Cache\Backend\Redis- The Redis backend stores cached data with a Redis server. Redis is caching for serious, large-scale applications.Hazaar\Cache\Backend\Memcached- The memcached backend stores cached data with a memcached server. Memcached is caching for medium-scale applications.Hazaar\Cache\Backend\Dbi- The DBI backend stores caching information using the Hazaar DBI adapter. It can use any supported DBI driver.Hazaar\Cache\Backend\Chain- The chaining backend allows multiple backends to be used for storing cached data. This allows a mix of backends to be used as persistent backends usually have lower performance as compared to non-persistent backends. Using chaining, a high performance backend can be used as the primary and a low performance persistent backend can be used to protect cached data. The chain backend can be used by simply providing an array of backends to use to theHazaar\Cache\Adapterobject.
Example Usage
Below is an example of how to use the Hazaar\Cache\Adapter class to cache some data from a database query using the Hazaar\Cache\Backend\Redis backend.
$cache = new Hazaar\Cache\Adapter('redis', [
'ttl' => 300,
]);
if (($result = $cache->get('db_result')) === false) {
$db = Hazaar\DBI\Adapter::create();
$result = $db->mytable->findOne(['id' => 1234]);
$cache->set('db_result', $result);
}
return $result;