Redis的字符串就是一个由字节组成的序列,它们和不少编程语言里面的字符串没有什么明显redis
的不一样,跟C或者C++风格的字符数组也相去不远。在Redis里面,字符串能够存储如下3中类型编程
的值。数组
1)字节串(byte string)less
2)整数dom
3)浮点数编程语言
用户能够经过给定一个任意的数值,对存储着整数或者浮点数的字符串执行自增ui
(increment)或者自减(decrement)操做,在有须要的时候,Redis还会将整数转换成this
浮点数。整数的取值范围和系统的长整数(long integer)的取值范围相同(在32位系统上,spa
整数就是32位有符号整数,在64位系统上,整数就是64位有符号整数),而浮点数的取值范围code
和精度则与IEEE 754 标准的双精度浮点数(double)相同。Redis明确地区分字节串、整数和
浮点数的作法是一种优点,比起只可以存储字节串的作法,Redis的作法在数据表现方面具备更
大的灵活性。
本节将对Redis里面最简单的结构--字符串进行讨论,介绍基本的数值自增和自减操做,
以及二进制位(bit)和子串(substring)处理命令。
当用户将一个值存储到Redis字符串里面的时候,若是这个值能够被解释(interpret)为十进
制整数或者浮点数,那么Redis会察觉到这一点,并容许用户对这个字符串执行各类INCR*和
DECR*操做。若是用户对一个不存在的键或者一个保存了空串的键执行自增或者自减操做,那么
Redis在执行操做时会将这个键的值看成是0来处理。若是用户尝试对一个值没法被解释为整数或
者浮点数的字符串键执行自增或者自减操做,那么Redis将向用户返回一个错误
除了自增和自减操做以外,Redis还拥有对字节串的其中一部份内容进行读取或者写入
的操做(这些操做也能够用于整数或者浮点数,但这种用法并不常见)
GetRange命令是由之前的substr命令更名而来的
在使用SETRANGE或者SETBIT命令对字符串进行写入的时候,若是字符串当前的长度不能
知足写入的要求,那么Redis会自动地使用空字节(null)来将字符串扩展至所需的长度,而后
才执行写入或者更新操做。在使用getrange读取字符串的时候,超过字符串末尾的数据会被视为
是空串,而在使用getbit读取二进制位串的时候,超过字符串末尾的二进制位会被视为是0.
SET 设置存储在给定键中的值
官方文档说明 https://redis.io/commands/set
Available since 1.0.0.
Time complexity: O(1)
Set key to hold the string value. If key already holds a value, it is overwritten,
regardless of its type. Any previous time to live associated with the key is discarded on successful SET operation.
Options
Starting with Redis 2.6.12 SET supports a set of options that modify its behavior:
•EX seconds -- Set the specified expire time, in seconds.
•PX milliseconds -- Set the specified expire time, in milliseconds.
•NX -- Only set the key if it does not already exist.
•XX -- Only set the key if it already exist.
Note: Since the SET command options can replace SETNX, SETEX, PSETEX,
it is possible that in future versions of Redis these three commands will be deprecated and finally removed.
Return value
Simple string reply: OK if SET was executed correctly.
Null reply: a Null Bulk Reply is returned if the SET operation was not performed because the user specified the NX or XX option
but the condition was not met.
Examples
redis> SET mykey "Hello" "OK"
redis> GET mykey "Hello"
redis>
Patterns
Note: The following pattern is discouraged in favor of the Redlock algorithm which is only a bit more complex to implement,
but offers better guarantees and is fault tolerant.
The command SET resource-name anystring NX EX max-lock-time is a simple way to implement a locking system with Redis.
A client can acquire the lock if the above command returns OK (or retry after some time if the command returns Nil),
and remove the lock just using DEL.
The lock will be auto-released after the expire time is reached.
It is possible to make this system more robust modifying the unlock schema as follows:
•Instead of setting a fixed string, set a non-guessable large random string, called token.
•Instead of releasing the lock with DEL, send a script that only removes the key if the value matches.
This avoids that a client will try to release the lock after the expire time deleting the key created by another client that acquired the lock later.
An example of unlock script would be similar to the following:
if redis.call("get",KEYS[1]) == ARGV[1]
then
return redis.call("del",KEYS[1])
else
return 0
end
The script should be called with EVAL ...script... 1 resource-name token-value
Get 获取存储在给定键中的值
官方说明 https://redis.io/commands/get
Available since 1.0.0.
Time complexity: O(1)
Get the value of key. If the key does not exist the special value nil is returned.
An error is returned if the value stored at key is not a string, because GET only handles string values.
Return value
Bulk string reply: the value of key, or nil when key does not exist.
*Examples
redis> GET nonexisting
(nil)
redis> SET mykey "Hello"
"OK"
redis> GET mykey
"Hello"
redis>
DEL 删除存储在给定键中的值(这个命令能够用于全部类型)
官方说明 https://redis.io/commands/del
Available since 1.0.0.
Time complexity: O(N) where N is the number of keys that will be removed. When a key to remove holds a value other than a string, the individual complexity for this key is O(M) where M is the number of elements in the list, set, sorted set or hash. Removing a single key that holds a string value is O(1).
Removes the specified keys. A key is ignored if it does not exist.
Return value
Integer reply: The number of keys that were removed.
*Examples
redis> SET key1 "Hello"
"OK"
redis> SET key2 "World"
"OK"
redis> DEL key1 key2 key3
(integer) 2
redis>