Documentation

Redis extends AbstractAdapter
in package
uses NamespacedVersionedKeys

Redis cache adapter class

Tags
category

Pop

author

Nick Sagona, III nick@popphp.org

copyright

Copyright (c) 2009-2026 Nick Sagona, III

license

https://www.popphp.org/license New BSD License

version
5.0.0

Table of Contents

Constants

INCREMENT_SCRIPT  = <<<'LUA' local key = KEYS[1] local amount = tonumber(ARGV[1]) local initial = tonumber(ARGV[2]) local ttl = tonumber(ARGV[3]) if redis.call('EXISTS', key) == 0 then if ttl > 0 then redis.call('SET', key, initial, 'EX', ttl) else redis.call('SET', key, initial) end end return redis.call('INCRBY', key, amount) LUA
Lua script for incrementItem()/decrementItem(): atomically seeds a new counter at the given initial value (with a TTL, if any) when the key doesn't exist yet, then applies the delta. Redis's single-threaded script execution guarantees the whole sequence is atomic. decrementItem() reuses this same script by passing a negative amount — INCRBY with a negative delta is exactly DECRBY.

Properties

$clock  : ClockInterface
Clock used to resolve the current time
$namespace  : string
Cache namespace
$redis  : Redis|null
Redis object
$ttl  : int
Global time-to-live

Methods

__construct()  : mixed
Constructor
clear()  : Redis
Clear all stored values from cache
decrementItem()  : int
Atomically decrement a counter in cache, creating it at $initial if it doesn't exist
deleteItem()  : Redis
Delete a value in cache
destroy()  : Redis
Destroy cache resource
getItem()  : mixed
Get an item from cache
getItemTtl()  : int
Get the time-to-live for an item in cache
getTtl()  : int
Get the global time-to-live for the cache object
getVersion()  : string
Get the current version of redis.
hasItem()  : bool
Determine if the item exist in cache
incrementItem()  : int
Atomically increment a counter in cache, creating it at $initial if it doesn't exist
redis()  : Redis
Get the redis object.
saveItem()  : Redis
Save an item to cache
setTtl()  : AbstractAdapter
Set the global time-to-live for the cache adapter
evalIncrement()  : int
Shared implementation for incrementItem()/decrementItem(), executing INCREMENT_SCRIPT atomically
fetchVersion()  : mixed
Fetch the raw version value from redis, or false if it isn't set
isFresh()  : bool
Determine if a cache envelope (with 'start'/'ttl' keys) is still within its time-to-live
key()  : string
Build the versioned, namespaced storage key for an item id
resolveVersion()  : int
Resolve the current version for this namespace, defaulting to 1
versionKey()  : string
Get the storage key for this namespace's version counter

Constants

INCREMENT_SCRIPT

Lua script for incrementItem()/decrementItem(): atomically seeds a new counter at the given initial value (with a TTL, if any) when the key doesn't exist yet, then applies the delta. Redis's single-threaded script execution guarantees the whole sequence is atomic. decrementItem() reuses this same script by passing a negative amount — INCRBY with a negative delta is exactly DECRBY.

protected string INCREMENT_SCRIPT = <<<'LUA' local key = KEYS[1] local amount = tonumber(ARGV[1]) local initial = tonumber(ARGV[2]) local ttl = tonumber(ARGV[3]) if redis.call('EXISTS', key) == 0 then if ttl > 0 then redis.call('SET', key, initial, 'EX', ttl) else redis.call('SET', key, initial) end end return redis.call('INCRBY', key, amount) LUA

Properties

$namespace

Cache namespace

protected string $namespace = 'pop_cache'

$redis

Redis object

protected Redis|null $redis = null

Methods

__construct()

Constructor

public __construct([int $ttl = 0 ][, string $host = 'localhost' ][, int $port = 6379 ][, string $namespace = 'pop_cache' ][, ClockInterface $clock = new ClockSystemClock() ]) : mixed

