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!!!"