search
Homephp教程php手册项目中应用Redis+Php的场景,项目redisphp场景

项目中应用Redis+Php的场景,项目redisphp场景

前言

一些案例中有的同学说为什么不可以用string类型,string类型完全可以实现呀

我建议你看下我的专栏文章《Redis高级用法》,里面介绍了用hash类型的好处

商品维度计数

对商品喜欢数,评论数,鉴定数,浏览数进行计数
说起电商,肯定离不开商品,而附带商品有各种计数(喜欢数,评论数,鉴定数,浏览数,etc)
Redis的命令都是原子性的,你可以轻松地利用INCR,DECR等命令来计数。

采用Redis 的类型: Hash. 如果你对redis数据类型不太熟悉,可以参考
http://redis.io/topics/data-types-intro

为product定义个key product:,为每种数值定义hashkey, 譬如喜欢数like_num

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

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

用户维度计数

对用户动态数、关注数、粉丝数、喜欢商品数、发帖数等计数
用户维度计数同商品维度计数都采用 Hash. 为User定义个key 为 user:
为每种数值定义hashkey, 譬如关注数follow

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

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

                                
存储社交关系

譬如将用戶的好友/粉丝/关注,可以存在一个sorted set中,score可以是timestamp
默认集合按照score递增排序
这样求两个人的共同好友的操作,可能就只需要用求交集命令即可

$redis->zAdd('user:1000:follow', 1463557212, '1001'); 

                #uid为1000用户关注uid为1001 , score值设定时间戳1463557212

$redis->zAdd('user:1000:follow', 1463557333, '1002'); 

$redis->zAdd('user:2000:follow', 1463577568, '1001'); 

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

$redis->zInter('com_fllow:1000:2000', array('user:1000:follow', 'user:2000:follow')); 

    #对集合'user:1000:follow'和'user:2000:follow'取交集'com_fllow:1000:2000'
    #获得共同关注的uid 

$redis->zRange('com_fllow:1000:2000',0,-1); // 获取全部集合元素
    #array('10001','10002')

用作缓存代替memcached

应用于商品列表,评论列表,@提示列表

相对memcached 简单的key-value存储来说,redis众多的数据结构(list,set,sorted set,hash,
etc)

可以更方便cache各种业务数据,性能也不亚于memcached。
NOTE: RPUSH pagewviews.user: EXPIRE pagewviews.user: 60 //注意要update timeout

反spam系统

应用系统评论、发布商品、论坛发贴的spam控制

作为一个电商网站被各种spam攻击是少不免(垃圾评论、发布垃圾商品、广告、刷自家商品排名等)

针对这些spam制定一系列anti-spam规则,其中有些规则可以利用redis做实时分析

譬如:1分钟评论不得超过2次、5分钟评论少于5次等(更多机制/规则需要结合drools )
常规sorted set将最近一天用户操作记录起来
(为什么不全部记录?节省memory,全部操作会记录到log,后续利用hadoop进行更全面分析统计)

#获取5秒内操作记录
$res = $redis->zRangeByScore('user:1000:comment', time() - 5, time());
#判断5秒内不能评论
if (!$res) {
  $redis->zAdd('user:1000:comment', time(), '评论内容');
} else {
  echo '5秒之内不能评论';
}

#5秒内评论不得超过2次
if($redis->zRangeByScore('user:1000:comment',time()-5 ,time())==1)
echo '5秒之内不能评论2次';

#5秒内评论不得少于2次

if(count($redis->zRangeByScore('user:1000:comment',time()-5 ,time()))<2)
echo '5秒之内不能评论2次';


 

用户Timeline/Feeds

应用于关注的人、主题、品牌及专栏

redis在这边主要当作cache使用

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

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

最新列表&排行榜

