randomKey
Description
Returns a random key.
返回一个随机的KEY。
Parameters
None.
Return value
STRING: an existing key in redis.
STRING:一个存在于REDIS中的KEY
Example
$key = $redis->randomKey();$surprise = $redis->get($key); // who knows what's in there.
select
Description
Switches to a given database.
选择数据库
Parameters
INTEGER: dbindex, the database number to switch to.
整数值:数据库索引,数据库的ID
Return value
TRUE
in case of success, FALSE
in case of failure.
Example
(See following function)
move
Description
Moves a key to a different database.
移动一个KEY-VALUE到另一个DB
Parameters
Key: key, the key to move.
key:要移动的key
INTEGER: dbindex, the database number to move the key to.
整数值:要移动到的数据库ID
Return value
BOOL: TRUE
in case of success, FALSE
in case of failure.
Example
$redis->select(0); // switch to DB 0$redis->set('x', '42'); // write 42 to x$redis->move('x', 1); // move to DB 1$redis->select(1); // switch to DB 1$redis->get('x'); // will return 42
rename, renameKey
Description
Renames a key.
重命名一个KEY
Parameters
STRING: srckey, the key to rename.
STRING:源KEY,需要改名的KEY
STRING: dstkey, the new name for the key.
STRING:目标KEY,KEY的新名字
Return value
BOOL: TRUE
in case of success, FALSE
in case of failure.
Example
$redis->set('x', '42');$redis->rename('x', 'y');$redis->get('y'); // → 42$redis->get('x'); // → `FALSE`
renameNx
Description
Same as rename, but will not replace a key if the destination already exists. This is the same behaviour as setNx.
看起来功能和rename是一样的,但是renameNx不是KEY改名的功能,而是复制一个KEY的VALUE到一个新的KEY。是复制出一个新的KEY-VALUE,而VALUE则是srcKEY的VALUE,而不是PHP中的引用概念。
Example
$redis->set('x', '42');$redis->renameNx('x', 'y');$redis->get('y'); // → 42$redis->get('x'); // → 42
$redis->set('x','39');
$redis->get('x'); //->39
$redis->get('y'); //->42
setTimeout, expire, pexpire
Description
Sets an expiration date (a timeout) on an item. pexpire requires a TTL in milliseconds.
给KEY设置一个生存周期,pexpire使用毫秒作为计算单位
Parameters
Key: key. The key that will disappear.
Key:将被销毁的KEY
Integer: ttl. The key's remaining Time To Live, in seconds.
integer:生命周期
Return value
BOOL: TRUE
in case of success, FALSE
in case of failure.
Example
$redis->set('x', '42');$redis->setTimeout('x', 3); // x will disappear in 3 seconds.sleep(5); // wait 5 seconds$redis->get('x'); // will return `FALSE`, as 'x' has expired.
expireAt, pexpireAt
Description
Sets an expiration date (a timestamp) on an item. pexpireAt requires a timestamp in milliseconds.
给一个KEY设置一个生命周期,单位是UNIX的时间戳。
Parameters
Key: key. The key that will disappear.
Key:将被销毁的key
Integer: Unix timestamp. The key's date of death, in seconds from Epoch time.
integer:Unix 时间戳
Return value
BOOL: TRUE
in case of success, FALSE
in case of failure.
Example
$redis->set('x', '42');$now = time(NULL); // current timestamp$redis->expireAt('x', $now + 3); // x will disappear in 3 seconds.sleep(5); // wait 5 seconds$redis->get('x'); // will return `FALSE`, as 'x' has expired.
keys, getKeys
Description
Returns the keys that match a certain pattern.
返回某种计算模式取得的KEYS。
Description
Parameters
STRING: pattern, using '*' as a wildcard.
STRING:计算符
Return value
Array of STRING: The keys that match a certain pattern.
Example
$allKeys = $redis->keys('*'); // all keys will match this.$keyWithUserPrefix = $redis->keys('user*');
dbSize
Description
Returns the current database's size.
返回当前DB的KEY的数量
Parameters
None.
Return value
INTEGER: DB size, in number of keys.
Example
$count = $redis->dbSize();echo "Redis has $count keys\n";
auth
Description
Authenticate the connection using a password. Warning: The password is sent in plain-text over the network.
使用PASSWORD验证链接。警告:PASSWD以明码的形式在网络中传输。Parameters
STRING: password
Return value
BOOL: TRUE
if the connection is authenticated, FALSE
otherwise.
Example
$redis->auth('foobared');
bgrewriteaof
Description
Starts the background rewrite of AOF (Append-Only File)
使用aof来进行数据库持久化。
Parameters
None.
Return value
BOOL: TRUE
in case of success, FALSE
in case of failure.
Example
$redis->bgrewriteaof();
slaveof
Description
Changes the slave status
选择从服务器
Parameters
Either host (string) and port (int), or no parameter to stop being a slave.
Return value
BOOL: TRUE
in case of success, FALSE
in case of failure.
Example
$redis->slaveof('10.0.1.7', 6379);/* ... */$redis->slaveof();
object
Description
Describes the object pointed to by a key.
声明一个对象,并指向KEY。
Parameters
The information to retrieve (string) and the key (string). Info can be one of the following:
- "encoding"
- "refcount"
- "idletime"
Return value
STRING for "encoding", LONG for "refcount" and "idletime", FALSE
if the key doesn't exist.
Example
$redis->object("encoding", "l"); // → ziplist$redis->object("refcount", "l"); // → 1$redis->object("idletime", "l"); // → 400 (in seconds, with a precision of 10 seconds).
save
Description
Performs a synchronous save.
同步执行写入到磁盘。
Parameters
None.
Return value
BOOL: TRUE
in case of success, FALSE
in case of failure. If a save is already running, this command will fail and return FALSE
.
Example
$redis->save();
bgsave
Description
Performs a background save.
异步保存到磁盘。
Parameters
None.
Return value
BOOL: TRUE
in case of success, FALSE
in case of failure. If a save is already running, this command will fail and return FALSE
.
Example
$redis->bgSave();
lastSave
Description
Returns the timestamp of the last disk save.
返回最后一次数据磁盘持久化的时间戳。
Parameters
None.
Return value
INT: timestamp.
Example
$redis->lastSave();
type
Description
Returns the type of data pointed by a given key.
返回KEY所指向的VALUE的数据类型。
Parameters
Key: key
Return value
Depending on the type of the data pointed by the key, this method will return the following value:
string: Redis::REDIS_STRING set: Redis::REDIS_SET list: Redis::REDIS_LIST zset: Redis::REDIS_ZSET hash: Redis::REDIS_HASH other: Redis::REDIS_NOT_FOUNDExample
$redis->type('key');
append
Description
Append specified string to the string stored in specified key.
添加指定的字符串到指定的字符串KEY。
Parameters
Key Value
Return value
INTEGER: Size of the value after the append
返回添加后KEY的SIZE
Example
$redis->set('key', 'value1');$redis->append('key', 'value2'); /* 12 */$redis->get('key'); /* 'value1value2' */
getRange (substr also supported but deprecated in redis)
Description
Return a substring of a larger string
返回字符串的一部分
Parameters
key start end
Return value
STRING: the substring
Example
$redis->set('key', 'string value');$redis->getRange('key', 0, 5); /* 'string' */$redis->getRange('key', -5, -1); /* 'value' */
setRange
Description
Changes a substring of a larger string.
修改字符串的一部分。
Parameters
key
offsetvalueReturn value
STRING: the length of the string after it was modified.
Example
$redis->set('key', 'Hello world');$redis->setRange('key', 6, "redis"); /* returns 11 */$redis->get('key'); /* "Hello redis" */
strlen
Description
Get the length of a string value.
返回字符串的长度。
Parameters
key
Return value
INTEGER
Example
$redis->set('key', 'value');$redis->strlen('key'); /* 5 */
getBit
Description
Return a single bit out of a larger string
Parameters
key
offsetReturn value
LONG: the bit value (0 or 1)
Example
$redis->set('key', "\x7f"); // this is 0111 1111$redis->getBit('key', 0); /* 0 */$redis->getBit('key', 1); /* 1 */
setBit
Description
Changes a single bit of a string.
Parameters
key
offsetvalue: bool or int (1 or 0)Return value
LONG: 0 or 1, the value of the bit before it was set.
Example
$redis->set('key', "*"); // ord("*") = 42 = 0x2f = "0010 1010"$redis->setBit('key', 5, 1); /* returns 0 */$redis->setBit('key', 7, 1); /* returns 0 */$redis->get('key'); /* chr(0x2f) = "/" = b("0010 1111") */
bitop
Description
Bitwise operation on multiple keys.
Parameters
operation: either "AND", "OR", "NOT", "XOR"
ret_key: return keykey1key2...Return value
LONG: The size of the string stored in the destination key.
bitcount
Description
Count bits in a string.
Parameters
key
Return value
LONG: The number of bits set to 1 in the value behind the input key.
flushDB
Description
Removes all entries from the current database.
强制刷新当前DB的内存数。
Parameters
None.
Return value
BOOL: Always TRUE
.
Example
$redis->flushDB();
flushAll
Description
Removes all entries from all databases.
强制刷新所有的DB。
Parameters
None.
Return value
BOOL: Always TRUE
.
Example
$redis->flushAll();
sort
Description
筛选
Parameters
Key: key Options: array(key => value, ...) - optional, with the following keys and values:
'by' => 'some_pattern_*', 'limit' => array(0, 1), 'get' => 'some_other_pattern_*' or an array of patterns, 'sort' => 'asc' or 'desc', 'alpha' => TRUE, 'store' => 'external-key'
Return value
An array of values, or a number corresponding to the number of elements stored if that was used.
Example
$redis->delete('s');$redis->sadd('s', 5);$redis->sadd('s', 4);$redis->sadd('s', 2);$redis->sadd('s', 1);$redis->sadd('s', 3);var_dump($redis->sort('s')); // 1,2,3,4,5var_dump($redis->sort('s', array('sort' => 'desc'))); // 5,4,3,2,1var_dump($redis->sort('s', array('sort' => 'desc', 'store' => 'out'))); // (int)5
info
Description
Returns an associative array from REDIS that provides information about the server. Passing no arguments to INFO will call the standard REDIS INFO command, which returns information such as the following:
返回redis的相关系统信息,类似于PHP的PHPINFO();可以选择参数,按照选择的参数返回相应的信息,也可以不写参数,返回所有的信息。
- redis_version
- arch_bits
- uptime_in_seconds
- uptime_in_days
- connected_clients
- connected_slaves
- used_memory
- changes_since_last_save
- bgsave_in_progress
- last_save_time
- total_connections_received
- total_commands_processed
- role
You can pass a variety of options to INFO (per the Redis documentation), which will modify what is returned.
Parameters
option: The option to provide redis (e.g. "COMMANDSTATS", "CPU")
Example
$redis->info(); /* standard redis INFO command */$redis->info("COMMANDSTATS"); /* Information on the commands that have been run (>=2.6 only)$redis->info("CPU"); /* just CPU information from Redis INFO */
resetStat
Description
Resets the statistics reported by Redis using the INFO command (info()
function).
使用info()重置静态的日志。
These are the counters that are reset:
- Keyspace hits
- Keyspace misses
- Number of commands processed
- Number of connections received
- Number of expired keys
Parameters
None.
Return value
BOOL: TRUE
in case of success, FALSE
in case of failure.
Example
$redis->resetStat();
ttl, pttl
Description
Returns the time to live left for a given key, in seconds. If the key doesn't exist, FALSE
is returned. pttl returns a time in milliseconds.
取得一个KEY已经存在的时间。如果这个KEY不存在,则返回FLASE。PTTL使用毫秒为单位。
Parameters
Key: key
Return value
Long, the time left to live in seconds.
Example
$redis->ttl('key');
persist
Description
Remove the expiration timer from a key.
删除一个KEY的生命周期设置。
Parameters
Key: key
Return value
BOOL: TRUE
if a timeout was removed, FALSE
if the key didn’t exist or didn’t have an expiration timer.
如果确实有生命周期并且成功移除,则返回TRUE。如果KEY不存在,或者KEY没有生命周期则返回FLASE。
Example
$redis->persist('key');
mset, msetnx
Description
Sets multiple key-value pairs in one atomic command. MSETNX only returns TRUE if all the keys were set (see SETNX).
批量设置多个KEY-VALUE。如果所有的KEYS都被设置成功,如果这些KEY-VALUE都SET成功,使用msetnx将仅仅返回一个TURE,而如果有一个是已经存在的KEY,则所有的操作都不被执行。
Parameters
Pairs: array(key => value, ...)
Return value
Bool TRUE
in case of success, FALSE
in case of failure.
Example
$redis->mset(array('key0' => 'value0', 'key1' => 'value1'));var_dump($redis->get('key0'));var_dump($redis->get('key1'));
Output:
string(6) "value0"string(6) "value1"
rpoplpush (redis >= 1.1)
Description
Pops a value from the tail of a list, and pushes it to the front of another list. Also return this value.
从源LIST的最后弹出一个元素,并且把这个元素从目标LIST的顶部(左侧)压入目标LIST。
Parameters
Key: srckey
Key: dstkeyReturn value
STRING The element that was moved in case of success, FALSE
in case of failure.
Example
$redis->delete('x', 'y');$redis->lPush('x', 'abc');$redis->lPush('x', 'def');$redis->lPush('y', '123');$redis->lPush('y', '456');// move the last of x to the front of y.var_dump($redis->rpoplpush('x', 'y'));var_dump($redis->lRange('x', 0, -1));var_dump($redis->lRange('y', 0, -1));
Output:
string(3) "abc"array(1) { [0]=> string(3) "def"}array(3) { [0]=> string(3) "abc" [1]=> string(3) "456" [2]=> string(3) "123"}
brpoplpush
Description
A blocking version of rpoplpush
, with an integral timeout in the third parameter.
rpoplpush的阻塞版本,这个版本有第三个参数用于设置阻塞时间,即如果源LIST为空,那么可以阻塞监听timeout的时间,如果有元素了则执行操作。
Parameters
Key: srckey
Key: dstkeyLong: timeoutReturn value
STRING The element that was moved in case of success, FALSE
in case of timeout.