Redis Notes
- C
- Corss-Platform
- NoSQL
Install
Usage
$ redis-cli
Data Types
- Strings, Lists, Sets, Sorted Sets, Hashes
- case-sensitive
clear : clear consoleping : test redis workFLUSHALL : 清空ECHO '123'
Strings
SET foo 100GET fooSET bar 'hi'GET bar
INCR foo : +1DECR foo : -1EXISTS foo : 是否存在EXISTS foooo
SET server:name hihiGET server:nameSET server:port 8000GET server:port
EXPIRE bar 50 : 50秒後過期TTL bar : 看剩幾秒
SETEX bar 30 "hi" : 過期前設值TTL bar : 看剩幾秒PERSIST bar : 永久存在
MSET : 設定很多值APPEND : 串接加上RENAME : 改名
Lists (L)
LPUSH people "abby1"LPUSH people "abby2"RPUSH people "abby3"RPUSH people "abby4"
LRANGE people 0 -1 : print allLRANGE people 0 1 : index 0, 1
LPOP peopleRPOP peolple
LINSERT people BEFORE "abby2" "abbyX"
Sets (S)
SADD cars "Ford"SADD cars "Honda"SADD cars "BWM"
SISMEMBER cars "Ford"SMEMBERS cars
SMOVE cars mycars "Ford"
SREM : remove
Sorted Sets (Z)
ZADD users 1988 'abby'ZADD users 1288 'ybba'
ZRANGE users 0 -1 : print all
ZRANK user 'abby' : 回傳排名
Hashes (H)
HSET user:abby name "Abby"HSET user:abby email "abby@com"HGET user:abbyHGET user:abby emailHGETALL user:abby
Data Persistence
Snapshottingsave 60 1000
Applications
- 短網址系統
- 統計系統
- 秒殺搶購系統