Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to apply Redis+Php

2025-02-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Commodity dimension count

Count the number of likes, comments, appraisals and views of goods

When it comes to e-commerce, it must be inseparable from goods, and there are various counts of incidental goods (likes, comments, appraisals, views, etc).

Redis commands are atomic, and you can easily count them with commands such as INCR,DECR.

Adopt the type of Redis: 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 and define hashkey for each type of value, for example, like to count like_num

$redis- > hSet ('product:123',' like_num', 5); / add goods with id 123The like_num is 5$ redis- > hIncrBy ('product:123',' like_num', 1); / add goods with id 123like_num + 1$ redis- > hGetAll ('product:123'); / / get product related information array with id 123s (' like_num'= > 1)

User dimension count

Count the number of dynamic users, followers, fans, favorite products, posts, etc.

Both the user dimension count and the commodity dimension count use Hash. Define a key for User as user:

Define a hashkey for each value, such as the number of concerns follow

$redis- > hSet ('user:100000',' follow', 5); / add user follow with uid 10000 as 5$ redis- > hIncrBy ('user:100000',' follow', 1); / update user follow + 1$ redis- > hGetAll ('user:100000') with uid 10000; / / get user array with uid 10000 (' like_num'= > 1)

Store social relationships

For example, friends / fans / followers who will use score can be stored in a sorted set, and score can be timestamp.

The default collection is sorted incrementally by score

In this way, the operation of finding a common friend of two people may only need to use the command to find the intersection.

$redis- > zAdd ('user:1000:follow', 1463557212,' 1001'); # uid for 1000 users follow uid is 1001, score value sets timestamp 1463557212$ redis- > zAdd ('user:1000:follow', 1463557333,' 1002'); $redis- > zAdd ('user:2000:follow', 1463577568,' 1001'); $redis- > zAdd ('user:2000:follow', 1463896964,' 1003') # uid follows 1001 and 1003 users for 2000 users, and the score value sets the timestamp $redis- > zInter ('com_fllow:1000:2000', array (' user:1000:follow', 'user:2000:follow')); # pairs of sets' user:1000:follow' and 'user:2000:follow' intersection' com_fllow:1000:2000' # to get common concerns uid $redis- > zRange ('com_fllow:1000:2000',0,-1) / / get all the collection elements # array ('10001records and records 10002')

Used as a cache instead of memcached

Apply to product list, comment list, @ prompt list

Compared with memcached's simple key-value storage, redis's numerous data structures (list,set,sorted set,hash)

Etc)

It is more convenient to cache all kinds of business data, and its performance is no less than that of memcached.

NOTE: RPUSH pagewviews.user: EXPIRE pagewviews.user: 60 / / pay attention to update timeout

Anti-spam system

Spam control of comment, publication and forum posting of application system

As an e-commerce website, it is inevitable to be attacked by all kinds of spam (spam comments, post junk goods, advertisements, brush your own product rankings, etc.)

Make a series of anti-spam rules for these spam, some of which can be analyzed in real time by redis.

For example, no more than 2 comments per minute, less than 5 comments in 5 minutes, etc. (more mechanisms / rules need to be combined with drools)

Regular sorted set records the most recent day of user actions

Why not record it all? Save memory, all operations will be recorded to log, and then use hadoop for more comprehensive analysis and statistics)

# get the operation record within 5 seconds $res = $redis- > zRangeByScore ('user:1000:comment', time ()-5, time ()); # determine that you cannot comment on if (! $res) {$redis- > zAdd (' user:1000:comment', time (), 'comment content');} else {echo'5 seconds cannot comment' } No more than 2 if comments within # 5 seconds ($redis- > zRangeByScore ('user:1000:comment',time ()-5, time ()) = 1) echo' 2 comments within 5 seconds' and no less than 2 if comments within # 5 seconds (count ($redis- > zRangeByScore ('user:1000:comment',time ()-5, time ()) zAdd (' user:2000:feed:topic', time (), '13')) / / users with a timestamp uid of 2000 follow topic$redis- > expire with a tid of 13 ('user:2000:feed:topic',24*60*60); # follow is valid for 24 hours # ttl is calculated in seconds within 30 days except timestamp.

The latest list & ranking list

It is used to record business scenarios such as the latest list of products that users have just liked, such as or rankings.

The latest list of goods-sorted set structure presentation

$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,-1jue true) # default preference time ascending order # Array ([3002] = > 1463565179 [3001] = > 1463565189 [3004] = > 1463565199 [3003] = > 1463565209) $redis- > zRevRange ('user:1000:product:like', 0,-1 user:1000:product:like', true) # Array in descending order of preference time ([3003] = > 1463565424 [3004] = > 1463565414 [3001] = > 1463565404 [3002] = > 1463565394)

Ranking-list data structure presentation

$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

Count the business scenarios of message notification using Hash structure

$redis- > hSet ('user:1000:message:notice',' system', 1); # set an unread system message $redis- > hIncrBy ('user:1000:message:notice',' system', 1); # unread system message + 1$ redis- > hSet ('user:1000:message:notice',' comment', 1); # set an unread comment $redis- > hIncrBy ('user:1000:message:notice',' comment', 1) # unread comments + 1$ redis- > hGetAll ('user:1000:message:notice'); # View the number of message notifications Array ([system] = > 2 [comment] = > 2)

Using Redis as a message queue

Using List data structure of Redis to realize distributed message queue

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report