企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
# redis应用 - 消息队列,左进右出,用于定时发送短信,微信模板消息,最近新闻,最新新闻 ``` <pre class="calibre14">``` $r <span class="token">=</span> <span class="token5">new</span> <span class="token1">Redis</span><span class="token2">(</span><span class="token2">)</span><span class="token2">;</span> $r<span class="token">-</span><span class="token">></span><span class="token1">connect</span><span class="token2">(</span><span class="token4">"127.0.0.1"</span><span class="token2">,</span> <span class="token3">6379</span><span class="token2">)</span><span class="token2">;</span> $r<span class="token">-</span><span class="token">></span><span class="token1">lpush</span><span class="token2">(</span><span class="token4">"msg"</span><span class="token2">,</span><span class="token4">"入队"</span><span class="token2">)</span><span class="token2">;</span> $res <span class="token">=</span> $r<span class="token">-</span><span class="token">></span><span class="token1">brpop</span><span class="token2">(</span><span class="token4">"msg"</span><span class="token2">,</span><span class="token3">0</span><span class="token2">)</span><span class="token2">;</span><span class="token6">//出队 返回数组 $res[1] 才是值 与rpop直接返回值不同</span> ``` ``` - 集合,标签,爱好,共同好友 - 有序集合,排行榜,过滤器刷选 - 字符串,阅读量,计数,点赞,顶踩,session共享,限制每分钟 ``` <pre class="calibre14">``` $times <span class="token">=</span> <span class="token1">mktime</span><span class="token2">(</span><span class="token3">23</span><span class="token2">,</span><span class="token3">59</span><span class="token2">,</span><span class="token3">59</span><span class="token2">)</span> <span class="token">-</span> <span class="token1">mktime</span><span class="token2">(</span><span class="token1">date</span><span class="token2">(</span><span class="token4">'H'</span><span class="token2">)</span><span class="token2">,</span><span class="token1">date</span><span class="token2">(</span><span class="token4">'i'</span><span class="token2">)</span><span class="token2">,</span><span class="token1">date</span><span class="token2">(</span><span class="token4">'s'</span><span class="token2">)</span><span class="token2">)</span><span class="token2">;</span> $redis<span class="token">-</span><span class="token">></span><span class="token1">incr</span><span class="token2">(</span><span class="token4">"today"</span><span class="token2">)</span><span class="token2">;</span> $redis<span class="token">-</span><span class="token">></span><span class="token1">expire</span><span class="token2">(</span><span class="token4">'today'</span><span class="token2">,</span>$times<span class="token2">)</span><span class="token2">;</span><span class="token3">0</span>点自动删除<span class="token2">,</span>统计当天用户数 ``` ``` ``` <pre class="calibre14">``` <span class="token6">//手机验证码</span> $redis<span class="token">-</span><span class="token">></span><span class="token1">setex</span><span class="token2">(</span><span class="token4">'15689889565'</span><span class="token2">,</span> <span class="token3">60</span><span class="token2">,</span> <span class="token3">3455</span><span class="token2">)</span><span class="token2">;</span> <span class="token6">//验证码60秒过期</span> <span class="token6">//计数器</span> $redis<span class="token">-</span><span class="token">></span><span class="token1">incr</span><span class="token2">(</span>$key<span class="token2">)</span><span class="token2">;</span> <span class="token6">//自增</span> $redis<span class="token">-</span><span class="token">></span><span class="token1">incrby</span><span class="token2">(</span>$key<span class="token2">,</span> $increase_num<span class="token2">)</span><span class="token2">;</span> <span class="token6">//按量增加</span> $redis<span class="token">-</span><span class="token">></span><span class="token1">hincrby</span><span class="token2">(</span><span class="token4">'article:id'</span><span class="token2">,</span> <span class="token4">'click'</span><span class="token2">,</span> <span class="token3">1</span><span class="token2">)</span><span class="token2">;</span> <span class="token6">//点赞增加</span> ``` ``` - 哈希,缓存用户信息 ``` <pre class="calibre14">``` $id<span class="token">=</span><span class="token3">1</span><span class="token2">;</span> $redis<span class="token">-</span><span class="token">></span><span class="token1">hmset</span><span class="token2">(</span>‘user_’<span class="token2">.</span>$id<span class="token2">,</span><span class="token2">[</span><span class="token4">'name'</span><span class="token">=</span><span class="token">></span><span class="token4">'xxxxx'</span><span class="token2">,</span> <span class="token4">'sex'</span><span class="token">=</span><span class="token">></span><span class="token3">1</span><span class="token2">,</span> <span class="token4">'addr'</span><span class="token">=</span><span class="token">></span><span class="token4">'xxxxxxxxxf'</span><span class="token2">]</span><span class="token2">)</span><span class="token2">;</span> $user <span class="token">=</span> $redis<span class="token">-</span><span class="token">></span><span class="token1">hgetall</span><span class="token2">(</span><span class="token4">'user_'</span><span class="token2">.</span>$id<span class="token2">)</span><span class="token2">;</span> ``` ``` - 延时队列,处理订单一小时后关闭,七天未评价,提醒 ``` <pre class="calibre14">``` $redis<span class="token">-</span><span class="token">></span><span class="token1">zAdd</span><span class="token2">(</span><span class="token4">"sms"</span><span class="token2">,</span><span class="token1">time</span><span class="token2">(</span><span class="token2">)</span><span class="token">+</span><span class="token3">3600</span><span class="token2">,</span> <span class="token4">"消息数据"</span><span class="token2">)</span><span class="token2">;</span>入队 <span class="token5">while</span><span class="token2">(</span><span class="token3">true</span><span class="token2">)</span> <span class="token2">{</span> $msg <span class="token">=</span> $redis<span class="token">-</span><span class="token">></span><span class="token1">zRangeByScore</span><span class="token2">(</span><span class="token4">'sms'</span><span class="token2">,</span> <span class="token3">0</span><span class="token2">,</span> <span class="token1">time</span><span class="token2">(</span><span class="token2">)</span><span class="token2">,</span> <span class="token2">[</span><span class="token4">'limit'</span> <span class="token">=</span><span class="token">></span> <span class="token2">[</span><span class="token3">0</span><span class="token2">,</span> <span class="token3">1</span><span class="token2">]</span><span class="token2">]</span><span class="token2">)</span><span class="token2">;</span><span class="token6">//出队</span> <span class="token5">if</span> <span class="token2">(</span>$msg<span class="token2">)</span> <span class="token2">{</span> $msg<span class="token">=</span> $redis<span class="token">-</span><span class="token">></span><span class="token1">zrem</span><span class="token2">(</span><span class="token4">'sms'</span><span class="token2">,</span>$msg<span class="token2">[</span><span class="token3">0</span><span class="token2">]</span><span class="token2">)</span><span class="token2">;</span><span class="token6">//移除一个元素</span> <span class="token6">//具体业务</span> <span class="token2">}</span> <span class="token5">else</span> <span class="token2">{</span> <span class="token1">exit</span><span class="token2">(</span><span class="token4">"操作完成"</span><span class="token2">)</span><span class="token2">;</span> <span class="token2">}</span> <span class="token1">sleep</span><span class="token2">(</span><span class="token3">1</span><span class="token2">)</span><span class="token2">;</span> <span class="token2">}</span> ``` ``` - 最新评论,最新新闻,存储为list ``` <pre class="calibre14">``` lpush <span class="token5">new</span><span class="token2">.</span>say <span class="token3">1</span> 写入id ltrim <span class="token5">new</span><span class="token2">.</span>say <span class="token3">0</span> <span class="token3">9</span> 截取为最新<span class="token3">10</span>条前两条同时执行<span class="token2">;</span> lrange <span class="token5">new</span><span class="token2">.</span>say <span class="token3">0</span> <span class="token3">9</span> 取出这<span class="token3">10</span>个值 如果取出的元素数量不够<span class="token3">10</span>个<span class="token2">,</span>再从数据库查询 ``` ``` - 排名zset ``` <pre class="calibre14">``` zadd gamepop <span class="token3">10</span> userid1 写入游戏分数<span class="token2">,</span>每次写入 zrevrange gamepop <span class="token3">0</span> <span class="token3">3</span> 获取前四名排名分数从大到小 的id zrevrank gamepop userid4 显示所有位置中的排名 返回数字<span class="token">+</span><span class="token3">1</span>就是排名 ``` ``` - 统计用户 统计页面UV 统计IP ``` <pre class="calibre14">``` setbit online <span class="token3">1</span> <span class="token3">1</span> 设置用户<span class="token3">1</span>上线记录<span class="token3">1</span><span class="token2">,</span>第一个<span class="token3">1</span>是用户id<span class="token2">,</span>必须是数字 getbit online <span class="token3">1</span> 判断值是否<span class="token">=</span><span class="token3">1</span><span class="token2">,</span>是的话就在线 bitcount online 统计有多少个<span class="token3">1</span><span class="token2">,</span>就是在线用户 bitop or <span class="token3">7</span>days1 online1 online2 计算<span class="token3">2</span>天在线用户总数 bitop and <span class="token3">7</span>days2 online1 online2 计算两天都在线用户数 另一种方法 HyperLogLog pfadd onlines user1 记录上线 pfcount onlines 计算用户数量 pfmerge <span class="token3">7</span>day online1 online2<span class="token2">;</span> pfcount <span class="token3">7</span>day 计算两天内总人数 ``` ``` - 集群中共享 ``` <pre class="calibre16">``` <span class="token1">ini_set</span><span class="token2">(</span><span class="token4">"session.save_handler"</span><span class="token2">,</span> <span class="token4">"redis"</span><span class="token2">)</span><span class="token2">;</span> <span class="token1">ini_set</span><span class="token2">(</span><span class="token4">"session.save_path"</span><span class="token2">,</span> <span class="token4">"tcp://127.0.0.1:6379"</span><span class="token2">)</span><span class="token2">;</span> <span class="token1">session_start</span><span class="token2">(</span><span class="token2">)</span><span class="token2">;</span> $_SESSION<span class="token2">[</span><span class="token4">'qq'</span><span class="token2">]</span><span class="token">=</span><span class="token3">123</span><span class="token2">;</span> $redis <span class="token">=</span> <span class="token5">new</span> <span class="token1">Redis</span><span class="token2">(</span><span class="token2">)</span><span class="token2">;</span> $redis<span class="token">-</span><span class="token">></span><span class="token1">connect</span><span class="token2">(</span><span class="token4">'127.0.0.1'</span><span class="token2">,</span> <span class="token3">6379</span><span class="token2">)</span><span class="token2">;</span> echo $redis<span class="token">-</span><span class="token">></span><span class="token1">get</span><span class="token2">(</span> <span class="token4">'PHPREDIS_SESSION:'</span><span class="token2">.</span><span class="token1">session_id</span><span class="token2">(</span><span class="token2">)</span> <span class="token2">)</span><span class="token2">;</span> ``` ```