Class: Zebra_Cache
source file: /Zebra_Cache.php
A lightweight PHP caching library supporting multiple storage mechanisms.
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 by implementing the Storage_Interface
- 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
Read more here.
Author(s):
Version:
- 2.0.1 (last revision: January 15, 2025)
See CHANGELOG
License:
Copyright:
- © 2022 - 2025 Stefan Gabos
Class properties
boolean $cache_gzcompress = falsepublic
Whether data should be gzcompress-ed before being set() (using the function's default settings).
// enable gz-compressing
Note that the library will not check if the stored data is compressed or not - if this property is set to TRUE
, it will always try to uncompress data when using get()! Therefore, be careful not to mix compressed and uncompressed data.
Default is FALSE
top
integer $default_timeout = 300public
The default number of seconds after which cached data will be considered as expired.
This is used by the set() method when the timeout
argument is omitted.
Default is 300
(5 minutes)
top
Class methods
constructor __construct()
Zebra_Cache
__construct
(
Storage_Interface
$storage
)
Initializes the cache system with the provided storage backend.
Initializing file-based storage:
// make sure you have this at the top of your script
// initialize file-based storage
// or, if you don't want the "use" at the top of the script, initialize the file-based storage like this
// $storage = new stefangabos\Zebra_Cache\Storage\Storage_File('/path/to/cache/folder');
Initializing Redis-based storage:
// make sure you have this at the top of your script
// 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
// or, if you don't want the "use" at the top of the script, initialize the Redis-based storage like this
// $storage = new stefangabos\Zebra_Cache\Storage\Storage_Redis($redis);
// finally, instantiate the caching library using the storage engine configured above
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
// 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
// or, if you don't want the "use" at the top of the script,
// initialize the Memcached-based storage like this
// $storage = new stefangabos\Zebra_Cache\Storage\Storage_Memcached($memcache);
Once the storage engine is initialized:
// instantiate the caching library using the chosen storage engine
// 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
Arguments
Storage_Interface |
$storage |
The storage backend to use. An instance of one of the supported storage mechanisms: |
top
method delete()
boolean
delete
(
string
$key
)
Deletes the cache entry associated with the specified key.
Arguments
string |
$key |
The key identifying the cache entry to delete. |
Tags
return: |
Returns TRUE if an entry was deleted, or FALSE if there was nothing to delete. |
top
method get()
mixed
get
(
string
$key
)
Retrieves an item from the cache.
Retrieves the cached value for the specified key if the corresponding cache entry exists and it is not expired, or FALSE
otherwise.
// 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 hour (3600 seconds)
$cache->set('my-key', $my_data, 3600);
}
// at this point $my_data will always contain data, either from cache, or fresh
Arguments
string |
$key |
The key for which to return the cached value. |
Tags
return: |
Retrieves the cached value for the specified key if the corresponding cache entry exists and is not expired, or FALSE otherwise. |
top
method has()
mixed
has
(
string
$key
)
Checks if a cache entry exists.
Checks whether a cached, non-expired value exists for the given key, and returns information about it.
if ($cached_info =
$cache->has('my-key')) {
}
Arguments
string |
$key |
The key for which to check if a cached value exists. |
Tags
return: |
Returns information about the cache associated with the given key as an array, if the associated cache entry exists and it is not expired, or FALSE otherwise. For details on the exact return values, please refer to the documentation of the specific storage engine being used, as each may provide additional information unique to its implementation. |
top
method set()
boolean
set
(
string
$key
,
mixed
$data
,
[
int
$timeout
=
0
]
)
Stores an item in the cache.
Caches data identified by a unique key for a specific number of seconds. If the key already exists, the data will be overwritten.
// 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 hour (3600 seconds)
$cache->set('my-key', $my_data, 3600);
}
// at this point $my_data will always contain data, either from cache, or fresh
Arguments
string |
$key |
Unique name identifying the data that is about to be stored. |
mixed |
$data |
The data to be stored. Anything that evaluates as falsy will not be cached! These values are: the keyword false the integer zero (0 ) the floating-point number zero (0.0 ) the empty string ('' ) and the string "0" the empty string ('' ) and the string "0" the NULL value an empty array (an array with zero elements) |
integer |
$timeout |
Optional The number of seconds after which the cached data will be considered expired. Valid values are integers greater than 0 . The value 0 is also valid and it indicates that the value of $default_timeout will be applied. Providing an invalid value will result in the $default_timeout being applied instead. Default is 0 ($default_timeout) |
Tags
return: |
Returns TRUE on success or FALSE if data could not be cached. |
top