1. expire; persist
127.0.0.1:6379> set session:29e3d uid1314
OK
127.0.0.1:6379> expire session:29e3d 900
(integer) 1
127.0.0.1:6379> del session:29e3d
(integer) 1
127.0.0.1:6379> expire session:29e3d 900
(integer) 0
127.0.0.1:6379> set foo bar
OK
127.0.0.1:6379> expire foo 20
(integer) 1
127.0.0.1:6379> ttl foo
(integer) 15
127.0.0.1:6379> ttl foo
(integer) 7
127.0.0.1:6379> ttl foo
(integer) 2
127.0.0.1:6379> ttl too
(integer) -2
127.0.0.1:6379> ttl foo
(integer) -2
127.0.0.1:6379> get foo
(nil)
127.0.0.1:6379> set foo bar
OK
127.0.0.1:6379> expire foo 20
(integer) 1
127.0.0.1:6379> persist foo
(integer) 1
127.0.0.1:6379> ttl foo
(integer) -1
如果使用watch命令检测了一个拥有生存时间的键,该键时间到期自动删除并不会被watch命令认为该键被改变。
2. 限制一分钟内只能访问100次
$isKeyExists = exists rate.limiting:$IP
if $isKeyExists is 1
$times = incr rate.limiting:$IP
if $times > 100
print the times of visit beyond the limit, please try it again for mins!
exit
else
multi
incr rate.limiting:$IP
expire $keyName, 60
exec
$isKeyExists = exists rate.limiting:$IP
if $isKeyExists is 1
$times = incr rate.limiting:$IP
if $times > 100
print the times of visit beyond the limit, please try it again for mins!
exit
else
multi
incr rate.limiting:$IP
expire $keyName, 60
exec
$listLength = llen rate.limiting:$IP
if $listLength < 10
lpush rate.limiting:$IP, now()
else
$time = lindex rate.limiting:$IP, -1
if now() - $time < 60
print please try it again for several minus
else
lpush rate.limiting:$IP, now()
ltrim rate.limiting:$IP, 0, 9