《PHP實(shí)戰(zhàn):項(xiàng)目中應(yīng)用Redis+Php的場景》要點(diǎn):
本文介紹了PHP實(shí)戰(zhàn):項(xiàng)目中應(yīng)用Redis+Php的場景,希望對您有用。如果有疑問,可以聯(lián)系我們。
前言
PHP教程
一些案例中有的同學(xué)說為什么弗成以用string類型,string類型完全可以實(shí)現(xiàn)呀PHP教程
我建議你看下我的專欄文章《Redis高級(jí)用法》,里面介紹了用hash類型的好處
PHP教程
商品維度計(jì)數(shù)
PHP教程
對商品喜歡數(shù),評(píng)論數(shù),鑒定數(shù),瀏覽數(shù)進(jìn)行計(jì)數(shù)
說起電商,肯定離不開商品,而附帶商品有各種計(jì)數(shù)(喜歡數(shù),評(píng)論數(shù),鑒定數(shù),瀏覽數(shù),etc)
Redis的命令都是原子性的,你可以輕松地利用INCR,DECR等命令來計(jì)數(shù).PHP教程
采用Redis 的類型: Hash. 如果你對redis數(shù)據(jù)類型不太熟悉,可以參考
http://redis.io/topics/data-types-introPHP教程
為product定義個(gè)key product:,為每種數(shù)值定義hashkey, 譬如喜歡數(shù)like_num
PHP教程
$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)
用戶維度計(jì)數(shù)
PHP教程
對用戶動(dòng)態(tài)數(shù)、關(guān)注數(shù)、粉絲數(shù)、喜歡商品數(shù)、發(fā)帖數(shù)等計(jì)數(shù)
用戶維度計(jì)數(shù)同商品維度計(jì)數(shù)都采用 Hash. 為User定義個(gè)key 為 user:
為每種數(shù)值定義hashkey, 譬如關(guān)注數(shù)follow
PHP教程
$redis->hSet('user:100000', 'follow ', 5); // 添加uid為10000的用戶follow 為5 $redis->hIncrBy('user:100000', 'follow ', 1); // 更新uid為10000的用戶follow +1 $redis->hGetAll('user:100000'); // 獲取uid為10000的用戶 array('like_num '=> 1)
????????????????????????????????
存儲(chǔ)社交關(guān)系
PHP教程
譬如將用艫暮糜/粉絲/關(guān)注,可以存在一個(gè)sorted set中,score可以是timestamp
默認(rèn)集合依照score遞增排序
這樣求兩個(gè)人的共同好友的操作,可能就只需要用求交集命令即可
PHP教程
$redis->zAdd('user:1000:follow', 1463557212, '1001'); #uid為1000用戶關(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用戶關(guān)注1001和1003用戶 , score值設(shè)定時(shí)間戳 $redis->zInter('com_fllow:1000:2000', array('user:1000:follow', 'user:2000:follow')); #對集合'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
PHP教程
應(yīng)用于商品列表,評(píng)論列表,@提示列表PHP教程
相對memcached 簡單的key-value存儲(chǔ)來說,redis眾多的數(shù)據(jù)布局(list,set,sorted set,hash,
etc)PHP教程
可以更便利cache各種業(yè)務(wù)數(shù)據(jù),性能也不亞于memcached.
NOTE: RPUSH pagewviews.user: EXPIRE pagewviews.user: 60 //注意要update timeout
PHP教程
反spam系統(tǒng)
PHP教程
應(yīng)用系統(tǒng)評(píng)論、發(fā)布商品、論壇發(fā)貼的spam控制PHP教程
作為一個(gè)電商網(wǎng)站被各種spam攻擊是少難免(垃圾評(píng)論、發(fā)布垃圾商品、廣告、刷自家商品排名等)PHP教程
針對這些spam制定一系列anti-spam規(guī)則,其中有些規(guī)則可以利用redis做實(shí)時(shí)分析PHP教程
譬如:1分鐘評(píng)論不得超過2次、5分鐘評(píng)論少于5次等(更多機(jī)制/規(guī)則必要結(jié)合drools )
常規(guī)sorted set將最近一天用戶操作記錄起來
(為什么不全部記錄?節(jié)省memory,全部操作會(huì)記錄到log,后續(xù)利用hadoop進(jìn)行更全面分析統(tǒng)計(jì))
PHP教程
#獲取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)論不得超過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次';
用戶Timeline/Feeds
PHP教程
應(yīng)用于關(guān)注的人、主題、品牌及專欄PHP教程
redis在這邊主要當(dāng)作cache使用PHP教程
$redis->zAdd('user:2000:feed:topic', time(), '13'); //score 為timestamp uid為2000的用戶關(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)
最新列表&排行榜
PHP教程
用于記錄用戶剛剛喜歡的商品最新列表or排行榜 等業(yè)務(wù)場景PHP教程
商品最新列表-sorted set布局呈現(xiàn)PHP教程
$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ù)布局呈現(xiàn)PHP教程
$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 )
消息通知
PHP教程
采用Hash布局對消息通知業(yè)務(wù)場景計(jì)數(shù)
PHP教程
$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ì)列
PHP教程
采用Redis的List數(shù)據(jù)布局實(shí)現(xiàn)分布式的消息隊(duì)列PHP教程
轉(zhuǎn)載請注明本頁網(wǎng)址:
http://www.snjht.com/jiaocheng/6532.html