Redis Basic Commands
Mac run redis
redis-server
redis-cli
Redis Datatypes
- String
- List
- Hashes
- Sets
- Sorted Sets (zset)
Keys
setex
命令為指定的key 設置值及其過期時間.
如果key 已經存在, SETEX 命令將會替換舊的值.
單位是秒.
SETEX KEY_NAME TIMEOUT VALUE
local:0>setex key 10 "kimi"
"OK"
local:0>ttl key
"8"
local:0>ttl key
"7"
local:0>ttl key
"6"
local:0>pttl key
"5265"
local:0>ttl key
"4"
local:0>ttl key
"3"
local:0>get key
"kimi"
local:0>ttl key
"-2"
local:0>get key
null
Pattern Matching with keys
Return all keys matching pattern
h?llomatch hello, hallo and hxlloh*llomatch hllo and heeeeelloh[ae]llomatch hello and hallo, but not hilloh[^e]llomatch hallo, hbllo ..., but not helloh[a-b]llomatch hallo, hbllo
127.0.0.1:6379> set hello 1
OK
127.0.0.1:6379> set hallo 2
OK
127.0.0.1:6379> set hbllo 3
OK
127.0.0.1:6379> set hxllo 4
OK
127.0.0.1:6379> set haaallo 5
OK
127.0.0.1:6379> keys *
1) "hello"
2) "hallo"
3) "hxllo"
4) "hbllo"
5) "haaallo"
127.0.0.1:6379> keys h?llo
1) "hello"
2) "hallo"
3) "hxllo"
4) "hbllo"
Shutdown Command
shutdown [NOSAVE|SAVE]
shutdown NOSAVE
Dump & Restore in Redis
Dump
DUMP key
時間複雜度:查找給定鍵的複雜度為O(1). http://redisdoc.com/internal/dump.html
Example:
```
##### Restore
`RESTORE key ttl serialized-value [REPLACE]`
http://redisdoc.com/internal/restore.html
## Cheet Sheet
Set Key value
Get Key
DEL key1 key2 key3 (To delete a key).
EXISTS key1 key2 (To check a key exist or not).
TTL key (To check time to live).
EXPIRE key 10(in seconds).
PTTL mykey (to check time in millisecond).
PEXPIRE mykey 1500 (Time in Milliseconds).
PERSIST mykey (Remove EXPIRATION from the key)
KEYS a?? (Returns all keys matching pattern)
Supported glob-style patterns: • h?llo matches hello, hallo and hxllo • h*llo matches hllo and heeeello • h[a]llo matches hello and hallo, but not hillo • h["e]llo matches hallo, hbllo, ... but not hello • h[a-b]1lo matches hallo and hbllo ```
RANDOMKEY (Return a random key from the currently selected database)
RENAME mykey myotherkey
RENAMENX mykey myotherkey (Renames key to newkey if newkey does not yet exist)
TOUCH key1 key2 (Alters the last access time of a key(s).
UNLINK key1 key2 key3 (The actual removal will happen later asynchronously.)
TYPE key1 (Return Type of Value)
DUMP mykey (Serialize the value stored at the key in a Redis-specific format)
RESTORE mykey 0 "\n\x17\x17\x00\x00\x00\x12\x00\x00\x00\x03\x00\”