redis日常开发中的bug
Redis开发中容易出错的地方
本文档使用 MrDoc 发布
-
+
首页
Redis开发中容易出错的地方
1.创建key与incr未使用原子操作,会导致bug ```php $key = 'user_call:' . $user_id . ':' . $supply_id; if (Redis::exists($key)) { Redis::incr($key); } else { Redis::set($key, 1); Redis::expireAt($key, strtotime(date('Y-m-d') . ' 23:59:59')); } ``` 修改后 ```php $key = 'user_call:' . $user_id . ':' . $supply_id; // 使用 incr 操作,如果 key 不存在则创建并初始化为 1 $result = Redis::incr($key); // 如果是新创建的 key,则设置过期时间 if ($result == 1) { Redis::expireAt($key, strtotime(date('Y-m-d') . ' 23:59:59')); } ```
admin
2025年10月21日 10:32
转发文档
收藏文档
上一篇
下一篇
手机扫码
复制链接
手机扫一扫转发分享
复制链接
Markdown文件
分享
链接
类型
密码
更新密码