Objective
Some students in some cases say why you can't use string type, string type can be completely implemented AH
I suggest you take a look at my column, "Redis Advanced Usage," which describes the benefits of using the hash type
Commodity dimension Count
Count the number of items, comments, numbers, number of views
Talking about the electricity quotient, must be inseparable from the commodity, and the incidental goods have various counts (like number, comment number, appraisal number, browse number, etc)
Redis commands are atomic, and you can easily count them by using commands such as INCR,DECR.
Use Redis type: Hash. If you are not familiar with the Redis data type, you can refer to
Http://redis.io/topics/data-types-intro
Define key product for product: Define HashKey for each numeric value, for example, like a few like_num
$redis->hset (' product:123 ', ' Like_num ', 5); Add a product with ID 123 like_num 5
$redis->hincrby (' product:123 ', ' Like_num ', 1);//Add a product with ID 123 like_num +1
$redis- >hgetall (' product:123 '); Get product-related information for ID 123
Array (' Like_num ' => 1)
User Dimension Count
Count the number of users, the number of followers, the number of fans, the 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 numeric value, such as the number of concerns follow
$redis->hset (' user:100000 ', ' follow ', 5); Users who add a UID of 10000 follow 5
$redis->hincrby (' user:100000 ', ' follow ', 1);//update UID 10000 user follow +1
$redis- >hgetall (' user:100000 '); Gets the user array with a UID of 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
So for two of people's common friend operation, you may just need to find the intersection command
$redis->zadd (' User:1000:follow ', 1463557212, ' 1001 ');
#uid为1000用户关注uid为1001, score value set 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 is set to 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); Gets all set elements
#array (' 10001 ', ' 10002 ')
Used as a cache instead of memcached
Apply to Product list, comment list, @ hint list
Relatively memcached simple key-value storage, redis a large number of data structures (list,set,sorted Set,hash,
etc
Can be more convenient cache a variety of 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 of Application system review, publishing products, forum posts
As an electric dealer site by a variety of spam attacks are less unavoidable (spam comments, release of junk goods, advertising, brush their own commodity rankings, etc.)
A series of anti-spam rules are developed for these spam, some of which can be used for real time analysis using Redis
For example: 1 minutes review must not exceed 2 times, 5 minutes comment less than 5 times, etc. (more mechanisms/rules need to be combined with drools)
General sorted set records the most recent day user action
(Why not all records?) Save memory, all operations logged to log, followed by 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 {
echo ' No comment within 5 seconds ';
}
#5秒内评论不得超过2次
if ($redis->zrangebyscore (' User:1000:comment ', Time ()-5, Time ()) ==1)
Echo ' within 5 seconds cannot comment 2 times ';
#5秒内评论不得少于2次
if ($redis->zrangebyscore (' User:1000:comment ', Time ()-5, Time ())) <2)
Echo ' Cannot comment 2 times within 5 seconds ';
User Timeline/feeds
People, themes, brands, and columns that should be used for attention
Redis on this side mainly as cache use
$redis->zadd (' User:2000:feed:topic ', Time (), ' a ');
Score for the timestamp UID of 2000 of the users concerned about the TID 13 of the topic
$redis->expire (' User:2000:feed:topic ', 24*60*60);
#关注有效期为24小时
# TTL within 30 days in seconds count 30 days timestamp
Latest Listings & Rankings
A business scenario that records the latest listings or charts of items that users just like
Product Latest list-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
)
List-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
Message notification business scenario count with hash structure
$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
)
Using Redis as a message queue
Using Redis list data structure to realize distributed message queue