Redis ใช้ CLI Straightforward 雖然 Simple แต่มี Interesting Features ที่ไม่น่าคาด มาดู Basics และ Features ของ Client กัน
Simple Connection:
cweid@strange:~$ redis-cli -h 127.0.0.1 -p 6379 -a mysupersecretpassword 127.0.0.1:6379> PING PONG
Connected Redis Server Authenticated Super Secret Password
Alternative: Omit -a Auth After Connect:
cweid@strange:~$ redis-cli -h 127.0.0.1 -p 6379 127.0.0.1:6379> AUTH mysupersecretpassword OK 127.0.0.1:6379> PING PONG
Redis Server + Client Same Machine -> Unix Socket
Note: Provide Hostname/Port + Socket -> redis-cli Still Connect Via Unix Socket
cweid@strange:~$ redis-cli -s /tmp/redis.sock 127.0.0.1:6379> AUTH mysupersecretpassword OK 127.0.0.1:6379> PING PONG
Execute Command CLI Only Output Return Stdout:
cweid@strange:~$ redis-cli -h 127.0.0.1 -p 6379 -a mysupersecretpassword PING PONG
Execute Same Command N Times:
cweid@strange:~$ redis-cli -h 127.0.0.1 -p 6379 -a mysupersecretpassword -r 4 PING PONG PONG PONG PONG
-r = Repeat Option Add Delay -i With -r:
cweid@strange:~$ redis-cli -h 127.0.0.1 -p 6379 -a mysupersecretpassword -i 1 -r 4 PING PONG PONG PONG PONG
1 Second Sleep Between PING Subseconds: Float
cweid@strange:~$ redis-cli -h 127.0.0.1 -p 6379 -a mysupersecretpassword -i 0.1 -r 4 PING PONG PONG PONG PONG
Run PING Every 10th Second
Diagnostic Info Redis Instance: redis-cli --stat
cweid@strange:~$ redis-cli -h 127.0.0.1 -p 6379 -a mysupersecretpassword --stat ------- data ------ --------------------- load -------------------- - child - keys mem clients blocked requests connections 0 790.80K 1 0 122 (+0) 16 0 790.80K 1 0 123 (+1) 16 0 790.80K 1 0 124 (+1) 16 0 790.80K 1 0 125 (+1) 16 0 790.80K 1 0 126 (+1) 16
See: Keys Count, Memory Usage, Clients Connected/Blocked, Requests Served, Current Connections Useful Overview Redis Server Like Stating A File
Check Latency Redis Commands: redis-cli --latency
cweid@strange:~$ redis-cli -h 127.0.0.1 -p 6379 -a mysupersecretpassword --latency min: 0, max: 1, avg: 0.13 (763 samples)
Min, Max, Avg Request Time + Samples Note: Recorded In Microseconds More Info: Latency Monitoring Documentation
Analyze Keyspace Large Strings/Data Structures: --bigkeys Find Large Keys + Overall Distribution Key Types
cweid@strange:~$ redis-cli -h 127.0.0.1 -p 6379 -a mysupersecretpassword --bigkeys # Scanning the entire keyspace to find biggest keys as well as # average sizes per key type. You can use -i 0.1 to sleep 0.1 sec # per 100 SCAN commands (not usually needed). [00.00%] Biggest string found so far 'user:paul' with 4 bytes [00.00%] Biggest string found so far 'barrrr' with 19612 bytes -------- summary ------- Sampled 4 keys in the keyspace! Total key length in bytes is 29 (avg len 7.25) Biggest string found 'barrrr' has 19612 bytes 4 strings with 19624 bytes (100.00% of keys, avg size 4906.00) 0 lists with 0 items (00.00% of keys, avg size 0.00) 0 sets with 0 members (00.00% of keys, avg size 0.00) 0 hashs with 0 fields (00.00% of keys, avg size 0.00) 0 zsets with 0 members (00.00% of keys, avg size 0.00)
Useful Info Keys Types/Sizes Overall Redis CLI Powerful Tool Manage Redis Instance Built-in Options Help Analyze Problematic Redis Server