redis 字符串(string)函数
Posted xiong63
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了redis 字符串(string)函数相关的知识,希望对你有一定的参考价值。
字符串(string)函数
get 命令/方法/函数
Description
Get the value related to the specified key
取得与指定的键值相关联的值
Parameters
key
Return Value
String or Bool: If key didn‘t exist, FALSE is returned. Otherwise, the value related to this key is returned.
返回相关值或者BOOL值,如果KEY不存在,返回FALSE。如果有相关的KEY和值返回值。
Examples
$redis->get(‘key‘);
set 命令/方法/函数
Description
Set the string value in argument as value of the key.
设置值到KEY
Parameters
Key
Value
Timeout (optional). Calling SETEX is preferred if you want a timeout.
Return value
Bool TRUE if the command is successful.
Examples
$redis->set(‘key‘, ‘value‘);
setex 命令/方法/函数
Description
Set the string value in argument as value of the key, with a time to live. PSETEX uses a TTL in milliseconds.
设置一个有生命周期的KEY-VALUE,psetex()使用的周期单位为毫秒。
Parameters
Key TTL Value
Return value
Bool TRUE if the command is successful.
Examples
$redis->setex(‘key‘, 3600, ‘value‘); // sets key → value, with 1h TTL.
psetex 命令/方法/函数
Description
Set the string value in argument as value of the key, with a time to live(milliseconds).
设置一个有生命周期的KEY-VALUE,psetex()使用的周期单位为毫秒。
Return value
Bool TRUE if the command is successful.
Examples
$redis->psetex(‘key‘, 100, ‘value‘); // sets key → value, with 0.1 sec TTL.
setnx 命令/方法/函数
Description
Set the string value in argument as value of the key if the key doesn‘t already exist in the database.
setnx用于设置一个KEY-VALUE,这个函数会先判断Redis中是否有这个KEY,如果没有就SET,有就返回False。
Parameters
key value
Return value
Bool TRUE in case of success, FALSE in case of failure.
Examples
$redis->setnx(‘key‘, ‘value‘); /* return TRUE */
$redis->setnx(‘key‘, ‘value‘); /* return FALSE */
delete 命令/方法/函数
Description
Remove specified keys.
移除已经存在KEYS
Parameters
An array of keys, or an undefined number of parameters, each a key: key1 key2 key3 ... keyN
可以是一个KEYS的数组,或者一个未定义的数字参数,或者一个一个的写KEY
Return value
Long Number of keys deleted.
返回删除KEY-VALUE的数量
Examples
$redis->set(‘key1‘, ‘val1‘);
$redis->set(‘key2‘, ‘val2‘);
$redis->set(‘key3‘, ‘val3‘);
$redis->set(‘key4‘, ‘val4‘);
$redis->delete(‘key1‘, ‘key2‘); /* return 2 */
$redis->delete(array(‘key3‘, ‘key4‘)); /* return 2 */
getSet 命令/方法/函数
Description
Sets a value and returns the previous entry at that key.
设置一个VALUE 并且返回该KEY当前的VALUE。
Parameters
Key: key
STRING: value
Return value
A string, the previous value located at this key.
Example
$redis->set(‘x‘, ‘42‘);
$exValue = $redis->getSet(‘x‘, ‘lol‘); // return ‘42‘, replaces x by ‘lol‘
$newValue = $redis->get(‘x‘)‘ // return ‘lol‘
multi, exec, discard 命令/方法/函数
Description
Enter and exit transactional mode.
进入、退出事务处理模式。
Parameters
(optional) Redis::MULTI or Redis::PIPELINE. Defaults to Redis::MULTI. A Redis::MULTI block of commands runs as a single transaction; a Redis::PIPELINE block is simply transmitted faster to the server, but without any guarantee of atomicity. discardcancels a transaction.
multi函数的参数选项为Redis::MULTI或者是Redis::PIPELINE.默认的是参数是Redis::MULTI。
Redis::MULTI将多个操作当做一个事务来处理。Redis::PIPELINE将作为一个简单快速的处理通道给服务器进行处理,但是不保证处理数据的原子性。
discard()函数取消一个事物处理模式。
Return value
multi() returns the Redis instance and enters multi-mode. Once in multi-mode, all subsequent method calls return the same object until exec() is called.
multi()返回一个Redis实例,并且这个实例进入到了事务处理模式(批量处理)。当进入到事务处理模式,所有的方法调用都将返回相同的Redis实例,一直到exec()被调用执行事务处理。
Example
$ret = $redis->multi()
->set(‘key1‘, ‘val1‘)
->get(‘key1‘)
->set(‘key2‘, ‘val2‘)
->get(‘key2‘)
->exec();
/*
$ret == array(
0 => TRUE,
1 => ‘val1‘,
2 => TRUE,
3 => ‘val2‘);
*/
watch 命令/方法/函数
Description
Watches a key for modifications by another client. If the key is modified between WATCH and EXEC, the MULTI/EXEC transaction will fail (return FALSE).
监控一个KEY是否被其他的客户端修改。如果KEY在调用watch()和exec()之间被修改,那么批量处理最终的exec()执行将失败。通过一些实验,这个函数的效果其实并没有那么好,或者说不能够准确的去监控。
Parameters
keys: a list of keys
Example
$redis->watch(‘x‘);
/* long code here during the execution of which other clients could well modify `x` */
$ret = $redis->multi()
->incr(‘x‘)
->exec();
/*
$ret = FALSE if x has been modified between the call to WATCH and the call to EXEC.
*/
unwatch 命令/方法/函数
Description
unwatch cancels all the watching of all keys by this client.
unwatch()取消对于所有KEY值的监控操作针对于这个Redis实例。通过一些实验,这个函数的效果其实并没有那么好,或者说不能够准确的去监控。
subscribe 命令/方法/函数
Description
Subscribe to channels. Warning: this function will probably change in the future.
方法回调函数,注意:该方法在将来有可能被修改。
Parameters
channels: an array of channels to subscribe to
callback: either a string or an array($instance, ‘method_name‘). The callback function receives 3 parameters: the redis instance, the channel name, and the message.
Example
function f($redis, $chan, $msg) {
switch($chan) {
case ‘chan-1‘:
...
break;
case ‘chan-2‘:
...
break;
case ‘chan-2‘:
...
break;
}
}
$redis->subscribe(array(‘chan-1‘, ‘chan-2‘, ‘chan-3‘), ‘f‘); // subscribe to 3 chans
publish 命令/方法/函数
Description
Publish messages to channels. Warning: this function will probably change in the future.
将消息发布到信道。警告:这个功能可能会在未来发生变化。
Parameters
channel: a channel to publish to
messsage: string
Example
$redis->publish(‘chan-1‘, ‘hello, world!‘); // send message.
exists 命令/方法/函数
Description
Verify if the specified key exists.
验证一个指定的KEY是否存在。
Parameters
key
Return value
BOOL: If the key exists, return TRUE, otherwise return FALSE.
BOOL:如果key存在,返回true,否则返回false。
Examples
$redis->set(‘key‘, ‘value‘);
$redis->exists(‘key‘); /* TRUE */
$redis->exists(‘NonExistingKey‘); /* FALSE */
incr 命令/方法/函数
Description
Increment the number stored at key by one. If the second argument is filled, it will be used as the integer value of the increment.
对指定的KEY的值自增1。如何填写了第二个参数,将把第二个参数自增给KEY的值。
Parameters
key
value: value that will be added to key (only for incrBy)
Return value
INT the new value
返回新的INT数值
Examples
$redis->incr(‘key1‘); /* key1 didn‘t exists, set to 0 before the increment 如果key1不存在,在自增之前的默认值为0 */
/* and now has the value 1 执行incr后,现在为1 */
$redis->incr(‘key1‘); /* 2 */
$redis->incr(‘key1‘); /* 3 */
incrBy 命令/方法/函数
Description
对key的值加num,比如 incrby("ab", 10),相当于:ab+10
Return value
INT the new value
返回新的INT数值
Examples
$redis->incrBy(‘key1‘, 10); /* 如果key1的值为8,那么结果为:18 */
incrByFloat 命令/方法/函数
Increment the key with floating point precision.
自增一个浮点型的数值。
Parameters
key
value: (float) value that will be added to the key
Return value
FLOAT the new value
Examples
$redis->incrByFloat(‘key1‘, 1.5); /* key1 didn‘t exist, so it will now be 1.5 */
$redis->incrByFloat(‘key1‘, 1.5); /* 3 */
$redis->incrByFloat(‘key1‘, -1.5); /* 1.5 */
$redis->incrByFloat(‘key1‘, 2.5); /* 3.5 */
decr 命令/方法/函数
对指定的KEY的值自减1(如果要自减指定的值,可以使用decrBy)
Parameters
key
value: value that will be substracted to key (only for decrBy)
Return value
INT the new value
Examples
$redis->decr(‘key1‘); /* key1 didn‘t exists, set to 0 before the increment */
/* and now has the value -1 */
$redis->decr(‘key1‘); /* -2 */
$redis->decr(‘key1‘); /* -3 */
decrBy 命令/方法/函数
减去指定的值。
Return value
INT the new value
Examples
$redis->set(‘key1‘, 50);
$redis->decrBy(‘key1‘, 10); /* result:40 */
mGet 命令/方法/函数
Description
Get the values of all the specified keys. If one or more keys dont exist, the array will contain FALSE at the position of the key.
取得所有指定KEYS的值,如果一个或者更多的KEYS不存在,那么返回的ARRAY中将在相应的KEYS的位置填充FALSE。
Parameters
Array: Array containing the list of the keys
数组:一个KEYS的数组
Return value
Array: Array containing the values related to keys in argument
数组:返回相应的KEYS的值
Examples
$redis->set(‘key1‘, ‘value1‘);
$redis->set(‘key2‘, ‘value2‘);
$redis->set(‘key3‘, ‘value3‘);
$redis->mGet(array(‘key1‘, ‘key2‘, ‘key3‘)); /* array(‘value1‘, ‘value2‘, ‘value3‘);
$redis->mGet(array(‘key0‘, ‘key1‘, ‘key5‘)); /* array(`FALSE`, ‘value2‘, `FALSE`);
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 命令/方法/函数
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
offset
value
Return 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
offset
Return 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
offset
value: 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.
对一个或多个保存二进制位的字符串 key 进行位元操作,并将结果保存到 destkey 上。
Parameters
operation: either "AND", "OR", "NOT", "XOR"
ret_key: return key
key1
key2...
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.
example
https://blog.csdn.net/ma_jiang/article/details/61421888
mset 命令/方法/函数
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"
以上是关于redis 字符串(string)函数的主要内容,如果未能解决你的问题,请参考以下文章