redis的实际应用场景

参考:https://www.cnblogs.com/lxwphp/p/7727796.html

1.商品维度计算

对商品喜欢数,评论数,鉴定数,浏览数进行计数,可以利用INCRDECR等命令来计数

<?php
$redis = new Redis();
$redis->connect('127.0.0.1',6379);
$redis->hSet('product:123', 'like ', 1);  // 添加 id为123的商品 like 为1

$redis->hIncrBy('product:123', 'like ', 1);  // 添加 id为123的商品like +1

$a = $redis->hGetAll('product:123'); // 获取id为123的商品相关信息
print_r($a);

//输出:Array ( [like_num] => 2 )
//扩展板

2.用户维度计数

对用户动态数、关注数、粉丝数、喜欢商品数、发帖数等计数 ,用户维度计数同商品维度计数都采用 Hash.

<?php
$redis = new Redis();
$redis->connect('127.0.0.1',6379);
$redis->hSet('user:100000', 'follow ', 5);  // 添加uid为10000的用户follow 为5

$redis->hIncrBy('user:100000', 'follow ', 1);  // 更新uid为10000的用户follow +1

$a = $redis->hGetAll('user:100000'); // 获取uid为10000的用户

print_r($a);

//输出:Array ( [follow ] => 6 )

3.存储社交关系

譬如将用戶的好友/粉丝/关注,可以存在一个sorted set中,score可以是timestamp

默认集合按照score递增排序

这样求两个人的共同好友的操作,可能就只需要用求交集命令即可

<?php
$redis = new Redis();
$redis->connect('127.0.0.1',6379);

#uid为1000用户关注uid为1001\1002, score值设定时间戳1463557212\1463557333
$redis->zAdd('user:1000:follow', 1463557212, '1001');
$redis->zAdd('user:1000:follow', 1463557333, '1002');

#uid为2000用户关注uid为1001\1003, score值设定时间戳1463577568\1463896964
$redis->zAdd('user:2000:follow', 1463577568, '1001');
$redis->zAdd('user:2000:follow', 1463896964, '1003');

#对集合'user:1000:follow'和'user:2000:follow'取交集'com_fllow:1000:2000'
$redis->zInter('com_fllow:1000:2000', array('user:1000:follow', 'user:2000:follow'));

#获得共同关注的uid
$a = $redis->zRange('com_fllow:1000:2000',0,-1);

#获得uid为2000的用户关注了的人的uid
$b = $redis->zRange('user:1000:follow',0,-1,0);#默认为0,显示key值
$c = $redis->zRange('user:2000:follow',0,-1,1);#1显示全部数组内容

print_r($a);
echo '<br>';
print_r($b);
echo '<br>';
print_r($c);

//输出:Array ( [0] => 1001 ) 
//Array ( [0] => 1001 [1] => 1002 ) 
//Array ( [1001] => 1463577568 [1003] => 1463896964 )

4.反spam系统

  1. 应用系统评论、发布商品、论坛发贴的spam控制;
  2. 作为一个电商网站被各种spam攻击是少不免(垃圾评论、发布垃圾商品、广告、刷自家商品排名等);
  3. 针对这些spam制定一系列anti-spam规则,其中有些规则可以利用redis做实时分析;
  4. 譬如:1分钟评论不得超过2次、5分钟评论少于5次等(更多机制/规则需要结合drools );
  5. 常规sorted set将最近一天用户操作记录起来。

(为什么不全部记录?节省memory,全部操作会记录到log,后续利用hadoop进行更全面分析统计)

<?php
function rang($com){
    $redis = new Redis();
    $redis->connect('127.0.0.1',6379);
    $key = 'user:1006:comment';
#获取5秒内操作记录
    $res5 = $redis->zRangeByScore($key, time() - 5, time());
    $res60 = $redis->zRangeByScore($key, time() - 60, time());
    $res300 = $redis->zRangeByScore($key, time() - 300, time());


    if ($com){
        if (count($res300)>5)
            return '5分钟之内不能评论5次';
        if (count($res60)>=2)
            return '1分钟之内不能评论2次';
        if ($res5)
            return '5秒之内不能评论';
        else
            $redis->zAdd($key, time(), time().$com);

        /* #判断5分钟之内不能评论5次
         if (count($res300)<=5) {
             #判断1分钟之内不能评论2次
             if (count($res60)<2){
                 #判断5秒之内是否有评论
                 if (!$res5){
                     $redis->zAdd($key, time(), time().$com);
                 } else {
                     echo '5秒之内不能评论';
                 }
             } else {
                 echo '1分钟之内不能评论2次';
             }
         } else {
             echo '5分钟之内不能评论5次';
         }*/
    }
    $a = $redis->zRange($key,0,-1,0);#1显示全部数组内容
    foreach ($a as $k => &$v){
        $v = substr($v,10);
    }
    return $a;
}
$com = isset($_POST['com']) ? $_POST['com'] : '';
$res = rang($com);
print_r($res) ;

