Class: Storage_Interface
source file: /Storage/Storage_Interface.php
Interface for cache storage mechanisms used in the Zebra Cache library.
This interface defines the contract for all storage backends, allowing the Zebra Cache library to support multiple storage mechanisms, such as file-based storage, Memcache, Redis, or any other custom implementation.
Author(s):
License:
Copyright:
- © 2022 - 2025 Stefan Gabos
Class methods
method delete()
boolean
delete
(
string
$key
)
Deletes a cache entry.
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 it 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')) {
// could output something like
// depending on the used storage engine
// [
// 'ttl' => 28
// ]
}
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