亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

項(xiàng)目中應(yīng)用Redis+Php的場(chǎng)景

 更新時(shí)間:2016年05月22日 11:19:07   作者:Julylovin  
Redis是一個(gè)開(kāi)源的使用ANSI C語(yǔ)言編寫(xiě)、支持網(wǎng)絡(luò)、可基于內(nèi)存亦可持久化的日志型、Key-Value數(shù)據(jù)庫(kù),并提供多種語(yǔ)言的API。今天我們來(lái)看下php結(jié)合redis的一些應(yīng)用場(chǎng)景

前言

一些案例中有的同學(xué)說(shuō)為什么不可以用string類(lèi)型,string類(lèi)型完全可以實(shí)現(xiàn)呀

我建議你看下我的專(zhuān)欄文章《Redis高級(jí)用法》,里面介紹了用hash類(lèi)型的好處

商品維度計(jì)數(shù)

對(duì)商品喜歡數(shù),評(píng)論數(shù),鑒定數(shù),瀏覽數(shù)進(jìn)行計(jì)數(shù)
說(shuō)起電商,肯定離不開(kāi)商品,而附帶商品有各種計(jì)數(shù)(喜歡數(shù),評(píng)論數(shù),鑒定數(shù),瀏覽數(shù),etc)
Redis的命令都是原子性的,你可以輕松地利用INCR,DECR等命令來(lái)計(jì)數(shù)。

采用Redis 的類(lèi)型: Hash. 如果你對(duì)redis數(shù)據(jù)類(lèi)型不太熟悉,可以參考
http://redis.io/topics/data-types-intro

為product定義個(gè)key product:,為每種數(shù)值定義hashkey, 譬如喜歡數(shù)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的商品相關(guān)信息
                  array('like_num '=> 1)

用戶(hù)維度計(jì)數(shù)

對(duì)用戶(hù)動(dòng)態(tài)數(shù)、關(guān)注數(shù)、粉絲數(shù)、喜歡商品數(shù)、發(fā)帖數(shù)等計(jì)數(shù)
用戶(hù)維度計(jì)數(shù)同商品維度計(jì)數(shù)都采用 Hash. 為User定義個(gè)key 為 user:
為每種數(shù)值定義hashkey, 譬如關(guān)注數(shù)follow

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

$redis->hGetAll('user:100000'); // 獲取uid為10000的用戶(hù)
                  array('like_num '=> 1)

                                
存儲(chǔ)社交關(guān)系

譬如將用戶(hù)的好友/粉絲/關(guān)注,可以存在一個(gè)sorted set中,score可以是timestamp
默認(rèn)集合按照score遞增排序
這樣求兩個(gè)人的共同好友的操作,可能就只需要用求交集命令即可

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

                #uid為1000用戶(hù)關(guān)注uid為1001 , score值設(shè)定時(shí)間戳1463557212

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

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

$redis->zAdd('user:2000:follow', 1463896964, '1003');
 
                #uid為2000用戶(hù)關(guān)注1001和1003用戶(hù) , score值設(shè)定時(shí)間戳

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

    #對(duì)集合'user:1000:follow'和'user:2000:follow'取交集'com_fllow:1000:2000'
    #獲得共同關(guān)注的uid 

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

用作緩存代替memcached

應(yīng)用于商品列表,評(píng)論列表,@提示列表

相對(duì)memcached 簡(jiǎn)單的key-value存儲(chǔ)來(lái)說(shuō),redis眾多的數(shù)據(jù)結(jié)構(gòu)(list,set,sorted set,hash,
etc)

可以更方便cache各種業(yè)務(wù)數(shù)據(jù),性能也不亞于memcached。
NOTE: RPUSH pagewviews.user: EXPIRE pagewviews.user: 60 //注意要update timeout

反spam系統(tǒng)

應(yīng)用系統(tǒng)評(píng)論、發(fā)布商品、論壇發(fā)貼的spam控制

作為一個(gè)電商網(wǎng)站被各種spam攻擊是少不免(垃圾評(píng)論、發(fā)布垃圾商品、廣告、刷自家商品排名等)

針對(duì)這些spam制定一系列anti-spam規(guī)則,其中有些規(guī)則可以利用redis做實(shí)時(shí)分析

譬如:1分鐘評(píng)論不得超過(guò)2次、5分鐘評(píng)論少于5次等(更多機(jī)制/規(guī)則需要結(jié)合drools )
常規(guī)sorted set將最近一天用戶(hù)操作記錄起來(lái)
(為什么不全部記錄?節(jié)省memory,全部操作會(huì)記錄到log,后續(xù)利用hadoop進(jìn)行更全面分析統(tǒng)計(jì))

#獲取5秒內(nèi)操作記錄
$res = $redis->zRangeByScore('user:1000:comment', time() - 5, time());
#判斷5秒內(nèi)不能評(píng)論
if (!$res) {
  $redis->zAdd('user:1000:comment', time(), '評(píng)論內(nèi)容');
} else {
  echo '5秒之內(nèi)不能評(píng)論';
}

#5秒內(nèi)評(píng)論不得超過(guò)2次
if($redis->zRangeByScore('user:1000:comment',time()-5 ,time())==1)
echo '5秒之內(nèi)不能評(píng)論2次';

#5秒內(nèi)評(píng)論不得少于2次

if(count($redis->zRangeByScore('user:1000:comment',time()-5 ,time()))<2)
echo '5秒之內(nèi)不能評(píng)論2次';


 

用戶(hù)Timeline/Feeds

應(yīng)用于關(guān)注的人、主題、品牌及專(zhuān)欄

redis在這邊主要當(dāng)作cache使用

$redis->zAdd('user:2000:feed:topic', time(), '13');
//score 為timestamp uid為2000的用戶(hù)關(guān)注tid為13的topic

$redis->expire('user:2000:feed:topic',24*60*60);
#關(guān)注有效期為24小時(shí)
# ttl 30天之內(nèi)按秒數(shù)計(jì)算 30天之外以timestamp為準(zhǔn)

最新列表&排行榜

用于記錄用戶(hù)剛剛喜歡的商品最新列表or排行榜 等業(yè)務(wù)場(chǎng)景

商品最新列表-sorted set結(jié)構(gòu)呈現(xiàn)

    $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); 
    #默認(rèn)喜歡時(shí)間升序序排列
    #
      Array(
        [3002] => 1463565179
        [3001] => 1463565189
        [3004] => 1463565199
        [3003] => 1463565209
      )
    
    $redis->zRevRange('user:1000:product:like', 0, -1,true); 
    #以喜歡時(shí)間降序排列
    #
      Array
      (
        [3003] => 1463565424
        [3004] => 1463565414
        [3001] => 1463565404
        [3002] => 1463565394
      )
    

排行榜-list數(shù)據(jù)結(jié)構(gòu)呈現(xiàn)

    $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結(jié)構(gòu)對(duì)消息通知業(yè)務(wù)場(chǎng)景計(jì)數(shù)

$redis->hSet('user:1000:message:notice', 'system', 1);
#設(shè)置1條未讀系統(tǒng)消息

$redis->hIncrBy('user:1000:message:notice', 'system', 1);
#未讀系統(tǒng)消息+1

$redis->hSet('user:1000:message:notice', 'comment', 1);
#設(shè)置1條未讀評(píng)論

$redis->hIncrBy('user:1000:message:notice', 'comment', 1);
#未讀評(píng)論+1

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

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



將Redis用作消息隊(duì)列

采用Redis的List數(shù)據(jù)結(jié)構(gòu)實(shí)現(xiàn)分布式的消息隊(duì)列

相關(guān)文章

最新評(píng)論