PSUBSCRIBE可以监听键的过期事件
1.进行数据库的配置
notify-keyspace-events Ex
2.使用命令监听事件
psubscribe __keyevnet@0__:expired
这样就会一个阻塞的监听key的过期事件
3.设置一个订单6秒后过期
setex
order:10 60 10
4.一旦order:10过期之后,reids客户端就会收到一个消息,执行过期业务
遇到的错误代码:
在命令执行redis订阅端脚本时,发现在终端会输出:
PHP Fatal error: Uncaught exception 'RedisException' with message 'read error on connection' in …
这个错误大概的意思就是遇到了一个未捕获的异常:RedisException,消息读取错误当连接的时候。
应该是redis的客户端读取超时原因导致。
很多人在github上留言能不能提供一个类似php的pconnect的接口,但是貌似redis官方对这个没有一个官方的解决办法。
错误解决办法(以下3种办法):
【1】设置,default_socket_time
= -1 但是本机测试的时候,应该是版本不一样的原因,直接报错:
redis server went away
【2】给redis
connect的时候( pconnect(host," role="presentation" style="margin: 0px;
padding: 0px; display: inline; font-style: normal; font-weight:
normal; line-height: normal; font-size: 14px; text-indent: 0px;
text-align: left; text-transform: none; letter-spacing: normal;
word-spacing: normal; overflow-wrap: normal; white-space: nowrap;
float: none; direction: ltr; max-width: none; max-height: none;
min-width: 0px; min-height: 0px; border: 0px; position:
relative;">host,host,port
= 6379, $timeout = 0.0 ))给timeout设置一个较大的值。
【3】通过Redis自带的常量设置
$redis->setOption(Redis::OPT_READ_TIMEOUT, -1);