Instantiate the memcache cache object

Parameters
$ttl : int = 0
$host : string = 'localhost'
$port : int = 6379
$namespace : string = 'pop_cache'
$clock : ClockInterface = new ClockSystemClock()
Tags
throws
Exception

clear()

Clear all stored values from cache

public clear() : Redis
Return values
Redis

decrementItem()

Atomically decrement a counter in cache, creating it at $initial if it doesn't exist

public decrementItem(string $id[, int $amount = 1 ][, int $initial = 0 ][, int|null $ttl = null ]) : int

Stored as a raw scalar via a Lua script (see INCREMENT_SCRIPT), bypassing the start/ttl/value envelope used by saveItem()/getItem() entirely — a counter key and a saveItem()-managed key are two incompatible storage formats on this adapter, and a counter is not readable via getItem(). Unlike Memcached::decrement(), Redis allows the result to go negative (no clamping).

Parameters
$id : string
$amount : int = 1
$initial : int = 0
$ttl : int|null = null
Tags
throws
Exception
Return values
int

deleteItem()

Delete a value in cache

public deleteItem(string $id) : Redis
Parameters
$id : string
Return values
Redis

destroy()

Destroy cache resource

public destroy() : Redis
Return values
Redis

getItem()

Get an item from cache

public getItem(string $id[, mixed $default = false ]) : mixed
Parameters
$id : string
$default : mixed = false

getItemTtl()

Get the time-to-live for an item in cache

public getItemTtl(string $id[, int $default = 0 ]) : int
Parameters
$id : string
$default : int = 0
Return values
int

getTtl()

Get the global time-to-live for the cache object

public getTtl() : int
Return values
int

getVersion()

Get the current version of redis.

public getVersion() : string
Return values
string

hasItem()

Determine if the item exist in cache

public hasItem(string $id) : bool
Parameters
$id : string
Return values
bool

incrementItem()

Atomically increment a counter in cache, creating it at $initial if it doesn't exist

public incrementItem(string $id[, int $amount = 1 ][, int $initial = 0 ][, int|null $ttl = null ]) : int

Stored as a raw scalar via a Lua script (see INCREMENT_SCRIPT), bypassing the start/ttl/value envelope used by saveItem()/getItem() entirely — a counter key and a saveItem()-managed key are two incompatible storage formats on this adapter, and a counter is not readable via getItem(). $ttl is honored only when the counter is first created; a later call does not refresh an existing counter's expiry.

Parameters
$id : string
$amount : int = 1
$initial : int = 0
$ttl : int|null = null
Tags
throws
Exception
Return values
int

redis()

Get the redis object.

public redis() : Redis
Return values
Redis

saveItem()

Save an item to cache

public saveItem(string $id, mixed $value[, int|null $ttl = null ]) : Redis
Parameters
$id : string
$value : mixed
$ttl : int|null = null
Return values
Redis

evalIncrement()

Shared implementation for incrementItem()/decrementItem(), executing INCREMENT_SCRIPT atomically

protected evalIncrement(string $id, int $amount, int $initial, int|null $ttl) : int
Parameters
$id : string
$amount : int
$initial : int
$ttl : int|null
Tags
throws
Exception
Return values
int

fetchVersion()

Fetch the raw version value from redis, or false if it isn't set

protected fetchVersion(string $key) : mixed
Parameters
$key : string

isFresh()

Determine if a cache envelope (with 'start'/'ttl' keys) is still within its time-to-live

protected isFresh(array<string|int, mixed> $envelope) : bool
Parameters
$envelope : array<string|int, mixed>
Return values
bool

key()

Build the versioned, namespaced storage key for an item id

protected key(string $id) : string
Parameters
$id : string
Return values
string

resolveVersion()

Resolve the current version for this namespace, defaulting to 1

protected resolveVersion() : int
Return values
int

versionKey()

Get the storage key for this namespace's version counter

protected versionKey() : string
Return values
string

        
On this page

Search results