Home | 簡體中文 | 繁體中文 | 雜文 | 打賞(Donations) | ITEYE 博客 | OSChina 博客 | Facebook | Linkedin | 知乎專欄 | Search | Email

47.3. redis-cli - Command-line client to redis-server

47.3.1. 命令參數

			
-a <password>      Password to use when connecting to the server.
[root@netkiller conf.d]# redis-cli -a hsM8NK8b71vFQKFOS55jbWJrA1TYgI4e

			
			

47.3.2. --latency Enter a special mode continuously sampling latency.

參數的功能是從客戶端發出一條命令到客戶端接受到該命令的反饋所用的最長響應時間

# redis-cli --latency -h 192.168.2.1 
min: 1, max: 210, avg: 3.64 (13453 samples)
			

47.3.3. auth

認證密碼

[root@netkiller ~]# redis-cli 
127.0.0.1:6379> keys *
(error) NOAUTH Authentication required.
127.0.0.1:6379> auth hsM8NKb71vjbWJrA1TYgI4
OK
127.0.0.1:6379> keys *
 1) "HK50(1605)"
 2) "GBPUSD"
 3) "USDCHF"
 4) "SP500(1609)"
 5) "NZDJPY"
 6) "AUDNZD"
 7) "EURGBP"
 8) "CLN6"
 9) "BU6"
			

47.3.4. MONITOR

$ redis-cli monitor			
			

47.3.5. info

			
redis 127.0.0.1:6379> info
redis_version:2.4.10
redis_git_sha1:00000000
redis_git_dirty:0
arch_bits:64
multiplexing_api:epoll
gcc_version:4.4.6
process_id:29663
uptime_in_seconds:1189
uptime_in_days:0
lru_clock:1018411
used_cpu_sys:0.10
used_cpu_user:0.09
used_cpu_sys_children:0.01
used_cpu_user_children:0.00
connected_clients:1
connected_slaves:0
client_longest_output_list:0
client_biggest_input_buf:0
blocked_clients:0
used_memory:730664
used_memory_human:713.54K
used_memory_rss:7225344
used_memory_peak:730720
used_memory_peak_human:713.59K
mem_fragmentation_ratio:9.89
mem_allocator:jemalloc-2.2.5
loading:0
aof_enabled:0
changes_since_last_save:0
bgsave_in_progress:0
last_save_time:1373332622
bgrewriteaof_in_progress:0
total_connections_received:4
total_commands_processed:14
expired_keys:0
evicted_keys:0
keyspace_hits:3
keyspace_misses:0
pubsub_channels:0
pubsub_patterns:0
latest_fork_usec:744
vm_enabled:0
role:master
db0:keys=4,expires=0
redis 127.0.0.1:6379>
			
			

47.3.6. save/bgsave/lastsave

save/bgsave 保存持久化將數據,lastsave 查看相關信息

			
redis 127.0.0.1:6379> save
OK
redis 127.0.0.1:6379> bgsave
Background saving started
redis 127.0.0.1:6379> lastsave
(integer) 1373335757
			
			

47.3.7. keys

查詢所有key

			
172.18.52.15:6379> keys *
 1) "www.example.com:743f10d0f1dc569ed5893856e14c1fb7captcha"
 2) "www.example.com:d88e0b6c54a235763dd731bcc0914439captcha"
 3) "www.example.com:17f9091cb44f3cc5bb411eb801f07be8member_login"
 4) "www.example.com:10ff594fd42f4c81212020555cfb586amember_login_input"
 5) "www.example.com:a759ba5232ce324d0e6ae8da9290beaecaptcha"
 6) "www.example.com:37c78410af02d66a542d15b9707f215bcaptcha"
 7) "www.example.com:9f5070e217f4eac9a1d15f9b8dbe7148deposit_1_temp_var"
 8) "www.example.com:6c1a13c9396df2c35613043923bfe338captcha"
 9) "www.example.com:b611080c0627154871ea0e1498793238captcha"
10) "www.example.com:2792241f8d0f075528db2b50e0c9c684member_login"
			
			

查詢指定key

			
172.18.50.15:6379> set name neo
OK
172.18.50.15:6379> keys name
1) "name"			
				
			

47.3.8. set/get/del

			
172.18.52.15:6379> set name neo
OK
172.18.52.15:6379> get name 
"neo"
172.18.52.15:6379> keys name
1) "name"
172.18.52.15:6379> del name
(integer) 1
172.18.52.15:6379> get name
(nil)			
			
			

47.3.9. setnx

SETNX key value

當 key 不存在時將 key 的值設為 value,若給定的 key 已經存在,則 SETNX 不做任何動作。SETNX 是(SET if Not eXists) (如果不存在,則 SET)的簡寫。

			
返回值:
	設置成功,返回 1
	設置失敗,返回 0

redis> EXISTS neo             # neo 不存在
(integer) 0

redis> SETNX neo "chen"    	  # neo 設置成功
(integer) 1

redis> SETNX neo "netkiller"  # 嘗試覆蓋 neo ,失敗
(integer) 0

redis> GET neo                # 沒有被覆蓋
"chen"	
			
			

47.3.10. expire/ttl

EXPIRE 設置過期時間, TTL 可以查詢過期時間倒計時。

			
172.18.52.165:6379> set name neo
OK
172.18.52.165:6379> ttl name
(integer) -1
172.18.52.165:6379> expire name 30
(integer) 1
172.18.52.165:6379> ttl name
(integer) 22
172.18.52.165:6379> ttl name
(integer) 9
172.18.52.165:6379> ttl name
(integer) -1
172.18.52.165:6379> get  name
(nil)
			
			

注意ttl返回-1有兩種情況,一是沒有設置過期時間,另一種是該key已經過期不存在。

47.3.11. Pub/Sub 訂閲與發佈

redis 提供基本的MQ 功能,下面我們做一個演示

開啟第一個終端窗口,訂閲first second

$ redis-cli
redis 127.0.0.1:6379> SUBSCRIBE first second
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "first"
3) (integer) 1
1) "subscribe"
2) "second"
3) (integer) 2
			

開啟第二個終端窗口,分別發送first second

$ redis-cli
redis 127.0.0.1:6379>  PUBLISH second Hello
(integer) 1
redis 127.0.0.1:6379>  PUBLISH first Helloworld!!!
(integer) 1
redis 127.0.0.1:6379> quit
			

現在切換到第一個終端窗口,應該能夠看到發送過來的字元串

$ redis-cli
redis 127.0.0.1:6379> SUBSCRIBE first second
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "first"
3) (integer) 1
1) "subscribe"
2) "second"
3) (integer) 2

1) "message"
2) "second"
3) "Hello"

1) "message"
2) "first"
3) "Helloworld!!!"