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>