Documentation

Rdb
in package
uses RdbHelperTrait

AbstractYes

Rdb is a wrapper around other popular redis libraries.

It tries to create a unified interface for different adapters (php-redis, predis, credis).

It also contains some useful helpers:

  • prefixing
  • object serialisation
  • scanning generators
  • locking
  • rate limiting

Feel free to add to it.

Table of Contents

Constants

ADAPTERS  : mixed = [\karmabunny\rdb\RdbConfig::TYPE_PHP_REDIS => \...
CAST_AUTO  : mixed = 'auto'
CAST_FLOAT  : mixed = 'float'
CAST_INTEGER  : mixed = 'integer'

Properties

$config  : RdbConfig
$driver  : RdbObjectDriver|null

Methods

append()  : int
Append a value to a key.
blPop()  : array<string|int, string>|null
Remove (and return) an item from the start of a list. If there are no items then this method will block until an item is added.
brPop()  : array<string|int, string>|null
Remove (and return) an item from the end of a list. If there are no items then this method will block until an item is added.
brPoplPush()  : string|null
Remove (and return) an item from the end of a list. If there are no items then this method will block until an item is added.
create()  : Rdb
Create an Rdb client for the given config.
decr()  : int|float|null
Decrement a value by X.
decrBy()  : int|null
Decrement a value by X (int).
del()  : int
Delete a key, or a list of keys.
dump()  : string|null
eval()  : int|string|bool|array<string|int, mixed>|null
Execute a Lua script.
exists()  : int
Do these keys exist?
expire()  : bool
Set the expiry/TLL for a key.
expireAt()  : bool
Set the expiry in unix time for a key.
export()  : int
Export to this file.
flushAll()  : void
flushDb()  : void
flushPrefix()  : void
get()  : string|null
Get a value. Null if missing.
getBucket()  : RdbBucket
Create or fetch a leaky bucket for rate limiting.
getHash()  : array<string|int, mixed>|null
Get an array from a hash.
getJson()  : mixed
Get a JSON document from this key.
getObject()  : object|null
Get an object at this key.
getRange()  : string|null
Get a substring of a value. Null if missing.
hDel()  : int|null
Delete one or more hash fields.
hExists()  : bool|null
Check if a hash field exists.
hGet()  : string|null
Get the value of a hash field.
hGetAll()  : array<string|int, mixed>|null
Get all fields and values in a hash.
hIncr()  : int|float|null
Increment a hash field value by X.
hIncrBy()  : int|null
Increment a hash field by a number (integer).
hIncrByFloat()  : float|null
Increment a hash field by a number (integer).
hKeys()  : array<string|int, mixed>|null
Get all field names in a hash.
hLen()  : int|null
Get the number of fields in a hash.
hmGet()  : array<string|int, mixed>|null
Get the values of multiple hash fields.
hmSet()  : bool|null
Set multiple hash fields to multiple values.
hScan()  : Generator<string, string>
Incrementally iterate over hash fields and values.
hSet()  : bool|null
Set the value of a hash field.
hStrLen()  : int|null
Get the string length of a hash field's value.
hVals()  : array<string|int, mixed>|null
Get all values in a hash.
import()  : array<string|int, string>
Import from this file.
incr()  : int|float|null
Increment a value by X.
incrBy()  : int|null
Increment a value by X (integer).
incrByFloat()  : float|null
Increment a value by X (float).
inspectObject()  : null|string
Inspect the object stored in the key.
keys()  : array<string|int, string>
Get a list of keys that match a pattern.
lIndex()  : string|null
Get an item from a list at this index.
lLen()  : int|null
Get the length of a list.
lock()  : RdbLock|null
Create a lock.
lPop()  : string|null
Remove (and return) an item from the start of a list.
lPush()  : int|null
Add items to the start of a list.
lRange()  : array<string|int, string>|null
Retrieve items from a list.
lRem()  : int|null
Remove an item from a list.
lSet()  : bool|null
Store a item at this index of a list.
lTrim()  : bool|null
Remove items from a list.
match()  : bool
Does this key match the pattern?
mGet()  : array<string|int, string|null>
Get many items.
mGetObjects()  : array<string|int, object|null>
Bulk fetch objects via a list of keys.
move()  : bool
Move a key from the active database to another database.
mScan()  : Generator<string|int, string|null>
Bulk fetch via a list of keys.
mScanObjects()  : Generator<string|int, object|null>
Bulk fetch objects via a list of keys.
mSet()  : bool
Store many items.
mSetObjects()  : array<string|int, int>
Bulk set an array of object.
pack()  : int
Store a value using the MessagePack format.
prefix()  : Generator<string|int, string>
Apply a prefix to all items.
registerSessionHandler()  : bool
Install a session handler.
rename()  : bool
Rename a key.
restore()  : bool
rPop()  : string|null
Remove (and return) an item from the end of a list.
rPoplPush()  : string|null
Remove (and return) an item from the end of a list.
rPush()  : int|null
Add items to the end of a list.
sAdd()  : int|null
Add a value (or many) to a set.
scan()  : Generator<string|int, string>
Iterate over a set of pattern to find a list of keys.
sCard()  : int|null
Get the cardinality (size) of a set.
select()  : bool
Change the active database.
set()  : bool|string|null
Store a value at a key.
setHash()  : int
Store an array in a hash.
setJson()  : int
Store a JSON document in a key.
setObject()  : int
Store an object at this key.
sIsMember()  : bool|null
Test if an item is a member of a set.
sMembers()  : array<string|int, string>|null
Get values of a set.
sMove()  : bool|null
Move an item from one set to another.
sRem()  : int|null
Remove an item (or items) from a set.
sScan()  : Generator<string|int, string>
Scan values of a set.
substr()  : null|string
A wrapper around getRange because it's spooky.
ttl()  : float|int
Get the TTL for a key.
type()  : string|null
Get the type of a key.
unpack()  : mixed
Get a value from the MessagePack format.
zAdd()  : int|null
Add items to a sorted set.
zCard()  : int|null
Get the number of members in a sorted set.
zCount()  : int|null
Get the number of members within a range of scores within a sorted set.
zIncrBy()  : float|null
Increment a member's score in a sorted set.
zRange()  : null|array<string|int, mixed>
Get members from a sorted set.
zRank()  : int|null
Get the rank of a member in a sorted set.
zRem()  : int|null
Remove members from a sorted set.
zRevRank()  : int|null
Get the reversed rank of a member in a sorted set.
zScore()  : float|null
Get the score of a member in a sorted set.
__construct()  : mixed
Build the config.
cast()  : int|float
Convert string/int/float to int/float with a preference mode thing.
explodeFlatKeys()  : array<string|int, mixed>
Convert a flattened array into a nested form.
flatten()  : array<int, T>
Flatten an array input.
flattenKeys()  : array<string|int, mixed>
Flatten the keys of an array with a 'glue'.
getObjectDriver()  : RdbObjectDriver
Get the current object driver.
parseRangeFlags()  : array<string|int, mixed>
Parse + normalise zrange() flags.
parseRestoreFlags()  : array<string|int, mixed>
parseSetFlags()  : array<string|int, mixed>
Parse + normalise set() flags.
parseTtl()  : array{0: bool, 1: int}
stripPrefix()  : string
Strip the prefix from list of keys.

Constants

ADAPTERS

public mixed ADAPTERS = [\karmabunny\rdb\RdbConfig::TYPE_PHP_REDIS => \karmabunny\rdb\PhpRedisAdapter::class, \karmabunny\rdb\RdbConfig::TYPE_PREDIS => \karmabunny\rdb\PredisAdapter::class, \karmabunny\rdb\RdbConfig::TYPE_CREDIS => \karmabunny\rdb\CredisAdapter::class]

CAST_AUTO

public mixed CAST_AUTO = 'auto'

CAST_FLOAT

public mixed CAST_FLOAT = 'float'

CAST_INTEGER

public mixed CAST_INTEGER = 'integer'

Properties

Methods

append()

Append a value to a key.

public abstract append(string $key, string $value) : int
Parameters
$key : string
$value : string
Return values
int —

string length after append

blPop()

Remove (and return) an item from the start of a list. If there are no items then this method will block until an item is added.

public abstract blPop(array<string|int, string>|string $keys[, int|float|null $timeout = null ]) : array<string|int, string>|null

aka: BLOCKING LET POP

Parameters
$keys : array<string|int, string>|string
$timeout : int|float|null = null

in seconds - if unset, the config/connection timeout

Return values
array<string|int, string>|null —

[key, item] or null if the timeout occurs

brPop()

Remove (and return) an item from the end of a list. If there are no items then this method will block until an item is added.

public abstract brPop(array<string|int, string>|string $keys[, int|float|null $timeout = null ]) : array<string|int, string>|null

aka: BLOCKING RIGHT POP

Parameters
$keys : array<string|int, string>|string
$timeout : int|float|null = null

in seconds - if unset, the config/connection timeout

Return values
array<string|int, string>|null —

[key, item] or null if the timeout occurs

brPoplPush()

Remove (and return) an item from the end of a list. If there are no items then this method will block until an item is added.

public abstract brPoplPush(string $src, string $dst[, int|float|null $timeout = null ]) : string|null

aka: BLOCKING RIGHT POP -> LEFT PUSH

Note, although this is deprecated in redis 6.2 it will indefinitely be supported here, if removed, with a polyfill via BLMOVE.

Parameters
$src : string
$dst : string
$timeout : int|float|null = null

in seconds - if unset, the config/connection timeout

Return values
string|null —

item being moved or null if the timeout occurs

create()

Create an Rdb client for the given config.

public static create(RdbConfig|array<string|int, mixed> $config) : Rdb

This will build a client with the appropriate adapter - as defined by the config. If not specified, the default adapter is TYPE_PREDIS.

Parameters
$config : RdbConfig|array<string|int, mixed>
Tags
throws
InvalidArgumentException
Return values
Rdb

decr()

Decrement a value by X.

public decr(string $key[, int|float $amount = 1 ][, string $cast = self::CAST_AUTO ]) : int|float|null

This will use decrBy for integers and incrByFloat (negative value) for floats.

Parameters
$key : string
$amount : int|float = 1
$cast : string = self::CAST_AUTO

one of: 'auto', 'float', 'integer'

Return values
int|float|null —

the value after decrementing

decrBy()

Decrement a value by X (int).

public abstract decrBy(string $key, int $amount) : int|null
Parameters
$key : string
$amount : int
Return values
int|null —

the value after decrementing

del()

Delete a key, or a list of keys.

public abstract del(string|iterable<string|int, string> ...$keys) : int
Parameters
$keys : string|iterable<string|int, string>
Return values
int —

number of keys deleted

dump()

public abstract dump(string $key) : string|null
Parameters
$key : string
Return values
string|null

eval()

Execute a Lua script.

public abstract eval(string $script[, array<int, string> $keys = [] ][, array<int, string|int|float> $args = [] ]) : int|string|bool|array<string|int, mixed>|null
Parameters
$script : string
$keys : array<int, string> = []
$args : array<int, string|int|float> = []
Return values
int|string|bool|array<string|int, mixed>|null

exists()

Do these keys exist?

public abstract exists(string|iterable<string|int, string> ...$keys) : int
Parameters
$keys : string|iterable<string|int, string>
Return values
int —

number of matches

expire()

Set the expiry/TLL for a key.

public abstract expire(string $key[, int|float $ttl = 0 ]) : bool
Parameters
$key : string
$ttl : int|float = 0

seconds

Return values
bool

expireAt()

Set the expiry in unix time for a key.

public abstract expireAt(string $key[, int|float $ttl = 0 ]) : bool
Parameters
$key : string
$ttl : int|float = 0

seconds

Return values
bool

export()

Export to this file.

public export(string|resource $file[, array<string|int, mixed>|string $config = [] ]) : int
  • pattern (include pattern, default: '*')
  • excludes (array of patterns)
  • compressed (default true)
  • log (callback)
Parameters
$file : string|resource
$config : array<string|int, mixed>|string = []
Tags
throws
Exception
Exception

fatal errors

Return values
int —

number of items exported

flushAll()

public abstract flushAll([bool $async = false ]) : void
Parameters
$async : bool = false

flushDb()

public abstract flushDb([bool $async = false ]) : void
Parameters
$async : bool = false

flushPrefix()

public flushPrefix([bool $scan = true ]) : void
Parameters
$scan : bool = true

get()

Get a value. Null if missing.

public abstract get(string $key) : string|null
Parameters
$key : string
Return values
string|null

getBucket()

Create or fetch a leaky bucket for rate limiting.

public getBucket(array<string|int, mixed>|string $config) : RdbBucket
Parameters
$config : array<string|int, mixed>|string
Return values
RdbBucket

getHash()

Get an array from a hash.

public getHash(string $key) : array<string|int, mixed>|null

This supports nested arrays using flattened keys in a dot notation.

Parameters
$key : string
Return values
array<string|int, mixed>|null

getJson()

Get a JSON document from this key.

public getJson(string $key[, bool $throw = true ]) : mixed
Parameters
$key : string
$throw : bool = true

throw an exception if the JSON is invalid

Tags
throws
JsonException

getObject()

Get an object at this key.

public getObject(string $key, string $expected) : object|null

This returns null if the key is empty or the object doesn't match the 'expected' type.

Parameters
$key : string
$expected : string
Tags
throws
InvalidArgumentException
Return values
object|null

getRange()

Get a substring of a value. Null if missing.

public abstract getRange(string $key[, int $from = 0 ][, int $to = -1 ]) : string|null

Note, the second parameter is not a 'length' like PHP's substr and is an inclusive index.

Parameters
$key : string
$from : int = 0
$to : int = -1
Return values
string|null

hDel()

Delete one or more hash fields.

public abstract hDel(string $key, string ...$fields) : int|null
Parameters
$key : string
$fields : string
Return values
int|null —

Number of fields removed

hExists()

Check if a hash field exists.

public abstract hExists(string $key, string $field) : bool|null
Parameters
$key : string
$field : string
Return values
bool|null —

True if field exists

hGet()

Get the value of a hash field.

public abstract hGet(string $key, string $field) : string|null
Parameters
$key : string
$field : string
Return values
string|null —

value or null

hGetAll()

Get all fields and values in a hash.

public abstract hGetAll(string $key) : array<string|int, mixed>|null
Parameters
$key : string
Return values
array<string|int, mixed>|null —

[ field => value ]

hIncr()

Increment a hash field value by X.

public hIncr(string $key, string $field[, int|float $amount = 1 ][, string $cast = self::CAST_AUTO ]) : int|float|null

This is a wrapper around hIncrBy and hIncrByFloat and will use either depending on the given type. This behaviour can be forced either way with the 'cast' param.

Parameters
$key : string
$field : string
$amount : int|float = 1
$cast : string = self::CAST_AUTO

one of: 'auto', 'float', 'integer'

Return values
int|float|null —

the value after incrementing

hIncrBy()

Increment a hash field by a number (integer).

public abstract hIncrBy(string $key, string $field, int $amount) : int|null
Parameters
$key : string
$field : string
$amount : int

Amount to increment by

Return values
int|null —

New value after increment

hIncrByFloat()

Increment a hash field by a number (integer).

public abstract hIncrByFloat(string $key, string $field, float $amount) : float|null
Parameters
$key : string
$field : string
$amount : float

Amount to increment by

Return values
float|null —

New value after increment

hKeys()

Get all field names in a hash.

public abstract hKeys(string $key) : array<string|int, mixed>|null
Parameters
$key : string
Return values
array<string|int, mixed>|null —

Array of field names, or null if the key is not a hash

hLen()

Get the number of fields in a hash.

public abstract hLen(string $key) : int|null
Parameters
$key : string
Return values
int|null —

Number of fields

hmGet()

Get the values of multiple hash fields.

public abstract hmGet(string $key, string ...$fields) : array<string|int, mixed>|null
Parameters
$key : string
$fields : string
Return values
array<string|int, mixed>|null —

Array of values, or null if the key is not a hash

hmSet()

Set multiple hash fields to multiple values.

public abstract hmSet(string $key, array<string|int, mixed> $fields) : bool|null
Parameters
$key : string
$fields : array<string|int, mixed>

Array of field-value pairs

Return values
bool|null —

True if successful

hScan()

Incrementally iterate over hash fields and values.

public abstract hScan(string $key[, string $pattern = '*' ]) : Generator<string, string>
Parameters
$key : string
$pattern : string = '*'

Pattern to match field names

Return values
Generator<string, string> —

Generator yielding field-value pairs

hSet()

Set the value of a hash field.

public abstract hSet(string $key, string $field, mixed $value[, bool $replace = true ]) : bool|null
Parameters
$key : string
$field : string
$value : mixed
$replace : bool = true

use hSetNx if false

Return values
bool|null

hStrLen()

Get the string length of a hash field's value.

public abstract hStrLen(string $key, string $field) : int|null
Parameters
$key : string
$field : string
Return values
int|null —

Length of the value string, or null if field does not exist

hVals()

Get all values in a hash.

public abstract hVals(string $key) : array<string|int, mixed>|null
Parameters
$key : string
Return values
array<string|int, mixed>|null —

values of the hash (not keyed), or null if the key is not a hash

import()

Import from this file.

public import(string|resource $file[, array<string|int, mixed>|string $config = [] ]) : array<string|int, string>
  • pattern (include pattern, default: '*')
  • excludes (array of patterns)
  • log (callback)
Parameters
$file : string|resource
$config : array<string|int, mixed>|string = []
Tags
throws
Exception

fatal errors

Return values
array<string|int, string> —

errors

incr()

Increment a value by X.

public incr(string $key[, int|float $amount = 1 ][, string $cast = self::CAST_AUTO ]) : int|float|null

This is a wrapper around incrBy and incrByFloat and will use either depending on the given type. This behaviour can be force either way with the 'cast' param.

Parameters
$key : string
$amount : int|float = 1
$cast : string = self::CAST_AUTO

one of: 'auto', 'float', 'integer'

Return values
int|float|null —

the value after incrementing

incrBy()

Increment a value by X (integer).

public abstract incrBy(string $key, int $amount) : int|null
Parameters
$key : string
$amount : int
Return values
int|null —

the value after incrementing

incrByFloat()

Increment a value by X (float).

public abstract incrByFloat(string $key, float $amount) : float|null
Parameters
$key : string
$amount : float
Return values
float|null —

the value after incrementing

inspectObject()

Inspect the object stored in the key.

public inspectObject(string $key) : null|string
Parameters
$key : string
Return values
null|string —

class name

keys()

Get a list of keys that match a pattern.

public abstract keys(string $pattern) : array<string|int, string>

You should be using the scan() method.

Parameters
$pattern : string
Return values
array<string|int, string>

lIndex()

Get an item from a list at this index.

public abstract lIndex(string $key, int $index) : string|null

aka: LIST INDEX

Note, index can be negative - it behaves circularly.

Parameters
$key : string
$index : int
Return values
string|null —

the item or null if out-of-range

lLen()

Get the length of a list.

public abstract lLen(string $key) : int|null

aka: LIST LENGTH

Parameters
$key : string
Return values
int|null —

list length or null if not a list.

lock()

Create a lock.

public lock(string $key[, int|float $wait = 0 ][, int|float $ttl = 60 ]) : RdbLock|null

This will block for $wait seconds until the lock is released. It will then return a new lock if available. If the resource is still locked it returns null.

Given a $wait of zero (default), this will return immediately if the resource is not available.

The $ttl will auto-expire a lock should it somehow not self-destruct. If your code runs longer than 1 minute, it's recommended to bump this up.

Parameters
$key : string
$wait : int|float = 0

seconds

$ttl : int|float = 60

seconds (default 1 minute)

Return values
RdbLock|null

lPop()

Remove (and return) an item from the start of a list.

public abstract lPop(string $key) : string|null
  • aka: LEFT POP
  • aka: shift()
Parameters
$key : string
Return values
string|null —

the item or null if empty or not a list

lPush()

Add items to the start of a list.

public abstract lPush(string $key, string ...$items) : int|null
  • aka: LEFT PUSH
  • aka: unshift()
Parameters
$key : string
$items : string
Return values
int|null —

the list length after after pushing

lRange()

Retrieve items from a list.

public abstract lRange(string $key[, int $start = 0 ][, int $stop = -1 ]) : array<string|int, string>|null

aka: LIST RANGE

Negative indexes are circular and wrap around to the end of the list. E.g. [1,2,3] => -1 is 3

Out of range indexes are not errors. A too-large index is treated as and 'end of list' index, aka -1.

Parameters
$key : string
$start : int = 0
$stop : int = -1
Return values
array<string|int, string>|null —

range items or null if not a list.

lRem()

Remove an item from a list.

public abstract lRem(string $key, string $item[, int $count = 0 ]) : int|null

aka: LIST REMOVE

  • count > 0: Remove elements equal to element moving from head to tail.
  • count < 0: Remove elements equal to element moving from tail to head.
  • count = 0: Remove all elements equal to element.
Parameters
$key : string
$item : string
$count : int = 0
Return values
int|null —

number of removed items, or null if not a list

lSet()

Store a item at this index of a list.

public abstract lSet(string $key, int $index, string $item) : bool|null

aka: LIST SET

Parameters
$key : string
$index : int
$item : string
Return values
bool|null —

true if set, false if out of range, null if not a list

lTrim()

Remove items from a list.

public abstract lTrim(string $key[, int $start = 0 ][, int $stop = -1 ]) : bool|null

aka: LIST TRIM

Negative indexes are circular and wrap around to the end of the list. E.g. [1,2,3] => -1 is 3

Out of range indexes are not errors. A too-large index is treated as and 'end of list' index, aka -1.

Parameters
$key : string
$start : int = 0
$stop : int = -1
Return values
bool|null

match()

Does this key match the pattern?

public static match(string $pattern, string $key) : bool
Parameters
$pattern : string
$key : string
Return values
bool

mGet()

Get many items.

public abstract mGet(iterable<string|int, string> $keys) : array<string|int, string|null>

The number of returns will always match number of keys.

Parameters
$keys : iterable<string|int, string>
Return values
array<string|int, string|null> —

Missing keys are null

mGetObjects()

Bulk fetch objects via a list of keys.

public mGetObjects(iterable<string|int, string> $keys, string $expected[, bool $nullish = false ]) : array<string|int, object|null>

Empty keys are filtered out - if nullish is false (default).

Parameters
$keys : iterable<string|int, string>

Non-prefixed keys

$expected : string
$nullish : bool = false

(false) return empty values

Tags
throws
InvalidArgumentException
Return values
array<string|int, object|null> —

[ key => item ]

move()

Move a key from the active database to another database.

public abstract move(string $key, int $database) : bool
Parameters
$key : string
$database : int
Return values
bool

mScan()

Bulk fetch via a list of keys.

public mScan(iterable<string|int, string> $keys) : Generator<string|int, string|null>
Parameters
$keys : iterable<string|int, string>
Return values
Generator<string|int, string|null> —

[ key => item ]

mScanObjects()

Bulk fetch objects via a list of keys.

public mScanObjects(iterable<string|int, mixed> $keys, string $expected[, bool $nullish = false ]) : Generator<string|int, object|null>

Empty keys are filtered out - if nullish is false (default).

Parameters
$keys : iterable<string|int, mixed>

Non-prefixed keys

$expected : string

Ensure all results is of this type

$nullish : bool = false

(false) return empty values

Tags
throws
InvalidArgumentException
Return values
Generator<string|int, object|null> —

[ key => item ]

mSet()

Store many items.

public abstract mSet(array<string|int, string> $items) : bool

This will replace existing items.

Parameters
$items : array<string|int, string>

[ key => string ]

Return values
bool —

always true, unless items is empty.

mSetObjects()

Bulk set an array of object.

public mSetObjects(array<string|int, object> $items) : array<string|int, int>
Parameters
$items : array<string|int, object>

[ key => string ]

Return values
array<string|int, int> —

[ key => int ] object sizes in bytes

pack()

Store a value using the MessagePack format.

public pack(string $key, mixed $value[, int|float $ttl = 0 ]) : int
Parameters
$key : string
$value : mixed
$ttl : int|float = 0

seconds

Tags
throws
PackingFailedException
Return values
int

prefix()

Apply a prefix to all items.

public static prefix(string $prefix, iterable<string|int, string> $items) : Generator<string|int, string>
Parameters
$prefix : string
$items : iterable<string|int, string>
Return values
Generator<string|int, string>

registerSessionHandler()

Install a session handler.

public registerSessionHandler([string $prefix = 'session:' ]) : bool
Parameters
$prefix : string = 'session:'
Return values
bool —

true on a successful install

rename()

Rename a key.

public abstract rename(string $src, string $dst) : bool
Parameters
$src : string
$dst : string
Return values
bool

restore()

public abstract restore(string $key, int|float $ttl, string $value[, array<string|int, mixed> $flags = [] ]) : bool
Parameters
$key : string
$ttl : int|float

seconds

$value : string
$flags : array<string|int, mixed> = []
Return values
bool

rPop()

Remove (and return) an item from the end of a list.

public abstract rPop(string $key) : string|null
  • aka: RIGHT POP
  • aka: pop()
Parameters
$key : string
Return values
string|null —

the item or null if empty or not a list

rPoplPush()

Remove (and return) an item from the end of a list.

public abstract rPoplPush(string $src, string $dst) : string|null

aka: BLOCKING RIGHT POP -> LEFT PUSH

Note, although this is deprecated in redis 6.2 it will indefinitely be supported here, if removed, with a polyfill via LMOVE.

Also note, if either key isn't a list it will also return null.

Parameters
$src : string
$dst : string
Return values
string|null —

item being moved or null if src list is empty

rPush()

Add items to the end of a list.

public abstract rPush(string $key, string ...$items) : int|null
  • aka: RIGHT PUSH
  • aka: append()
Parameters
$key : string
$items : string
Return values
int|null —

the list length after after pushing

sAdd()

Add a value (or many) to a set.

public abstract sAdd(string $key, mixed ...$values) : int|null

Sets by nature are unique. Adding a new item will not create a duplicate entry.

Parameters
$key : string
$values : mixed
Return values
int|null —

number of new items added, null if not a set

scan()

Iterate over a set of pattern to find a list of keys.

public abstract scan(string $pattern) : Generator<string|int, string>

Use this over the keys() method.

Parameters
$pattern : string
Return values
Generator<string|int, string>

sCard()

Get the cardinality (size) of a set.

public abstract sCard(string $key) : int|null
Parameters
$key : string
Return values
int|null —

number of items in the set, null if not a set

select()

Change the active database.

public abstract select(int $database) : bool
Parameters
$database : int
Return values
bool

set()

Store a value at a key.

public abstract set(string $key, string $value[, int|float $ttl = 0 ][, array<string|int, mixed> $flags = [] ]) : bool|string|null

Optionally specify a TTL that will cause the key to automatically delete after a period of seconds.

Flags are an array for modifying behaviour, for example:

$rdb->set('key', 'value', 1, [
   'time_at',           // PXAT or EXAT
   'get_set',           // GET
   'replace' => false,  // NX
]);
Parameters
$key : string
$value : string
$ttl : int|float = 0

seconds

$flags : array<string|int, mixed> = []
  • keep_ttl: retain the TTL when replacing a key
  • time_at: the TTL is an expiry unix time in seconds
  • get_set: get the old value before setting the new one
  • replace: bool - only set if it (not) exists (true: XX, false: NX)
Return values
bool|string|null —
  • string|null if the GET flag is set
  • bool for everything else

setHash()

Store an array in a hash.

public setHash(string $key, array<string|int, mixed> $value) : int

This supports nested arrays using flattened keys in a dot notation.

Parameters
$key : string
$value : array<string|int, mixed>
Return values
int —

number of keys set

setJson()

Store a JSON document in a key.

public setJson(string $key, mixed $value[, int|float $ttl = 0 ]) : int
Parameters
$key : string
$value : mixed
$ttl : int|float = 0

seconds

Tags
throws
JsonException
Return values
int

setObject()

Store an object at this key.

public setObject(string $key, object $value[, int|float $ttl = 0 ]) : int
Parameters
$key : string
$value : object
$ttl : int|float = 0

seconds

Return values
int —

object size in bytes

sIsMember()

Test if an item is a member of a set.

public abstract sIsMember(string $key, string $value) : bool|null
Parameters
$key : string
$value : string
Return values
bool|null —

true if a member, null if not a set

sMembers()

Get values of a set.

public abstract sMembers(string $key) : array<string|int, string>|null

All results will be unique.

Parameters
$key : string
Return values
array<string|int, string>|null —

set members, null if not a set

sMove()

Move an item from one set to another.

public abstract sMove(string $src, string $dst, string $value) : bool|null
Parameters
$src : string
$dst : string
$value : string
Return values
bool|null —

true if moved, or false if not a member, null if not a set

sRem()

Remove an item (or items) from a set.

public abstract sRem(string $key, mixed ...$values) : int|null
Parameters
$key : string
$values : mixed
Return values
int|null —

number of items removed, null if not a set

sScan()

Scan values of a set.

public abstract sScan(string $key[, string $pattern = '*' ]) : Generator<string|int, string>

All results will be unique.

Parameters
$key : string
$pattern : string = '*'
Return values
Generator<string|int, string> —

set members

substr()

A wrapper around getRange because it's spooky.

public substr(string $key, int $from[, int $length = -1 ]) : null|string

This behaves the same as PHP's substr().

Parameters
$key : string
$from : int
$length : int = -1
Return values
null|string

ttl()

Get the TTL for a key.

public abstract ttl(string $key[, bool $ms = false ]) : float|int

Special return values:

  • -1 - key exists but has no TTL
  • -2 - key does not exist
Parameters
$key : string
$ms : bool = false

force milliseconds mode

Return values
float|int —

seconds (or milliseconds, given ttl_mode or ms param)

type()

Get the type of a key.

public abstract type(string $key) : string|null
Parameters
$key : string
Return values
string|null —

one of:

  • string
  • list
  • set
  • zset
  • hash
  • null - unknown or missing

unpack()

Get a value from the MessagePack format.

public unpack(string $key) : mixed
Parameters
$key : string
Tags
throws
UnpackingFailedException

zAdd()

Add items to a sorted set.

public abstract zAdd(string $key, array<string|int, float> $members) : int|null

One must indicate a score for each member, the format looks like:

[
  'a' => 5,
  'b' => 1,
  'c' => 10,
]

If the set exists, the member values are updated - these updated members are excluded from the return count. Otherwise this will create a new sorted set.

Parameters
$key : string
$members : array<string|int, float>

[ member => score ]

Return values
int|null —

number of elements added

zCard()

Get the number of members in a sorted set.

public abstract zCard(string $key) : int|null
Parameters
$key : string
Return values
int|null —

number of items

zCount()

Get the number of members within a range of scores within a sorted set.

public abstract zCount(string $key, float $min, float $max) : int|null

This has the same semantics as zRange 'by score'.

Parameters
$key : string
$min : float
$max : float
Return values
int|null —

number of items

zIncrBy()

Increment a member's score in a sorted set.

public abstract zIncrBy(string $key, float $value, string $member) : float|null

This will create a new sorted set if it doesn't exist.

Parameters
$key : string
$value : float

an amount to update by, can be negative

$member : string
Return values
float|null —

the value after updated

zRange()

Get members from a sorted set.

public abstract zRange(string $key[, int|string|null $start = null ][, int|string|null $stop = null ][, array<string|int, mixed> $flags = [] ]) : null|array<string|int, mixed>

This supports all alternate flags:

Mode Flags
-- rev, withscores
byscore rev, limit, withscores
bylex rev, limit

The limit option can be specified as either:

  • numeric: [0, 10]
  • keyed: ['offset' => 0, 'count' => 10]

The default the start/stop range will return all members.

For bylex the start/stop is inclusive by default unless overridden with the [ or ( syntax.

Note, if the set does not exist it will return an empty array. This returns null only if the key is not a sorted set.

Parameters
$key : string
$start : int|string|null = null

defaults:

  • rank: 0
  • byscore: -inf
  • bylex: - (inf)
$stop : int|string|null = null

defaults:

  • rank: -1 (circular, meaning end-of-set)
  • byscore: +inf
  • bylex: + (inf)
$flags : array<string|int, mixed> = []
  • withscores: include the score with each member (not available for bylex)
  • rev: reverse the order
  • limit: limit the results (only for byscore + bylex)
  • byscore: filter by score
  • bylex: filter by lexicographical order
Return values
null|array<string|int, mixed> —

members are either:

  • a numeric list, ordered by their score
  • a keyed array like [ member => score ] (withscores)
  • null if the key is not a sorted set

zRank()

Get the rank of a member in a sorted set.

public abstract zRank(string $key, string $member) : int|null
Parameters
$key : string
$member : string
Return values
int|null —

rank, 0-indexed

zRem()

Remove members from a sorted set.

public abstract zRem(string $key, array<string|int, string>|string ...$members) : int|null
Parameters
$key : string
$members : array<string|int, string>|string
Return values
int|null —

number of removed items

zRevRank()

Get the reversed rank of a member in a sorted set.

public abstract zRevRank(string $key, string $member) : int|null
Parameters
$key : string
$member : string
Return values
int|null —

rank, 0-indexed

zScore()

Get the score of a member in a sorted set.

public abstract zScore(string $key, string $member) : float|null
Parameters
$key : string
$member : string
Return values
float|null —

score

__construct()

Build the config.

protected __construct(RdbConfig|array<string|int, mixed> $config) : mixed

This must be called first in any child constructors.

Parameters
$config : RdbConfig|array<string|int, mixed>
Tags
throws
InvalidArgumentException

cast()

Convert string/int/float to int/float with a preference mode thing.

protected cast(mixed $amount, string $mode) : int|float
Parameters
$amount : mixed
$mode : string
Return values
int|float

explodeFlatKeys()

Convert a flattened array into a nested form.

protected static explodeFlatKeys(iterable<string|int, mixed> $item[, string $glue = '.' ]) : array<string|int, mixed>

For example:

[
    'abc.def' => 123,
    'abc.ghi.0' => 456,
    'abc.ghi.1' => 789,
]

Becomes:

[ 'abc' => [
    'def' => 123,
    'ghi' => [456, 789],
]]
Parameters
$item : iterable<string|int, mixed>
$glue : string = '.'
Return values
array<string|int, mixed>

flatten()

Flatten an array input.

protected static flatten(iterable<string|int, T> $items[, int $depth = 25 ]) : array<int, T>

This also discards keys.

Largely ripped from karmabunny/kb.

Parameters
$items : iterable<string|int, T>
$depth : int = 25
Tags
template
Return values
array<int, T>

flattenKeys()

Flatten the keys of an array with a 'glue'.

protected static flattenKeys(iterable<string|int, mixed> $item[, string $glue = '.' ]) : array<string|int, mixed>

For example:

[ 'abc' => [
    'def' => 123,
    'ghi' => 567,
]]

Becomes:

[
    'abc.def' => 123,
    'abc.ghi' => 567,
]
Parameters
$item : iterable<string|int, mixed>
$glue : string = '.'
Return values
array<string|int, mixed>

parseRangeFlags()

Parse + normalise zrange() flags.

protected static parseRangeFlags(array<string|int, mixed> $flags) : array<string|int, mixed>
Parameters
$flags : array<string|int, mixed>
Return values
array<string|int, mixed>

parseRestoreFlags()

protected static parseRestoreFlags(array<string|int, mixed> $flags) : array<string|int, mixed>
Parameters
$flags : array<string|int, mixed>
Return values
array<string|int, mixed>

parseSetFlags()

Parse + normalise set() flags.

protected static parseSetFlags(array<string|int, mixed> $flags) : array<string|int, mixed>
Parameters
$flags : array<string|int, mixed>
Return values
array<string|int, mixed>

parseTtl()

protected parseTtl(int|float $ttl) : array{0: bool, 1: int}
Parameters
$ttl : int|float
Return values
array{0: bool, 1: int}

stripPrefix()

Strip the prefix from list of keys.

protected stripPrefix(string $key) : string
Parameters
$key : string
Return values
string
On this page

Search results