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-cbc
cipher method.
If you want this enabled you must set its value to a unique cipher to be used for encrypting and decrypting the data.
// use something random, hard to guess
Default is FALSE
top
boolean $cache_gzcompress = false
Whether cache files should be gzcompress-ed.
// enable gz-compressing, saving disk space
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.
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.
$cache =
new Zebra_Cache('path/to/store/cache-files/');
// if a cached, non-expired value for the sought key does not exist
if (!($some_data =
$cache->get('my-key'))) {
// do whatever you need to retrieve data
$some_data = 'get this data';
// cache the values for one hour (3600 seconds)
$cache->set('my-key', $some_data, 3600);
}
// 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.
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.
// if a cached, non-expired value for the sought key does not exist
if (!($some_data =
$cache->get('my-key'))) {
// do whatever you need to retrieve data
$some_data = 'get this data';
// cache the values for one hour (3600 seconds)
$cache->set('my-key', $some_data, 3600);
}
// 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.
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 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 Array (
'path' => '', // path to the cache file
'timeout' => '', // the number of seconds the cache was supposed to be valid
'ttl' => '', // time to live, the number of seconds remaining until the cache expires
)
|
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.
// if a cached, non-expired value for the sought key does not exist
if (!($some_data =
$cache->get('my-key'))) {
// do whatever you need to retrieve data
$some_data = 'get this data';
// cache the values for one hour (3600 seconds)
$cache->set('my-key', $some_data, 3600);
}
// 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
top