Zebra_Cache Zebra Cache

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).

  1. // enable gz-compressing
  2. $cache->cache_gzcompress true;

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:

  1. // make sure you have this at the top of your script
  2. use stefangabos\Zebra_Cache\Storage\Storage_File;
  3.  
  4. // initialize file-based storage
  5. $storage new Storage_File('/path/to/cache/folder');
  6.  
  7. // or, if you don't want the "use" at the top of the script, initialize the file-based storage like this
  8. // $storage = new stefangabos\Zebra_Cache\Storage\Storage_File('/path/to/cache/folder');

Initializing Redis-based storage:

  1. // make sure you have this at the top of your script
  2. use stefangabos\Zebra_Cache\Storage\Storage_Redis;
  3.  
  4. // connect to a Redis server
  5. $redis new Redis();
  6. $redis->connect('127.0.0.1'6379);
  7.  
  8. // pass the $redis instance as argument to initialize the Redis-based storage
  9. $storage new Storage_Redis($redis);
  10.  
  11. // or, if you don't want the "use" at the top of the script, initialize the Redis-based storage like this
  12. // $storage = new stefangabos\Zebra_Cache\Storage\Storage_Redis($redis);
  13.  
  14. // finally, instantiate the caching library using the storage engine configured above
  15. $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.

  1. // make sure you have this at the top of your script
  2. use stefangabos\Zebra_Cache\Storage\Storage_Memcached;
  3.  
  4. // connect to a Memcached server (using the `memcached` extension)
  5. $memcache new Memcached();
  6. $memcache->addServer('localhost'11211);
  7.  
  8. // OR using the `memcache` extension
  9. $memcache new Memcache();
  10. $memcache->addServer('localhost'11211);
  11.  
  12. // pass the $memcache instance as argument to initialize the Memcached-based storage
  13. $storage new Storage_Memcached($memcache);
  14.  
  15. // or, if you don't want the "use" at the top of the script,
  16. // initialize the Memcached-based storage like this
  17. // $storage = new stefangabos\Zebra_Cache\Storage\Storage_Memcached($memcache);

Once the storage engine is initialized:

  1. // instantiate the caching library using the chosen storage engine
  2. $cache new stefangabos\Zebra_Cache\Zebra_Cache($storage);
  3.  
  4. // if a cached, non-expired value for the sought key does not exist
  5. if (!($my_data $cache->get('my-key'))) {
  6.  
  7.     // do whatever you need to retrieve data
  8.     $my_data 'my data';
  9.  
  10.     // cache the values for one 10 minutes (10 x 60 seconds)
  11.     $cache->set('my-key'$my_data10 600);
  12.  
  13. }
  14.  
  15. // 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.

  1. $cache->delete('my-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.

  1. // if a cached, non-expired value for the sought key does not exist
  2. if (!($my_data $cache->get('my-key'))) {
  3.  
  4.     // do whatever you need to retrieve data
  5.     $my_data 'my data';
  6.  
  7.     // cache the values for one hour (3600 seconds)
  8.     $cache->set('my-key'$my_data3600);
  9.  
  10. }
  11.  
  12. // 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.

  1. if ($cached_info $cache->has('my-key')) {
  2.  
  3.     print_r('<pre>');
  4.     print_r($cached_info);
  5.  
  6. }
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.

  1. // if a cached, non-expired value for the sought key does not exist
  2. if (!($my_data $cache->get('my-key'))) {
  3.  
  4.     // do whatever you need to retrieve data
  5.     $my_data 'my data';
  6.  
  7.     // cache the values for one hour (3600 seconds)
  8.     $cache->set('my-key'$my_data3600);
  9.  
  10. }
  11.  
  12. // 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