brew install redis
redis-server
select 0/1/2/3
set name ‘name‘
set user:1 ‘name‘
string
set
get
append keyname appendvalue
getrange keyname rangestart rangeend
setrange keyname rangestart value
getset keyname value
mset(m means multi) key value key value
mget key key key
incr key
decr key
incrby key step
deincrby key step
incrbyfload key floatvalue
setex key ttl value
psetex key ttl value // ttl is milliseconds
setnx key value
msetnx key value key value
strlen key
list
lpush listname value value value //l means left
rpush listname value value value //r means right
lpushx
rpushx
len listname //list length
lrange listname start end
lrange list 0 -1 //get all the list items
lpop listname
rpop listname
lrem listname numters to remove removevalues
for example:
lrem colors 2 green
lrem also can remove items backwards,for example:
lrem colors -2 green wlll remove items in colors from bottom to the top
linsert listname before value insertvalue
linsert listname after value insertvalue
Notice: redis commands also can be upper case ,for example:
LINSERT colors AFTER "yellow" "purple"
lindex listname index
lset listname index value
ltrim listname start end
rpoplpush listname otherlist
blpop listname time
brpop listname time
brpoplpush list list 0
hash
hset hashname keyname keyvalue
hget hashname key
hgetall hashname
hkeys hashname
hvals hashname
hexists hashname keyname
hincrby hashname key step
hincrbyfloat hashname key floatvalue
hlen hashname
hmget hasnname key key
hmset hashname key value key value
hsetnx hashname key value
hdel hashname key
set
sadd setname value value value
srem setname value
smembers setname
sismember setname value
scard setname
srandmember setname
spop setname
sdiff set1 set2
sdiffstore newset set1 set2
sinter set1 set2
sinterstore newset set1 set2
sunion set1 set2
sunionstore newset set1 set2
smove fromset toset value
sorted set
zadd sortsetname weight value weight value weight value
zcard sortedsetname
zrange sortedsetname start end [withscores]
zrevrange sortedsetname start end [withscores]
zrangebyscore sortedsetname lowscore heighscore [withscore]
zrevrangebyscore sortedsetname heighscore lowscore [withscore]
zincrby sortedsetname score value
zscore sortedsetname value
zrank scortedsetname value
zrevrank sortedsetname value
zcount sortedsetname startscore endscore
zrem sortedsetname value
zremrangebyscore sortedsetname startscore endscore
zremrangebyrank sortedsetname startrange endrange
keys *user
exists key
type key
del
move key dbindex
expire key secondstoexpire
pexipre key sendstoexpire
expireat key timestamp
pexireat key timestamp
ttl
pttl
persist key
randomkey
rename
renamenx
sort
sort listname
sort listname limit from to
sort listname desc
sort listname desc limit from to
sort listname desc limit from to store listname
sort listname alpha
lrange medals 0 -1
third
second
first
keys medal:*
medal:third
medal:first
medal:second
hgetall medal:first
medal
gold
position
1
hgetall medal:second
medal
sliver
position
2
hgetall medal:third
medal
bronze
position
3
sort medals by medal:*->position
sort medals by medal:*->position get medal:*->medal
publish and subscribe commands
subscribe channelname channelname
publish channelname message
psubscribe a* //pattern subscribe
unsubscribe channelname channelname
punsubscribe channelpattern
transaction
multi
command
command
exec
discard //cancel the transaction
administration
redis.conf
daemonize yes
port 8888
loglevel debug/verbose/notice/warning
logfile /var/log/redis.log
save howofentosave//seconds howmanychanges
save 3000 1
requirepass password
rename-command config othercommandname
maxclients 8 //number of redis clients
slaveof localhost 6379
slave-read-only no
LUA scripting
validate lua script
eval "lua script" 0
i,e eval "return 1 + 1 " 0
eval "return redis.call(‘get‘,‘age‘)" 0
eval "return redis.call(‘get‘,keys[1])" 1 age
eval "return redis.call(‘get‘,keys[1]) - ARGV[1]" 1 age 2.5//simulate a decrbyfloat function
eval "redis.call(‘set‘,KEYS[1],redis.call(‘get‘,keys[1]) - ARGV[1])" 1 age 2.5//simulate a decrbyfloat function
11 common web use cases solved in redis
highscalability.com/blog/2011/7/6/11-common-web-use-cases-solved-in-redis.html