「Redis学习笔记」生存周期

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



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值