在平時的工作中,需要根據需求對Redis資料庫進行一些操作。
可以參考Redis官網http://redis.io/commands 進行詳細了解
1.SELECT 切換資料庫
redis 127.0.0.1:6379[1]> HELP SELECT
SELECT index
summary: Change the selected database for the current connection
since: 1.0.0
group: connection
redis 127.0.0.1:6379[1]> SELECT 2
OK
2.LLEN 得到一個清單的長度
redis 127.0.0.1:6379[2]> HELP LLEN
LLEN key
summary: Get the length of a list
since: 1.0.0
group: list
redis 127.0.0.1:6379[2]> LLEN bi
(integer) 412
3.LRANGE 擷取一個清單的所有元素
LRANGE 索引是以0開始的,0表示第一個元素,-1表示最後一個元素
redis 127.0.0.1:6379[2]> HELP LRANGE
LRANGE key start stop
summary: Get a range of elements from a list
since: 1.0.0
group: list
redis 127.0.0.1:6379[2]> LRANGE bi 0 5
4.LPUSH 将一個或多個值添加到一個清單的開頭
redis 127.0.0.1:6379[2]> HELP LPUSH
LPUSH key value [value ...]
summary: Prepend one or multiple values to a list
since: 1.0.0
group: list
redis 127.0.0.1:6379[2]> LPUSH bi http://abc.com/logUserLogin?event_id=25&uid=de721bcef5cba1fc182d18
5.RPUSH 将一個或多個值追加到一個清單的末尾
redis 127.0.0.1:6379[2]> HELP RPUSH
RPUSH key value [value ...]
summary: Append one or multiple values to a list
since: 1.0.0
group: list
redis 127.0.0.1:6379[2]> RPUSH bi http://abc.com/logUserLogin?event_id=25&uid=de721bcef5cba1fc182d18
6.SAVE 同步資料到磁盤
SAVE指令執行的時候會阻塞連接配接,是以生成環境最好使用BGSAVE指令
redis 127.0.0.1:6379[2]> HELP SAVE
SAVE -
summary: Synchronously save the dataset to disk
since: 1.0.0
group: server
redis 127.0.0.1:6379[2]> SAVE
OK
(1.33s)
7.BGSAVE 異步資料到磁盤
使用BGSAVE,Redis将會在背景執行儲存資料的操作,不影響正常的用戶端連接配接,Redis将會fork出一個子程序用于儲存資料,父程序繼續處理用戶端請求。
redis 127.0.0.1:6379[2]> HELP BGSAVE
BGSAVE -
summary: Asynchronously save the dataset to disk
since: 1.0.0
group: server
redis 127.0.0.1:6379[2]> BGSAVE
Background saving started
8.TYPE 判斷一個KEY的類型
redis 127.0.0.1:6379[2]> HELP TYPE
TYPE key
summary: Determine the type stored at key
since: 1.0.0
group: generic
redis 127.0.0.1:6379[2]> TYPE bi
list
9.BGREWRITEAOF
異步重寫AOF檔案,Redis将會建立一個對目前AOF檔案優化過的AOF版本。
redis 127.0.0.1:6379> help BGREWRITEAOF
BGREWRITEAOF -
summary: Asynchronously rewrite the append-only file
since: 1.0.0
group: server
10.CONFIG GET
擷取某個配置項的值
redis 127.0.0.1:6379> help config get
CONFIG GET parameter
summary: Get the value of a configuration parameter
since: 2.0.0
group: server
redis 127.0.0.1:6379> config get maxmemory
1) "maxmemory"
2) "0"
11.CONFIG SET
設定某個參數的值
redis 127.0.0.1:6379> help config set
CONFIG SET parameter value
summary: Set a configuration parameter to the given value
since: 2.0.0
group: server
redis 127.0.0.1:6379> config set maxmemory 200000000
OK
12.DBSIZE
傳回目前資料庫的KEY值得數量
redis 127.0.0.1:6379[3]> HELP DBSIZE
DBSIZE -
summary: Return the number of keys in the selected database
since: 1.0.0
group: server
redis 127.0.0.1:6379[3]> dbsize
(integer) 12502
13.DEL
删除一個KEY值
redis 127.0.0.1:6379> help del
DEL key [key ...]
summary: Delete a key
since: 1.0.0
group: generic
redis 127.0.0.1:6379> del foo
(integer) 1
14.EXISTS
檢查一個KEY是否存在
redis 127.0.0.1:6379> help exists
EXISTS key
summary: Determine if a key exists
since: 1.0.0
group: generic
redis 127.0.0.1:6379> exists foo
(integer) 1
15.SET 指令
設定一個KEY的值
redis 127.0.0.1:6379> help set
SET key value
summary: Set the string value of a key
since: 1.0.0
group: string
redis 127.0.0.1:6379> set foo test
OK
redis 127.0.0.1:6379>
16.PERSIST
删除一個KEY的過期時間
edis 127.0.0.1:6379> help persist
PERSIST key
summary: Remove the expiration from a key
since: 2.2.0
group: generic
17.RENAME
重新命名一個KEY
redis 127.0.0.1:6379> help rename
RENAME key newkey
summary: Rename a key
since: 1.0.0
group: generic
redis 127.0.0.1:6379> rename foo footest
OK
redis 127.0.0.1:6379>
18.EXPIRE
為一個KEY設定一個TTL過期時間
redis 127.0.0.1:6379> help expire
EXPIRE key seconds
summary: Set a key's time to live in seconds
since: 1.0.0
group: generic
redis 127.0.0.1:6379> expire footest 300
(integer) 1
19.TTL
擷取過期時間
redis 127.0.0.1:6379> help ttl
TTL key
summary: Get the time to live for a key
since: 1.0.0
group: generic
redis 127.0.0.1:6379> ttl footest
(integer) 289
redis 127.0.0.1:6379> ttl footest
(integer) 285
redis 127.0.0.1:6379> ttl footest
(integer) 283
redis 127.0.0.1:6379> ttl footest
(integer) 282
redis 127.0.0.1:6379> ttl footest
(integer) 282
redis 127.0.0.1:6379>
20.EXPIREAT
設定一個KEY的過期時間,以UNIX時間戳表示
redis 127.0.0.1:6379> help expireat
EXPIREAT key timestamp
summary: Set the expiration for a key as a UNIX timestamp
since: 1.2.0
group: generic
redis 127.0.0.1:6379> expireat foo 1431532800
(integer) 1
redis 127.0.0.1:6379> ttl foo
(integer) 3210141
21.GET
擷取一個KEY的值
redis 127.0.0.1:6379> help get
GET key
summary: Get the value of a key
since: 1.0.0
group: string
redis 127.0.0.1:6379> get foo
"test"
22.HGET
擷取一個哈希字段的值
redis 127.0.0.1:6379> help hget
HGET key field
summary: Get the value of a hash field
since: 2.0.0
group: hash
redis 127.0.0.1:6379> hset myhash field1 "foo"
(integer) 1
redis 127.0.0.1:6379> hget myhash field1
"foo"
redis 127.0.0.1:6379> hget myhash field2
(nil)
redis 127.0.0.1:6379>
23.LASTSAVE
上次成功儲存資料到磁盤的UNIX時間戳
redis 127.0.0.1:6379> help lastsave
LASTSAVE -
summary: Get the UNIX time stamp of the last successful save to disk
since: 1.0.0
group: server
redis 127.0.0.1:6379> lastsave
(integer) 1428373205
redis 127.0.0.1:6379>
24.LPUSH
redis 127.0.0.1:6379> help lpush
LPUSH key value [value ...]
summary: Prepend one or multiple values to a list
since: 1.0.0
group: list
redis 127.0.0.1:6379> lpush mylist a b c
(integer) 6
redis 127.0.0.1:6379> LRANGE mylist 0 -1
1) "c"
2) "b"
3) "a"
4) "c"
5) "b"
6) "a"
redis 127.0.0.1:6379> llen mylist
(integer) 6