CodeIgniter v4.7 API

RedisHandler extends BaseHandler
in package

Redis cache handler

Table of Contents

Constants

MAX_KEY_LENGTH  : mixed = PHP_INT_MAX
Maximum key length.
RESERVED_CHARACTERS  : mixed = '{}()/\@:'
Reserved characters that cannot be used in a key or tag. May be overridden by the config.
SAVE_SCRIPT  : mixed = <<<'LUA' local res = redis.pcall('HMSET', KEYS[...
Lua script to execute HMSET and EXPIREAT in a single round trip.

Properties

$config  : array{host: string, password: string|null, port: int, timeout: int, persistent: bool, database: int}
Default config
$prefix  : string
Prefix to apply to cache keys.
$redis  : Redis|null
Redis connection

Methods

__construct()  : mixed
Note: Use `CacheFactory::getHandler()` to instantiate.
clean()  : bool
Will delete all items in the entire cache.
decrement()  : int
Performs atomic decrementation of a raw stored value.
delete()  : bool
Deletes a specific item from the cache store.
deleteMatching()  : int
Deletes items from the cache store matching a given pattern.
get()  : mixed
Attempts to fetch an item from the cache store.
getCacheInfo()  : array<string|int, mixed>|false|object|null
Returns information on the entire cache.
getMetaData()  : array<string, mixed>|null
Returns detailed information about the specific item in the cache.
increment()  : int
Performs atomic incrementation of a raw stored value.
initialize()  : void
Takes care of any handler-specific setup that must be done.
isSupported()  : bool
Determines if the driver is supported on this system.
ping()  : bool
Check if connection is alive.
reconnect()  : bool
Reconnect to the cache server.
remember()  : mixed
Attempts to get an item from the cache, or executes the callback and stores the result on cache miss.
save()  : bool
Saves an item to the cache store.
validateKey()  : string
Validates a cache key according to PSR-6.
isDataCommandPermissionError()  : bool
Checks whether the message means that a data command executed inside the save script was denied by an ACL.
isEvalUnavailable()  : bool
Checks whether the message only means that the EVAL command itself is not available to this client, either because it was renamed or because ACL denies it.

Constants

MAX_KEY_LENGTH

Maximum key length.

public mixed MAX_KEY_LENGTH = PHP_INT_MAX

RESERVED_CHARACTERS

Reserved characters that cannot be used in a key or tag. May be overridden by the config.

in favor of the Cache config

public mixed RESERVED_CHARACTERS = '{}()/\@:'

From https://github.com/symfony/cache-contracts/blob/c0446463729b89dd4fa62e9aeecc80287323615d/ItemInterface.php#L43

SAVE_SCRIPT

Lua script to execute HMSET and EXPIREAT in a single round trip.

private mixed SAVE_SCRIPT = <<<'LUA' local res = redis.pcall('HMSET', KEYS[1], '__ci_type', ARGV[2], '__ci_value', ARGV[3]) if type(res) == 'table' and res.err then return res end local exp = redis.pcall('EXPIREAT', KEYS[1], ARGV[1]) if type(exp) == 'table' and exp.err then return exp end return 1 LUA

Both commands run through pcall and their error reply is returned as-is, so a failing HMSET never runs EXPIREAT and the original error message and type are preserved. phpredis exposes the message through Redis::getLastError(); save() rethrows a permission error as RedisException, as the sequential implementation did, and keeps reporting any other error (e.g. WRONGTYPE) as false.

If HMSET succeeds and EXPIREAT is rejected, the hash is stored without a TTL, exactly as with the previous sequential implementation (Redis does not roll back script effects).

Properties

$config

Default config

protected array{host: string, password: string|null, port: int, timeout: int, persistent: bool, database: int} $config = ['host' => '127.0.0.1', 'password' => null, 'port' => 6379, 'timeout' => 0, 'persistent' => false, 'database' => 0]

$prefix

Prefix to apply to cache keys.

protected string $prefix

May not be used by all handlers.

Methods

__construct()

Note: Use `CacheFactory::getHandler()` to instantiate.

public __construct(Cache $config) : mixed
Parameters
$config : Cache

clean()

Will delete all items in the entire cache.

public clean() : bool
Return values
bool —

Success or failure

decrement()

Performs atomic decrementation of a raw stored value.

public decrement(string $key[, int $offset = 1 ]) : int
Parameters
$key : string

Cache ID

$offset : int = 1

Step/value to increase by

Return values
int

delete()

Deletes a specific item from the cache store.

public delete(string $key) : bool
Parameters
$key : string

Cache item name

Return values
bool —

Success or failure

deleteMatching()

Deletes items from the cache store matching a given pattern.

public deleteMatching(string $pattern) : int
Parameters
$pattern : string

Cache items glob-style pattern

Return values
int —

Number of deleted items

get()

Attempts to fetch an item from the cache store.

public get(string $key) : mixed
Parameters
$key : string

Cache item name

getCacheInfo()

Returns information on the entire cache.

public getCacheInfo() : array<string|int, mixed>|false|object|null

The information returned and the structure of the data varies depending on the handler.

Return values
array<string|int, mixed>|false|object|null

getMetaData()

Returns detailed information about the specific item in the cache.

public getMetaData(string $key) : array<string, mixed>|null
Parameters
$key : string

Cache item name.

Return values
array<string, mixed>|null —

Returns null if the item does not exist, otherwise array<string, mixed> with at least the 'expire' key for absolute epoch expiry (or null).

increment()

Performs atomic incrementation of a raw stored value.

public increment(string $key[, int $offset = 1 ]) : int
Parameters
$key : string

Cache ID

$offset : int = 1

Step/value to increase by

Return values
int

initialize()

Takes care of any handler-specific setup that must be done.

public initialize() : void

isSupported()

Determines if the driver is supported on this system.

public isSupported() : bool
Return values
bool

ping()

Check if connection is alive.

public ping() : bool

Default implementation for handlers that don't require connection management. Handlers with persistent connections (Redis, Predis, Memcached) should override this.

Return values
bool

reconnect()

Reconnect to the cache server.

public reconnect() : bool

Default implementation for handlers that don't require connection management. Handlers with persistent connections (Redis, Predis, Memcached) should override this.

Return values
bool

remember()

Attempts to get an item from the cache, or executes the callback and stores the result on cache miss.

public remember(string $key, int $ttl, Closure $callback) : mixed
Parameters
$key : string

Cache item name

$ttl : int

Time To Live, in seconds

$callback : Closure

Callback executed on cache miss

save()

Saves an item to the cache store.

public save(string $key, mixed $value[, int $ttl = 60 ]) : bool
Parameters
$key : string

Cache item name

$value : mixed

The data to save

$ttl : int = 60

Time To Live, in seconds (default 60)

Return values
bool —

Success or failure

validateKey()

Validates a cache key according to PSR-6.

public static validateKey(mixed $key[, string $prefix = '' ]) : string

Keys that exceed MAX_KEY_LENGTH are hashed. From https://github.com/symfony/cache/blob/7b024c6726af21fd4984ac8d1eae2b9f3d90de88/CacheItem.php#L158

Parameters
$key : mixed

The key to validate

$prefix : string = ''

Optional prefix to include in length calculations

Tags
throws
InvalidArgumentException

When $key is not valid

Return values
string

isDataCommandPermissionError()

Checks whether the message means that a data command executed inside the save script was denied by an ACL.

private static isDataCommandPermissionError(string $message) : bool

EVAL availability was already checked, so the denied command can only be HMSET or EXPIREAT. Redis reports it either as a command level denial or as an ACL failure while running the script.

Predis throws for every error reply, so this mapping only exists for the phpredis false plus getLastError() path.

Parameters
$message : string
Return values
bool

isEvalUnavailable()

Checks whether the message only means that the EVAL command itself is not available to this client, either because it was renamed or because ACL denies it.

private static isEvalUnavailable(string $message) : bool

Keep the patterns in sync with PredisHandler::isEvalUnavailable().

Parameters
$message : string
Return values
bool
On this page

Search results