?>
<form action="a.php" method="post">
    <input type="text" name="com" value="<?= $com ?>">
    <input type="submit" value="评论">
</form>

5.用户Timeline/Feeds

应用于关注的人、主题、品牌及专栏,redis在这边主要当作cache使用

<?php
//a.php
$redis = new Redis();
$redis->connect('127.0.0.1',6379);

//score 为timestamp uid为2000的用户关注tid为13的topic
$redis->zAdd('user:2000:feed:topic', time(), '我是话题1');
$redis->zAdd('user:2001:feed:topic', time(), '我是话题2');

# ttl 30天之内按秒数计算 30天之外以timestamp为准
$redis->expire('user:2000:feed:topic',24*60*60);#关注有效期为24小时
$redis->expire('user:2001:feed:topic',5);#关注有效期为5s

<?php
//b.php
$redis = new Redis();
$redis->connect('127.0.0.1',6379);

$b = $redis->zRange('user:2000:feed:topic',0,-1,0);#默认为0,显示key值
$c = $redis->zRange('user:2001:feed:topic',0,-1,0);#默认为0,显示key值

print_r($b);
print_r($c);

6.最新列表和排行榜

商品最新列表-sorted set结构呈现

<?php
include_once 'include.php';

$sql = "select a.id,a.title,a.createtime,a.type,a.file_url,b.username from lyy_ziliao a 
JOIN lyy_member b ON a.uid=b.id WHERE a.del=1 ";
$res = mysqli_query(conn(),$sql);
$lists = mysqli_fetch_all($res,MYSQLI_ASSOC);#资料列表

$redis = new Redis();
$redis->connect('127.0.0.1',6379);

foreach ($lists as $k=>$v){#将数据库查出来的数据放入redis缓存
    $redis->zAdd('user:1000:product:name', time(), $v['title']);
}
$redis->zAdd('user:1000:product:like', time(), '3002');
$redis->zAdd('user:1000:product:like', time(), '3001');
$redis->zAdd('user:1000:product:like', time(), '3004');
$redis->zAdd('user:1000:product:like', time(), '3003');

#默认时间升序序排列
$a = $redis->zRange('user:1000:product:name', 0, -1,false);
//$a = $redis->zRange('user:1000:product:like', 0, -1,false);
print_r($a);

#以时间降序排列
$a = $redis->zRevRange('user:1000:product:like', 0, -1,true);
//$redis->flushAll();#清除数据
print_r($a);

队列排序
<?php
include_once 'include.php';

$sql = "select a.id,a.title,a.createtime,a.type,a.file_url,b.username from lyy_ziliao a 
JOIN lyy_member b ON a.uid=b.id WHERE a.del=1 ";
$res = mysqli_query(conn(),$sql);
$lists = mysqli_fetch_all($res,MYSQLI_ASSOC);#资料列表

$redis = new Redis();
$redis->connect('127.0.0.1',6379);

$redis->lPush('user:1000:product:l', '3006');
$redis->lPush('user:1000:product:l', '3001');
$redis->lPush('user:1000:product:l', '3004');
$redis->lPush('user:1000:product:l', '3003');

$a = $redis->lRange('user:1000:product:l', 0, -1);
//$a = $redis->zRange('user:1000:product:like', 0, -1,true);
$redis->flushAll();
print_r($a);

7.消息通知

采用Hash结构对消息通知业务场景计数

$redis = new Redis();
$redis->connect('127.0.0.1',6379);

#设置1条未读系统消息
$redis->hSet('user:1000:message:notice', 'system', 0);

$redis->hIncrBy('user:1000:message:notice', 'system', 1);#未读系统消息+1

$redis->hSet('user:1000:message:notice', 'comment', 0);#设置1条未读评论

$redis->hIncrBy('user:1000:message:notice', 'comment', 1);#未读评论+1

#查看所有消息通知数量
$a = $redis->hGetAll('user:1000:message:notice');
print_r($a);

输出:Array ( [system] => 1 [comment] => 1 )

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值