Project Application redis+php scenario, Project redisphp scene _php tutorial

Source: Internet
Author: User

Project Application redis+php scenario, project redisphp scene


Objective

Some of the students in some cases say why can not use String type, string type can be fully implemented AH

I suggest you look at my column, "Redis Advanced Usage," which describes the benefits of using hash types.

Commodity dimension Count

Count the number of items, number of comments, number of appraisals, number of views
Speaking of e-commerce, must be inseparable from goods, and the goods with a variety of counts (like number, number of reviews, number of appraisals, views, etc)
Redis commands are atomic, and you can easily count them with commands such as INCR,DECR.

The type of Redis used: Hash. If you are not familiar with Redis data types, you can refer to the
Http://redis.io/topics/data-types-intro

Define a key product for product: HashKey for each value, such as a number like_num

$redis->hset (' product:123 ', ' Like_num ', 5); Add an item with ID 123 like_num to 5 $redis->hincrby (' product:123 ', ' Like_num ', 1); Add an item with an ID of 123 like_num +1$redis->hgetall (' product:123 '); Get product-related information for ID 123                  Array (' Like_num ' + 1)

Count of user dimensions

Count the number of users, number of followers, number of fans, number of likes, number of posts, etc.
The user dimension count is hashed with the commodity dimension count. Define a key for user as User:
Define HashKey for each value, such as the number of followers follow

$redis->hset (' user:100000 ', ' follow ', 5); Add UID 10000 user follow to 5 $redis->hincrby (' user:100000 ', ' follow ', 1); Update the user with UID 10000 follow +1$redis->hgetall (' user:100000 '); Gets the user with UID 10000                  (' like_num ' = + 1)


Store Social relationships

For example, the user's friends/fans/concerns, can exist in a sorted set, score can be timestamp
The default collection is sorted incrementally by score
In this way, the operation of a common friend of two people may only need to use the intersection command.

$redis->zadd (' User:1000:follow ', 1463557212, ' 1001 ');                 #uid为1000用户关注uid为1001, the score value sets the timestamp 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用户, the score value sets the timestamp $redis->zinter (' com_fllow:1000:2000 ', Array (' User:1000:follow ', ' user : 2000:follow '));     #对集合 ' User:1000:follow ' and ' User:2000:follow ' take the intersection of ' com_fllow:1000:2000 '    #获得共同关注的uid $redis->zrange (' Com_fllow : 1000:2000 ', 0,-1); Get all collection elements    #array (' 10001 ', ' 10002 ')

Use as cache instead of memcached

Apply to Product list, comment list, @ hint list

Compared to memcached simple key-value storage, Redis's numerous data structures (list,set,sorted Set,hash,
etc

It can be more convenient to cache various business data, performance is no less than memcached.
Note:rpush Pagewviews.user:EXPIRE pagewviews.user:60//Note to update timeout

Anti-spam system

Spam Control for application reviews, publishing products, forum posts

As an e-commerce site by a variety of spam attacks is less unavoidable (spam reviews, spam, advertising, brush home products rankings, etc.)

Develop a series of anti-spam rules for these spam, some of which can be used for real-time analysis using Redis

For example: 1-minute reviews must not exceed 2 times, 5-minute reviews less than 5 times (more mechanisms/rules need to be combined with drools)
Regular sorted set records the last day of user action
(Why not all records?) Save memory, all operations will be recorded to log, subsequent use of Hadoop for more comprehensive analysis of statistics)

#获取5秒内操作记录 $res = $redis->zrangebyscore (' User:1000:comment ', Time ()-5, Time ()), #判断5秒内不能评论if (! $res) {  $redis- >zadd (' User:1000:comment ', Time (), ' comment content ');} else {  

User Timeline/feeds

People, themes, brands and columns to focus on

Redis is mostly used as a cache here.

$redis->zadd (' User:2000:feed:topic ', Time (), ' 2000 '),//score for timestamp uid 13 for the topic$redis-> of Tid Expire (' User:2000:feed:topic ', 24*60*60); #关注有效期为24小时 # TTL within 30 days in seconds 30 days outside of the timestamp

Latest Listings & Leaderboards

Used to record the latest list of items the user has just liked. Business scenarios such as or leaderboard

Latest list of items-sorted set structure rendering

    $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      )    

Leaderboard-LIST Data Structure rendering

    $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    )

Message notification

Use hash structure to count message notification business scenarios

$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 '); View all message notifications array (  [System] + 2  [Comment] = 2)

Using Redis as a message queue

Using the Redis list data structure to implement distributed Message Queuing

http://www.bkjia.com/PHPjc/1133110.html www.bkjia.com true http://www.bkjia.com/PHPjc/1133110.html techarticle Project Application redis+php scenario, Project redisphp scenario Preface Some of the students in some cases said why can not use String type, string type can be fully implemented AH I suggest you look at ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.