用于记录用户刚刚喜欢的商品最新列表or排行榜 等业务场景

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

    $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');
    
    $redis->zRange('user:1000:product:like', 0, -1,true); 
    #默认喜欢时间升序序排列
    #
      Array(
        [3002] => 1463565179
        [3001] => 1463565189
        [3004] => 1463565199
        [3003] => 1463565209
      )
    
    $redis->zRevRange('user:1000:product:like', 0, -1,true); 
    #以喜欢时间降序排列
    #
      Array
      (
        [3003] => 1463565424
        [3004] => 1463565414
        [3001] => 1463565404
        [3002] => 1463565394
      )
    

排行榜-list数据结构呈现

    $redis->lPush('user:1000:product:like', '3002');
    $redis->lPush('user:1000:product:like', '3001');
    $redis->lPush('user:1000:product:like', '3004');
    $redis->lPush('user:1000:product:like', '3003');
    
    $redis->lRange('user:1000:product:like', 0, -1);
    
    Array
    (
      [0] => 3003
      [1] => 3004
      [2] => 3001
      [3] => 3002
    )

消息通知

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

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

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

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

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

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

Array
(
  [system] => 2
  [comment] => 2
)



将Redis用作消息队列

采用Redis的List数据结构实现分布式的消息队列

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
es和redis区别es和redis区别Jul 06, 2019 pm 01:45 PM

Redis是现在最热门的key-value数据库,Redis的最大特点是key-value存储所带来的简单和高性能;相较于MongoDB和Redis,晚一年发布的ES可能知名度要低一些,ES的特点是搜索,ES是围绕搜索设计的。

一起来聊聊Redis有什么优势和特点一起来聊聊Redis有什么优势和特点May 16, 2022 pm 06:04 PM

本篇文章给大家带来了关于redis的相关知识,其中主要介绍了关于redis的一些优势和特点,Redis 是一个开源的使用ANSI C语言编写、遵守 BSD 协议、支持网络、可基于内存、分布式存储数据库,下面一起来看一下,希望对大家有帮助。

实例详解Redis Cluster集群收缩主从节点实例详解Redis Cluster集群收缩主从节点Apr 21, 2022 pm 06:23 PM

本篇文章给大家带来了关于redis的相关知识,其中主要介绍了Redis Cluster集群收缩主从节点的相关问题,包括了Cluster集群收缩概念、将6390主节点从集群中收缩、验证数据迁移过程是否导致数据异常等,希望对大家有帮助。

Redis实现排行榜及相同积分按时间排序功能的实现Redis实现排行榜及相同积分按时间排序功能的实现Aug 22, 2022 pm 05:51 PM

本篇文章给大家带来了关于redis的相关知识,其中主要介绍了Redis实现排行榜及相同积分按时间排序,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,希望对大家有帮助。

详细解析Redis中命令的原子性详细解析Redis中命令的原子性Jun 01, 2022 am 11:58 AM

本篇文章给大家带来了关于redis的相关知识,其中主要介绍了关于原子操作中命令原子性的相关问题,包括了处理并发的方案、编程模型、多IO线程以及单命令的相关内容,下面一起看一下,希望对大家有帮助。

一文搞懂redis的bitmap一文搞懂redis的bitmapApr 27, 2022 pm 07:48 PM

本篇文章给大家带来了关于redis的相关知识,其中主要介绍了bitmap问题,Redis 为我们提供了位图这一数据结构,位图数据结构其实并不是一个全新的玩意,我们可以简单的认为就是个数组,只是里面的内容只能为0或1而已,希望对大家有帮助。

实例详解Redis实现排行榜及相同积分按时间排序功能的实现实例详解Redis实现排行榜及相同积分按时间排序功能的实现Aug 26, 2022 pm 02:09 PM

本篇文章给大家带来了关于redis的相关知识,其中主要介绍了Redis实现排行榜及相同积分按时间排序,本文通过实例代码给大家介绍的非常详细,下面一起来看一下,希望对大家有帮助。

一起聊聊Redis实现秒杀的问题一起聊聊Redis实现秒杀的问题May 27, 2022 am 11:40 AM

本篇文章给大家带来了关于redis的相关知识,其中主要介绍了关于实现秒杀的相关内容,包括了秒杀逻辑、存在的链接超时、超卖和库存遗留的问题,下面一起来看一下,希望对大家有帮助。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool