CRedisCache
in package
implements
ICache
Implements the ICache interface using a Redis server.
This class uses the 'predis/predis' library to communicate with Redis. It supports optional data encryption before storing in Redis.
Table of Contents
Interfaces
- ICache
- Interface for caching data and pages.
Properties
- $area_name : string
- $encryption_cipher : string
- $encryption_enabled : bool
- $encryption_key : string
- $prefix : string
- $redis_client : Client|null
- $uid : int
Methods
- __construct() : mixed
- Constructor for CRedisCache.
- Clear() : void
- {@inheritdoc} Uses SCAN to safely iterate and delete keys without blocking the server.
- ClearArea() : void
- Removes all items from a specific area.
- Delete() : void
- Deletes a specific item from the cache.
- Exists() : bool
- Checks if an item exists in the cache for the current user.
- ExistsGlobal() : bool
- Checks if a global item (valid for all users) exists in the cache.
- Get() : mixed
- Retrieves the value of a cached item.
- GetCacheNames() : array<string|int, string>|false
- Provides a list of all cache keys within the current scope.
- GetCachePath() : string
- {@inheritdoc} This is not applicable for Redis cache.
- GetGlobal() : mixed
- Retrieves the value of a global cached item (valid for all users).
- GetName() : string
- Generates the actual internal key for a given name.
- Set() : void
- Stores an item in the cache for the current user.
- SetArea() : void
- Sets the name of the current area.
- SetGlobal() : void
- Stores a global item in the cache (valid for all users).
- SetUid() : void
- Sets the user ID for the current context.
- WriteDebug() : void
- Writes a message to a cache-specific log file for debugging purposes.
- DecryptData() : string|null
- Decrypts data using OpenSSL.
- EncryptData() : string|null
- Encrypts data using OpenSSL.
Properties
$area_name
private
string
$area_name
= 'default'
The current cache area name for grouping related items
$encryption_cipher
private
string
$encryption_cipher
The OpenSSL cipher algorithm to use
$encryption_enabled
private
bool
$encryption_enabled
Whether data encryption is enabled
$encryption_key
private
string
$encryption_key
The encryption key for data encryption
$prefix
private
string
$prefix
The key prefix for cache isolation (domain:app)
$redis_client
private
Client|null
$redis_client
= null
The Predis client instance for Redis communication
$uid
private
int
$uid
= 0
The current user ID (0 for global cache)
Methods
__construct()
Constructor for CRedisCache.
public
__construct([string|null $domain_name = null ][, string|null $app_name = null ]) : mixed
Parameters
- $domain_name : string|null = null
-
The domain name to prefix keys. Defaults to DOMAIN_NAME constant.
- $app_name : string|null = null
-
The application name to prefix keys. Defaults to APP_NAME constant.
Clear()
{@inheritdoc} Uses SCAN to safely iterate and delete keys without blocking the server.
public
Clear([string $needle = '*' ]) : void
Parameters
- $needle : string = '*'
-
Removes all keys containing this string. If '*' or empty, clears the entire current scope (area/user).
ClearArea()
Removes all items from a specific area.
public
ClearArea(string $area_name) : void
Parameters
- $area_name : string
-
The name of the area to clear.
Delete()
Deletes a specific item from the cache.
public
Delete(string $name) : void
Parameters
- $name : string
-
The key of the item to delete.
Exists()
Checks if an item exists in the cache for the current user.
public
Exists(string $name[, string|null $area_name = null ]) : bool
Parameters
- $name : string
-
The key of the item to check.
- $area_name : string|null = null
-
The area name. If null, the current area is used.
Return values
bool —True if the item exists, false otherwise.
ExistsGlobal()
Checks if a global item (valid for all users) exists in the cache.
public
ExistsGlobal(string $name[, string|null $area_name = null ]) : bool
Parameters
- $name : string
-
The key of the item to check.
- $area_name : string|null = null
-
The area name. If null, the current area is used.
Return values
bool —True if the item exists, false otherwise.
Get()
Retrieves the value of a cached item.
public
Get(string $name[, string|null $area_name = null ]) : mixed
Parameters
- $name : string
-
The key of the item to retrieve.
- $area_name : string|null = null
-
The area name. If null, the current area is used.
Return values
mixed —The cached item's value (can be a scalar, array, or object), or null if not found or on error.
GetCacheNames()
Provides a list of all cache keys within the current scope.
public
GetCacheNames() : array<string|int, string>|false
Return values
array<string|int, string>|false —An array with a list of cache keys, or false if the scope is empty or not accessible.
GetCachePath()
{@inheritdoc} This is not applicable for Redis cache.
public
GetCachePath() : string
Return values
string —The cache path of the application. Returns an empty string if not applicable.
GetGlobal()
Retrieves the value of a global cached item (valid for all users).
public
GetGlobal(string $name[, string|null $area_name = null ]) : mixed
Parameters
- $name : string
-
The key of the item to retrieve.
- $area_name : string|null = null
-
The area name. If null, the current area is used.
Return values
mixed —The cached item's value, or null if not found or on error.
GetName()
Generates the actual internal key for a given name.
public
GetName(string $name) : string
Parameters
- $name : string
-
The public key of the item.
Return values
string —The complete internal key used by the cache system.
Set()
Stores an item in the cache for the current user.
public
Set(string $name, mixed $value[, string|null $area_name = null ]) : void
Parameters
- $name : string
-
The key of the item to store.
- $value : mixed
-
The value to be stored.
- $area_name : string|null = null
-
The area name. If null, the current area is used.
SetArea()
Sets the name of the current area.
public
SetArea(string $area_name) : void
Parameters
- $area_name : string
-
The name of the area.
SetGlobal()
Stores a global item in the cache (valid for all users).
public
SetGlobal(string $name, mixed $value[, string|null $area_name = null ]) : void
Parameters
- $name : string
-
The key of the item to store.
- $value : mixed
-
The value to be stored.
- $area_name : string|null = null
-
The area name. If null, the current area is used.
SetUid()
Sets the user ID for the current context.
public
SetUid(int $uid) : void
Parameters
- $uid : int
-
The ID of the current user. A value of 0 typically represents a global (non-user-specific) context.
WriteDebug()
Writes a message to a cache-specific log file for debugging purposes.
public
WriteDebug(mixed $msg) : void
Parameters
- $msg : mixed
-
The string message or object to write to the log file.
DecryptData()
Decrypts data using OpenSSL.
private
DecryptData(string $data) : string|null
Parameters
- $data : string
-
The base64-encoded string to decrypt.
Return values
string|null —The decrypted plaintext data, or null on failure.
EncryptData()
Encrypts data using OpenSSL.
private
EncryptData(string $data) : string|null
Parameters
- $data : string
-
The plaintext data to encrypt.
Return values
string|null —The base64-encoded encrypted data (IV prepended), or null on failure.