Zebra_Cache Zebra Cache

Class: Zebra_Cache

source file: /Zebra_Cache.php

A file-based lightweight PHP caching library that uses file locking to ensure proper functionality under heavy load

Read more here.

Author(s):

Version:

  • 1.3.2 (last revision: January 03, 2023)
    See CHANGELOG

License:

Copyright:

  • © 2022 - 2023 Stefan Gabos

Class properties

mixed $cache_encrypt = false

Whether content in cache files should be openssl_encrypt-ed using the aes-256-cbccipher method.

If you want this enabled you must set its value to a unique cipher to be used for encrypting and decrypting the data.

  1. // use something random, hard to guess
  2. $cache->cache_encrypt 'Z&y&m^VBPJmCVtya';

Default is FALSE

top

boolean $cache_gzcompress = false

Whether cache files should be gzcompress-ed.

  1. // enable gz-compressing, saving disk space
  2. $cache->cache_gzcompress true;

Default is FALSE

top

integer $default_timeout = 3600

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 3600 (one hour)

top

string $extension = ''

The extension to suffix cache files with.

  1. $cache->extension '.cache';

The . (dot) is not implied - you need to also add the dot if you need it

Default is '' (an empty string, nothing)

top

string $path = ''

The path where the cache files to be stored at.

top

Class methods

constructor __construct()

void __construct ( string $path , [ string $extension = '' ] )

Constructor of the class.

  1. $cache new Zebra_Cache('path/to/store/cache-files/');
  2.  
  3. // if a cached, non-expired value for the sought key does not exist
  4. if (!($some_data $cache->get('my-key'))) {
  5.  
  6.     // do whatever you need to retrieve data
  7.     $some_data 'get this data';
  8.  
  9.     // cache the values for one hour (3600 seconds)
  10.     $cache->set('my-key'$some_data3600);
  11.  
  12. }
  13.  
  14. // at this point $some_data will always contain your data, either from cache, or fresh
Arguments
string $path

The path where the cache files to be stored at.

This can be changed later by updating the path property.

string $extension

(Optional) The extension to suffix cache files with.

This can be changed later by updating the extension property.

Default is '' (an empty string)

top

method delete()

boolean delete ( string $key )

Deletes the cache file associated with a key.

  1. $cache->delete('my-key');
Arguments
string $key The key for which to delete the associated cache file.
Tags
return:   Returns TRUE if there was a file to be deleted or FALSE if there was nothing to delete.
top

method get()

boolean get ( string $key , [ boolean $ignore_expiry = false ] )

Retrieves the cached value for the given key if the associated cache file exists and it is not expired.

  1. // if a cached, non-expired value for the sought key does not exist
  2. if (!($some_data $cache->get('my-key'))) {
  3.  
  4.     // do whatever you need to retrieve data
  5.     $some_data 'get this data';
  6.  
  7.     // cache the values for one hour (3600 seconds)
  8.     $cache->set('my-key'$some_data3600);
  9.  
  10. }
  11.  
  12. // at this point $some_data will always contain your data, either from cache, or fresh
Arguments
string $key The key for which to return the cached value.
boolean $ignore_expiry (Optional) If set to TRUE the cached value will be returned even if it is expired.
Useful when you have a different process setting the cache.
Tags
return:   Returns the cached content associated with the given key if the associated cache file exists and it is not expired, or FALSE otherwise.
top

method has()

boolean has ( string $key )

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 if the associated cache file exists and it is not expired, or FALSE otherwise.

If a cached values exists and it is not expired, the returned array will look like

  1. Array (
  2.     'path'       => '',  //  path to the cache file
  3.     'timeout'    => '',  //  the number of seconds the cache was supposed to be valid
  4.     'ttl'        => '',  //  time to live, the number of seconds remaining until the cache expires
  5. )
top

method set()

boolean set ( string $key , mixed $data , [ integer $timeout = -1 ] )

Caches data identified by a unique key for a specific number of seconds.

  1. // if a cached, non-expired value for the sought key does not exist
  2. if (!($some_data $cache->get('my-key'))) {
  3.  
  4.     // do whatever you need to retrieve data
  5.     $some_data 'get this data';
  6.  
  7.     // cache the values for one hour (3600 seconds)
  8.     $cache->set('my-key'$some_data3600);
  9.  
  10. }
  11.  
  12. // at this point $some_data will always contain your data, either from cache, or fresh
Arguments
string $key

unique name identifying the data that is about to be stored.

The data will be saved in a file on disk at path and in the form of

md5($key) . '-' . $timeout . extension

Therefore, a cache file for the key my-key, having a validity of 3600 seconds (one hour) and with extension set to .cache would look like

515780610702189dabd912e9c9ef6f38-3600.cache

mixed $data

The data to be stored.

The data will be stored in serialized form.

Optionally, stored data can be encrypted and/or compressed.

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 as expired.

If omitted, the value of default_timeout will be used.

Tags
return:   Returns TRUE
top