Smart.Framework Logo

final class \SmartRedisDb
{ } ->

Class: SmartRedisDb - provides a Client for Redis MemDB Server.
By default this class will just log the errors.
Tested and Stable on Redis versions: 2.6.x / 2.8.x / 3.0.x / 3.2.x / 4.x / 5.x


class Methods

public function __construct ( STRING $y_description = "DEFAULT", BOOLEAN $y_fatal_err = false, STRING $host = "", INTEGER $port = "", INTEGER+ $db = "", STRING $password = "", INTEGER+ $timeout = 5, FLOAT $y_debug_exch_slowtime = 0.0005 ) {} @ 
@param: {STRING} $y_description: *OPTIONAL* Default is 'DEFAULT' ; The description of the Redis connection to make easy debug and log errors
@param: {BOOLEAN} $y_fatal_err: *OPTIONAL* Default is FALSE ; If Errors are Fatal or Not ... ; Set this parameter to TRUE if you want to Raise a fatal error on Redis errors ; otherwise default is FALSE and will ignore Redis errors but just log them as warnings (this is the wanted behaviour on a production server ...)
@param: {STRING} $host: *OPTIONAL* The Redis Server Host (Ex: '127.0.0.1')
@param: {INTEGER} $port: *OPTIONAL* The Redis Server Port (Ex: 6379)
@param: {INTEGER+} $db: *OPTIONAL* The Redis Server DB Number (Ex: 0) ; By Default min: 0 and max: 15 (16 databases) ; if redis.conf specify more than 16, can be a larger number, min: 0 ; max 255
@param: {STRING} $password: *OPTIONAL* The Redis Auth Password ; Default is Empty String
@param: {INTEGER+} $timeout: *OPTIONAL* The connection TimeOut in seconds
@param: {FLOAT} $y_debug_exch_slowtime: *OPTIONAL* The Debug Slow Time in microseconds to Record slow Queries
Object Constructor
If no value is provided for Host, Port and DbNum it will use the values from the config[redis]
Sample code: PHP
<?php // Sample Redis configuration of Default Instance (must be set in etc/config.php) $configs['redis']['server-host'] = '127.0.0.1'; // redis host $configs['redis']['server-port'] = 6379; // redis port $configs['redis']['dbnum'] = 8; // redis db number 0..15 $configs['redis']['password'] = ''; // redis Base64-Encoded password ; by default is empty $configs['redis']['timeout'] = 5; // redis connect timeout in seconds $configs['redis']['slowtime'] = 0.0005; // redis slow query time (for debugging) 0.0010 .. 0.0001 // #end php code
public function __call ( ) {} @ 
This is the Magic Method (Call) that maps the PHP class extra methods to Redis methods.
It have variadic parameters mapped to Redis sub-calls.
The most common Redis methods are documented here. For the rest of the Redis methods see: https://redis.io/commands
There are some methods that are not available in this driver because they need to use more than one database and this is not supported (this driver binds to only one database at a time). Example: MOVE (but MOVE can be emulated with an example by using two Redis instances that will GET the key from DB#1/connection#1 SET the key in DB#2/connection#2 and if successful UNSET the key from DB#1/connection#1)
NOTICE: Not all Redis methods were ported to this driver but the most essential methods are available.

public function ping ( ) {} -> STRING
@magic __call()
@return {STRING}
Ping the Redis server ; returns: the test answer which is always PONG
public function watch ( STRING $keys ) {} -> STRING
@magic __call()
@return {STRING}
@param {STRING} $keys
Must be used before starting the transaction (MULTI) ; Marks the given key(s) to be watched for conditional execution of a transaction ; A single key can be passed as 'key1'; If multiple keys have to be passed they have to be separed by comma as: 'key1, key2' ; This method always return: OK
public function unwatch ( ) {} -> STRING
@magic __call()
@return {STRING}
Can be used after ending the transaction (EXEC) ; Flushes all the previously watched keys for a transaction ; If EXEC or DISCARD, there's not mandatory to manually call UNWATCH ; This method always return: OK
public function multi ( ) {} -> STRING
@magic __call()
@return {STRING}
Marks the start of a transaction block. Subsequent commands will be queued for atomic execution using EXEC ; This method always return: OK ; NOTICE: all subsequent commands when used in a transaction block will return: QUEUED (instead of OK, 1, ... or other responses)
public function exec ( ) {} -> ARRAY
@magic __call()
@return {ARRAY}
Executes all previously queued commands in a transaction started by MULTI ; When using WATCH before MULTI, EXEC will execute commands only if the watched keys were not modified, allowing for a check-and-set mechanism ; if using DISCARD all the statements before DISCARD will be discarded and will not be executed ; When using WATCH, EXEC can return a Null reply if the execution was aborted ; If not a Null reply the returned array looks like [ 0 => OK, 1 => OK, ..., 2 => 1, ..., 5 => OK ] (non-associative array), each entry represent the result of each statement after EXEC
public function discard ( ) {} -> STRING
@magic __call()
@return {STRING}
Flushes all previously queued commands in a transaction ; If required, must be called withing transaction (after MULTI and before EXEC) ; This method always return: OK
public function exists ( STRING $key ) {} -> INT
@magic __call()
@return {INT}
@param {STRING} $key
Determine if a key exists ; returns 1 if the key exists or 0 if does not exists
public function get ( STRING $key ) {} -> MIXED
@magic __call()
@return {MIXED}
@param {STRING} $key
Get a Redis Key ; returns: the value of key as STRING or NULL if not exists
public function set ( STRING $key, STRING $value ) {} -> MIXED
@magic __call()
@return {MIXED}
@param {STRING} $key
@param {STRING} $value
Set a Redis Key ; returns: OK on success or NULL on failure
public function append ( STRING $key, STRING $value ) {} -> MIXED
@magic __call()
@return {MIXED}
@param {STRING} $key
@param {STRING} $value
Append a Redis Key ; returns: OK on success or NULL on failure
public function incr ( STRING $key ) {} -> MIXED
@magic __call()
@return {MIXED}
@param {STRING} $key
Increments a key that have an integer value with 1 ; max is 64-bit int ; if the value is non integer returns error ; returns the value after increment
public function incrby ( STRING $key, INTEGER $value ) {} -> MIXED
@magic __call()
@return {MIXED}
@param {STRING} $key
@param {INTEGER} $value
Increments a key that have an integer value with the given int value ; max is 64-bit int ; if the value is non integer returns error ; returns the value after increment
public function decr ( STRING $key ) {} -> MIXED
@magic __call()
@return {MIXED}
@param {STRING} $key
Decrements a key that have an integer value with 1 ; min is 64-bit int ; if the value is non integer returns error ; returns the value after decrement
public function decrby ( STRING $key, INTEGER $value ) {} -> MIXED
@magic __call()
@return {MIXED}
@param {STRING} $key
@param {INTEGER} $value
Decrements a key that have an integer value with the given int value ; min is 64-bit int ; if the value is non integer returns error ; returns the value after decrement
public function rename ( STRING $key, STRING $newkey ) {} -> STRING
@magic __call()
@return {STRING}
@param {STRING} $key
@param {STRING} $newkey
Renames key to newkey. It returns an error when key does not exist or OK. If newkey already exists it is overwritten
public function del ( STRING $key ) {} -> INT
@magic __call()
@return {INT}
@param {STRING} $key
Delete a Redis Key ; returns: 0 if not successful or 1 if successful
public function ttl ( STRING $key ) {} -> INT
@magic __call()
@return {INT}
@param {STRING} $key
Get the TTL in seconds for a key ; -1 if the key does not expire ; -2 if the key does not exists
public function expire ( STRING $key, INT $expireinseconds ) {} -> INT
@magic __call()
@return {INT}
@param {STRING} $key
@param {INT} $expireinseconds
Set the Expiration time for a Redis Key in seconds ; returns: 0 if not successful or 1 if successful
public function expireat ( STRING $key, INT $expirationtime ) {} -> INT
@magic __call()
@return {INT}
@param {STRING} $key
@param {INT} $expirationtime
Set the Expiration time for a Redis Key at unixtimestamp ; returns: 0 if not successful or 1 if successful
public function persist ( STRING $key ) {} -> INT
@magic __call()
@return {INT}
@param {STRING} $key
Remove the existing expiration timeout on a key ; returns: 0 if not successful or 1 if successful
public function type ( STRING $key ) {} -> STRING
@magic __call()
@return {STRING}
@param {STRING} $key
Returns the string representation of the type of the value stored at key (string, list, set, zset, hash and stream)
public function strlen ( STRING $key ) {} -> INT
@magic __call()
@return {INT}
@param {STRING} $key
Returns the length of the string value stored at key. An error is returned when key holds a non-string value
public function keys ( STRING $pattern ) {} -> MIXED
@magic __call()
@return {MIXED}
@param {STRING} $pattern
Get all keys matching a pattern ; return array of all keys matching a pattern or null if no key
public function randomkey ( ) {} -> MIXED
@magic __call()
@return {MIXED}
Return a random key from the currently selected database ; if no key exist, returns NULL
public function info ( ) {} -> STRING
@magic __call()
@return {STRING}
Returns information and statistics about the server
public function dbsize ( ) {} -> INT
@magic __call()
@return {INT}
Return the number of keys in the currently-selected database
public function flushdb ( ) {} -> STRING
@magic __call()
@return {STRING}
Remove all keys from the selected database ; This command never fails ; Returns OK

class Properties


class Constants


Sample code: PHP

<?php

// Redis Client usage example, with custom connection parameters:
 
$redis = new SmartRedisDb('SampleInstance'false'localhost''6379'3); // connects at the database no. 3 ; for debug and log will identify by 'SampleInstance' (1st param) ; 2nd param set the fatal errors to false and just log errors not raise fatal errors ... (this is default behaviour)
 
$redis->set('key1''value1');
 
$redis->set('list1:key1''value2');
 
$value1 $redis->get('key1');
 if(
$redis->exists('key1')) {
     
$value2 $redis->get('list5:key7');
 } 
//end if

// #end php code


documentation generated on: 2023-10-19 23:15:46 +0000


Smart.Framework © 2009-2023 unix-world.org