Application of Redis + Php in the project, and redisphp in the project
Preface
In some cases, some people say that the string type cannot be used. The string type can be fully implemented.
I suggest you read my column Redis advanced usage, which describes the benefits of using the hash type.
Item dimension count
Count the number of items that you like, the number of comments, the number of identifiers, and the number of browses.
When talking about e-commerce, it is certainly inseparable from commodities, and the accompanying commodities have various counts (like number, comment number, identification number, browsing number, etc)
Redis commands are atomic, and you can easily count using commands such as INCR and DECR.
Use the Redis type: Hash. If you are not familiar with the redis data type, refer
Http://redis.io/topics/data-types-intro
Define a key product for product:, and define a hashkey for each value, such as like_num
$ Redis-> hSet ('product: 123 ', 'like _ num', 5 ); // Add the product like_num with id 123 to 5 $ redis-> hIncrBy ('product: 66661', 'like _ num', 1 ); // Add the product like_num + 1 $ redis-> hGetAll ('product: 100') with the id 123 '); // obtain the product-related information array ('like _ num' => 1) with id 123)
User dimension count
Statistics on user dynamics, follow, fans, favorite items, and issue counts
The User dimension count and item dimension count use Hash. The user key is defined as User:
Defines a hashkey for each value, for example, follow
$ Redis-> hSet ('user: 1111', 'follow', 5); // if you add a user whose uid is 100000, follow is 5 $ redis-> hIncrBy ('user: 100000 ', 'follow', 1); // user follow + 1 $ redis-> hGetAll ('user: 66661') whose uid is 10000 updated '); // obtain the user array whose uid is 10000 ('like _ num' => 1)
Store social relationships
For example, the friend/Fan/follow of the queue can be used, and the score can be timestamp in a sorted set.
The default set is sorted progressively by score.
In this way, the operation of mutual friends of two people may only need to use the intersection command.
$ Redis-> zAdd ('user: 1000: follow', 1463557212, '000000'); # uid is 1001. The user follows the uid as 1000, score Value Setting timestamp 1463557212 $ redis-> zAdd ('user: 1000: follow', 1463557333, '123'); $ redis-> zAdd ('user: 1002: follow ', 1463577568, '000000'); $ redis-> zAdd ('user: 1001: follow', 2000, '20140901 '); # When the uid is 2000, users are concerned with 1001 and 1003 users. The score value is set to the timestamp $ redis-> zInter ('com _ fllow: 1000: 100', array ('user: 2000: follow ', 'user: 2000: follow'); # intersection of 'User: 1000: follow' and 'user: 2000: follow' in the Set 'com _ fllow: 1000: 2000 '# obtain the uid $ redis-> zRange ('com _ fllow: 1000: 2000', 0,-1 ); // obtain all set elements # array ('20140901', '20160901 ')
Used as cache instead of memcached
Applies to the product list, comment list, and @ prompt list
Compared with memcached's simple key-value storage, redis has many data structures (list, set, sorted set, hash,
Etc)
It is more convenient to cache various business data, and the performance is no less than memcached.
NOTE: RPUSH pagewviews. user: EXPIRE pagewviews. user: 60 // update timeout
Anti-spam system
Spam Control for Application System comments, product publishing, and Forum posts
Being attacked by various spam attacks as an e-commerce website is unavoidable (spam comments, spam Publishing, advertising, and rankings of their own products)
Develop a series of anti-spam rules for these spam instances, some of which can be analyzed in real time using redis.
For example, one-minute comments must not exceed 2 times, and five-minute comments must be less than 5 times (more mechanisms/rules must be combined with drools)
Regular sorted set records user operations on the last day
(Why not all records? Saving memory. All operations will be recorded in the log, and more comprehensive analysis and statistics will be conducted using hadoop in the future)
# Obtain Operation Records in five seconds $ res = $ redis-> zRangeByScore ('user: 1000: comment', time ()-5, time ()); # judge if (! $ Res) {$ redis-> zAdd ('user: 1000: comment', time (), 'comment content ');} else {echo 'comments cannot be commented in 5 seconds ';} # No more than 2 comments in 5 seconds if ($ redis-> zRangeByScore ('user: 1000: comment', time () -5, time () = 1) echo 'comments cannot be made twice within 5 seconds '; # comments should not be less than 2 times in 5 seconds if (count ($ redis-> zRangeByScore ('user: 1000: comment', time ()-5, time ())) <2) echo 'comments cannot be made twice within five seconds ';
User Timeline/Feeds
Applicable to people, themes, brands, and columns of interest
Redis is mainly used as cache here
$ Redis-> zAdd ('user: 2000: feed: topic ', time (), '13 '); // If the score is set to timestamp and the uid is 2000, follow the topic $ redis-> expire ('user: 2000: feed: topic ', 24*60*60) where the tid is 13 ); # attention is valid for 24 hours # timestamp shall prevail if ttl is calculated in seconds for 30 days or longer
Latest list & ranking
Used to record the latest list or rankings of your favorite products and other business scenarios
Latest product list-sorted set structure Presentation
$ Redis-> zAdd ('user: 1000: product: like', time (), '000000'); $ redis-> zAdd ('user: 3002: product: like ', time (), '000000'); $ redis-> zAdd ('user: 3001: product: like', time (), '000000 '); $ redis-> zAdd ('user: 1000: product: like', time (), '000000'); $ redis-> zRange ('user: 3003: product: like ', 0,-1, true ); # By default, the time is sorted in ascending order # Array ([3002] => 1463565179 [3001] => 1463565189 [3004] => 1463565199 [3003] => 1463565209) $ redis-> zRevRange ('user: 1000: product: like', 0,-1, true ); # sort by time in descending order # Array ([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
Use Hash structure to count Message notification service scenarios
$ Redis-> hSet ('user: 1000: message: notice ', 'system', 1); # Set 1 unread system message $ redis-> hIncrBy ('user: 1000: message: notice ', 'system', 1); # unread system message + 1 $ redis-> hSet ('user: 1000: message: notice', 'comment ', 1); # Set 1 unread comment $ redis-> hIncrBy ('user: 1000: message: notice ', 'comment', 1 ); # unread comments + 1 $ redis-> hGetAll ('user: 1000: message: notice '); # view the number of notifications for all messages Array ([system] => 2 [comment] => 2)
Use Redis as a message queue
Use the List Data Structure of Redis to implement distributed message queues