Redis 绉掓潃瀹炴垬
Posted Java缂栫▼绮鹃€?/a>
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Redis 绉掓潃瀹炴垬相关的知识,希望对你有一定的参考价值。
Python瀹炴垬绀剧兢
Java瀹炴垬绀剧兢
闀挎寜璇嗗埆涓嬫柟浜岀淮鐮侊紝鎸夐渶姹傛坊鍔?/span>
鎵爜鍏虫敞娣诲姞瀹㈡湇
杩汸ython绀剧兢鈻?/span>
鎵爜鍏虫敞娣诲姞瀹㈡湇
杩汮ava绀剧兢鈻?/span>
浣滆€呬辅闄堝溅鏂?nbsp;
鏉ユ簮涓↗ava寤鸿鑰?/span>
https://www.cnblogs.com/chenyanbin/p/13587508.html
涓哄暐鍐欒繖涓井淇℃姠绾㈠寘椤圭洰鍛紝鍏徃 0202 骞?08 鏈?22 鏃ワ紝鍏徃鍛ㄥ勾搴嗭紝鎶簡100澶氱孩鍖咅煣э紝O(鈭鈭?O鍝堝搱~
涓氬姟娴佺▼鍒嗘瀽
鍔熻兘鎷嗚В
鏂板缓绾㈠寘
鍦?DB銆丷edis 鍒嗗埆鏂板涓€鏉¤褰?/p>
鎶㈢孩鍖?骞跺彂)
銆屼娇鐢ㄦ妧鏈€?/strong>
Redis 涓暟鎹被鍨嬬殑 String 鐗规€х殑鍘熷瓙閫掑噺锛圖ECR key锛夊拰鍑忓皯鎸囧畾鍊硷紙DECRBY key decrement锛?/p>
銆屼笟鍔°€?/strong>
-
璇锋眰 Redis 锛屽綋鍓╀綑绾㈠寘涓暟澶т簬 0锛岀孩鍖呬釜鏁板師瀛愰€掑噺锛岄殢鏈鸿幏鍙栫孩鍖? -
璁$畻閲戦锛屽綋鏈€鍚庝竴涓孩鍖呮椂锛屾渶鍚庝竴涓孩鍖呴噾棰?鎬婚噾棰?鎬诲凡鎶㈢孩鍖呴噾棰? -
鏇存柊鏁版嵁搴?
銆屾煡璇㈢孩鍖呰褰曘€?/strong>
鏌ヨ DB 鍗冲彲
鏁版嵁搴撹璁?/span>
绾㈠寘娴佹按琛?/p>
CREATE TABLE `red_packet_info` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`red_packet_id` bigint(11) NOT NULL DEFAULT 0 COMMENT '绾㈠寘id锛岄噰饨?br>timestamp+5浣嶉殢鏈烘暟',
`total_amount` int(11) NOT NULL DEFAULT 0 COMMENT '绾㈠寘鎬烩睛棰濓紝鍗曚綅鍒?,
`total_packet` int(11) NOT NULL DEFAULT 0 COMMENT '绾㈠寘鎬讳釜鏁?,
`remaining_amount` int(11) NOT NULL DEFAULT 0 COMMENT '鍓╀綑绾㈠寘饩﹂锛屽崟浣?br>鍒?,
`remaining_packet` int(11) NOT NULL DEFAULT 0 COMMENT '鍓╀綑绾㈠寘涓暟',
`uid` int(20) NOT NULL DEFAULT 0 COMMENT '鏂板缓绾㈠寘饨ゆ埛鐨勨饯鎴锋爣璇?,
`create_time` timestamp COMMENT '鍒涘缓鏃堕棿',
`update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE
CURRENT_TIMESTAMP COMMENT '鏇存柊鏃堕棿',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COMMENT='绾㈠寘淇℃伅
琛紝鏂板缓饧€涓孩鍖呮彃饧娾紑鏉¤褰?;
绾㈠寘璁板綍琛?/p>
CREATE TABLE `red_packet_record` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`amount` int(11) NOT NULL DEFAULT '0' COMMENT '鎶㈠埌绾㈠寘鐨勨睛棰?,
`nick_name` varchar(32) NOT NULL DEFAULT '0' COMMENT '鎶㈠埌绾㈠寘鐨勨饯鎴风殑饨ゆ埛
鍚?,
`img_url` varchar(255) NOT NULL DEFAULT '0' COMMENT '鎶㈠埌绾㈠寘鐨勨饯鎴风殑澶村儚',
`uid` int(20) NOT NULL DEFAULT '0' COMMENT '鎶㈠埌绾㈠寘饨ゆ埛鐨勨饯鎴锋爣璇?,
`red_packet_id` bigint(11) NOT NULL DEFAULT '0' COMMENT '绾㈠寘id锛岄噰饨?br>timestamp+5浣嶉殢鏈烘暟',
`create_time` timestamp COMMENT '鍒涘缓鏃堕棿',
`update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE
CURRENT_TIMESTAMP COMMENT '鏇存柊鏃堕棿',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COMMENT='鎶㈢孩鍖呰
褰曡〃锛屾姠饧€涓孩鍖呮彃饧娾紑鏉¤褰?;
鍙戠孩鍖?API
鍙戠孩鍖呮帴鍙e紑鍙?/p>
-
鏂板涓€鏉$孩鍖呰褰? -
寰€ mysql 閲岄潰娣诲姞涓€鏉$孩鍖呰褰? -
寰€ redis 閲岄潰娣诲姞涓€鏉$孩鍖呮暟閲忚褰? -
寰€redis閲岄潰娣诲姞涓€鏉$孩鍖呴噾棰濊褰?
鉂?/span>寰€db涓氨鍗曠函瀛樺叆涓€鏉¤褰曪紝Service灞傚拰Mapper灞傦紝灏辩畝鍗曠殑涓€鏉ql璇彞锛屼富瑕佹槸鎻愪緵鎬濊矾锛屼笅闈細闄勬渚嬫簮鐮侊紝涓嶈鎱?/p> 鉂?/span>
鎶㈢孩鍖?API
-
鎶㈢孩鍖呭姛鑳藉睘浜庡師瀛愬噺鎿嶄綔 -
褰撳ぇ灏忓皬浜?0 鏃跺師瀛愬噺澶辫触 -
褰撶孩鍖呬釜鏁颁负0鏃讹紝鍚庨潰杩涙潵鐨勭敤鎴峰叏閮ㄦ姠绾㈠寘澶辫触锛屽苟涓嶄細杩涘叆鎷嗙孩鍖呯幆鑺? -
鎶㈢孩鍖呭姛鑳借璁? -
灏嗙孩鍖匢D鐨勮姹傛斁鍏ヨ姹傞槦鍒椾腑锛屽鏋滃彂鐜拌秴杩囩孩鍖呯殑涓暟锛岀洿鎺ヨ繑鍥? -
娉ㄦ剰浜嬮」 -
鎶㈠埌绾㈠寘涓嶄竴瀹氳兘鎷嗘垚鍔?
鎶㈢孩鍖呯畻娉曟媶瑙?/span>
閫氳繃涓婂浘绠楁硶寰楀嚭锛岄潬鍓嶉潰鐨勪汉锛屾墜姘旀渶浣冲嚑鐜囧皬锛屾墜姘旀渶浣筹紝寰€寰€鍦ㄥ悗闈?/p>
-
鍙?100 鍏冿紝鍏?10 涓孩鍖咃紝閭d箞骞冲潎鍊兼槸 10 鍏冧竴涓紝閭d箞鍙戝嚭鏉ョ殑绾㈠寘閲戦鍦?0.01~20 鍏冧箣闂存尝鍔? -
褰撳墠闈?4 涓孩鍖呮€诲叡琚浜?30 鍏冩椂锛屽墿涓?70 鍏冿紝鎬诲叡 6 涓孩鍖咃紝閭d箞杩?6 涓孩鍖呯殑閲戦鍦?0.01~23.3 鍏冧箣闂存尝鍔?
鎶㈢孩鍖呮帴鍙e紑鍙?/span>
銆屾祴璇曘€?/strong>
銆屽彂绾㈠寘銆?/strong>
妯℃嫙楂樺苟鍙戞姠绾㈠寘(Jmeter鍘嬫祴宸ュ叿)
鍥犱负鎴戝彂浜?10 涓孩鍖咃紝閲戦鏄?20000锛屼娇鐢ㄥ帇娴嬪伐鍏凤紝妯℃嫙50涓姹傦紝鍙厑璁稿墠10涓姹傝兘鎶㈠埌绾㈠寘锛屽苟涓旈噾棰濈瓑浜?0000銆?/p>
甯冮殕杩囨护鍣?/span>
浠嬬粛
甯冮殕杩囨护鍣ㄦ槸1970骞寸敱甯冮殕鎻愬嚭鐨勩€傚畠瀹為檯涓婃槸涓€涓緢闀跨殑浜岃繘鍒跺悜閲忓拰涓€绯诲垪闅忔満鏄犲皠鍑芥暟銆傚竷闅嗚繃婊ゅ櫒鍙互鐢ㄤ簬妫€绱竴涓厓绱犳槸鍚﹀湪涓€涓泦鍚堜腑銆傚畠鐨勪紭鐐规槸绌洪棿鏁堢巼鍜屾煡璇㈡椂闂撮兘杩滆繙瓒呰繃涓€鑸殑绠楁硶锛岀己鐐规槸鏈変竴瀹氱殑璇瘑鍒巼鍜屽垹闄ゅ洶闅俱€?/p>
浼樼偣
鐩告瘮浜庡叾浠栫殑鏁版嵁缁撴瀯锛屽竷闅嗚繃婊ゅ櫒鍦ㄧ┖闂村拰鏃堕棿鏂归潰閮芥湁宸ㄥぇ鐨勪紭鍔裤€傚竷闅嗚繃婊ゅ櫒瀛樺偍绌洪棿鍜屾彃鍏?鏌ヨ鏃堕棿閮芥槸甯告暟銆傚彟澶栦笁鍒楀嚱鏁扮浉浜掍箣闂存病鏈夊叧绯伙紝鏂逛究鐢辩‖浠跺苟琛屽疄鐜般€傚竷闅嗚繃婊ゅ櫒涓嶉渶瑕佸瓨鍌ㄥ厓绱犳湰韬紝鍦ㄦ煇浜涘淇濆瘑瑕佹眰闈炲父涓ユ牸鐨勫満鍚堟湁浼樺娍銆?/p>
缂虹偣
浣嗘槸甯冮殕杩囨护鍣ㄧ殑缂虹偣鍜屾湁鐐逛竴鏍锋槑鏄俱€傝绠楃巼鏄叾涓箣涓€銆傞殢鐫€瀛樺叆鐨勫厓绱犳暟閲忓鍔狅紝璇畻鐜囬殢涔嬪鍔犮€備絾鏄鏋滃厓绱犳暟閲忓お灏戯紝鍒欎娇鐢ㄦ暎鍒楄〃瓒崇煟銆?/p>
甯冮殕杩囨护鍣ㄦ湁浠€涔堢敤
-
榛戝娴侀噺鏀诲嚮锛氭晠鎰忚闂笉瀛樺湪鐨勬暟鎹紝瀵艰嚧鏌ョ▼搴忎笉鏂闂瓺B鐨勬暟鎹? -
榛戝瀹夊叏闃绘埅锛氬綋榛戝璁块棶涓嶅瓨鍦ㄧ殑缂撳瓨鏃惰繀閫熻繑鍥為伩鍏嶇紦瀛樺強DB鎸傛帀 -
缃戦〉鐖櫕瀵?URL 鐨勫幓閲嶏紝閬垮厤鐖彇鐩稿悓鐨刄RL鍦板潃 -
鍙嶅瀮鍦鹃偖浠讹紝浠庢暟鍗佷嚎涓瀮鍦鹃偖浠跺垪琛ㄤ腑鍒ゆ柇鏌愰偖浠舵槸鍚﹀瀮鍦鹃偖浠?鍚岀悊锛屽瀮鍦剧煭淇? -
缂撳瓨鍑荤┛锛屽皢宸插瓨鍦ㄧ殑缂撳瓨鏀惧埌甯冮殕涓紝褰撻粦瀹㈣闂笉瀛樺湪鐨勭紦瀛樻椂杩呴€熻繑鍥為伩鍏嶇紦瀛樺強 DB 鎸傛帀
甯冮殕杩囨护鍣ㄥ疄鐜颁細鍛樿浆鐩樻娊濂?/span>
闇€姹?/span>
涓€涓娊濂栫▼搴忥紝鍙拡瀵逛細鍛樼敤鎴锋湁鏁?/p>
閫氳繃google甯冮殕杩囨护鍣ㄥ瓨鍌ㄤ細鍛樻暟鎹?/span>
-
绋嬪簭鍚姩鏃跺皢鏁版嵁鏀惧叆鍐呭瓨涓? -
google鑷姩鍒涘缓甯冮殕杩囨护鍣? -
鐢ㄦ埛ID杩涙潵涔嬪悗鍒ゆ柇鏄惁鏄細鍛?
浠g爜瀹炵幇
寮曞叆渚濊禆
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>29.0-jre</version>
</dependency>
鏁版嵁搴撲細鍛樿〃
CREATE TABLE `sys_user` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`user_name` varchar(11) CHARACTER SET utf8mb4 DEFAULT NULL COMMENT '饨ゆ埛鍚?,
`image` varchar(11) CHARACTER SET utf8mb4 DEFAULT NULL COMMENT '饨ゆ埛澶村儚',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8;
鍒濆鍖栧竷闅嗚繃婊ゅ櫒
dao 灞傚拰 dao 鏄犲皠鏂囦欢锛屽氨鍗曠函鐨勪竴涓?sql 鏌ヨ锛岀湅鏍稿績鏂规硶锛屼笅闈細闄勬簮鐮佹淮锛屼笉瑕佹厡濂藉槢
鎺у埗灞?/span>
娴嬭瘯
缂虹偣
-
鍐呭瓨绾у埆浜ч儴 -
閲嶅惎鍗冲け鏁? -
鏈湴鍐呭瓨鏃犳硶鐢ㄥ湪鍒嗗竷寮忓満鏅? -
涓嶆敮鎸佸ぇ鏁版嵁閲忓瓨鍌?
Redis甯冮殕杩囨护鍣?/span>
浼樼偣
-
鍙墿灞曟€?Bloom 杩囨护鍣? -
涓嶅瓨鍦ㄩ噸鍚嵆澶辨晥鎴栧畾鏃朵换鍔$淮鎶ょ殑鎴愭湰
缂虹偣
-
闇€瑕佺綉缁淚O锛屾€ц兘姣斿熀浜庡唴瀛樼殑杩囨护鍣ㄤ綆
甯冮殕杩囨护鍣ㄥ畨瑁?/span>
銆屼笅杞姐€?/strong>
github锛歨ttps://github.com/RedisBloom/RedisBloom
閾炬帴: https://pan.baidu.com/s/16DlKLm8WGFzGkoPpy8y4Aw 瀵嗙爜: 25w1
銆岀紪璇戙€?/strong>
make
銆屽皢 Rebloom 鍔犺浇鍒?Redis 涓€?/strong>
鍏堟妸 Redis 缁欏仠鎺夛紒锛侊紒鍦?redis.conf 閲岄潰娣诲姞涓€琛屽懡浠?>鍔犺浇妯″潡
loadmodule /usr/soft/RedisBloom-2.2.4/redisbloom.so
銆屾祴璇曞竷闅嗚繃婊ゅ櫒銆?/strong>
SpringBoot 鏁村悎 Redis 甯冮殕杩囨护鍣?/span>
缂栧啓涓や釜lua鑴氭湰
-
娣诲姞鏁版嵁鍒版寚瀹氬悕绉扮殑甯冮殕杩囨护鍣? -
浠庢寚瀹氬悕绉扮殑甯冮殕杩囨护鍣ㄨ幏鍙杒ey鏄惁瀛樺湪鐨勮剼鏈?
local bloomName = KEYS[1]
local value = KEYS[2]
--bloomFilter
local result_1 = redis.call('BF.ADD',bloomName,value)
return result_1
local bloomName = KEYS[1]
local value = KEYS[2]
--bloomFilter
local result_1 = redis.call('BF.EXISTS',bloomName,value)
return result_1
鍦?RedisService.java 涓坊鍔?2 涓柟娉?/span>
楠岃瘉
绉掓潃
绉掓潃涓氬姟娴佺▼鍥?/span>
鏁版嵁钀藉湴瀛樺偍鏂规
-
閫氳繃鍒嗗竷寮弐edis鍑忓簱瀛? -
DB瀛樻渶缁堣鍗曚俊鎭暟鎹?
API鎬ц兘璋冧紭
-
鎬ц兘鐡堕鍦ㄩ珮骞跺彂绉掓潃 -
鎶€鏈毦棰樺湪浜庤秴鍗栭棶棰?
瀹炵幇姝ラ
鎻愬墠灏嗙鏉€鏁版嵁缂撳瓨鍒?redis
set skuId_start_1 0_1554045087 --绉掓潃鏍囪瘑
set skuId_access_1 12000 --鍏佽鎶㈣喘鏁?br>set skuId_count_1 0 --鎶㈣喘璁℃暟
set skuId_booked_1 0 --鐪熷疄绉掓潃鏁?br>
-
绉掓潃寮€濮嬪墠锛宻kuId_start涓?锛屼唬琛ㄦ椿鍔ㄦ湭寮€濮? -
褰搒kuId_start鏀逛负1鏃讹紝娲诲姩寮€濮嬶紝寮€濮嬬鏉€鍙? -
褰撴帴鍙椾笅鍗曟暟杈惧埌sku_count*1.2鍚庯紝缁х画鎷︽埅鎵€鏈夎姹傦紝鍟嗗搧鍓╀綑鏁伴噺涓?(涓哄暐鎺ュ彈鎶㈣喘鏁颁负1涓?鍛紝鐪嬩笟鍔℃祦绋嬪浘锛屾秹鍙婂埌鈥滄牎楠岃鍗曚俊鎭€濓紝涓€鑸缃殑鍊艰姣旀€绘暟澶氫竴鐐癸紝澶氬灏戣嚜宸卞畾)
鍒╃敤 Redis 缂撳瓨鍔犻€熷搴撳瓨鏁?/p>
"skuId_booked":10000 //浠?寮€濮嬬疮鍔狅紝绉掓潃鐨勪釜鏁板彧鑳藉姞鍒?涓?br>
灏嗙敤鎴疯鍗曟暟鎹啓鍏?MQ(寮傛鏂瑰紡)銆?/p>
鍙﹀涓€鍙版湇鍔″櫒鐩戝惉 mq锛屽皢璁㈠崟淇℃伅鍐欏叆鍒?DB銆?/p>
濂戒簡锛屼互涓婂氨鏄畬鏁寸殑寮€鍙戞楠わ紝涓嬮潰鎴戜滑寮€濮嬬紪鍐欎唬鐮?/p>
浠g爜瀹炴垬
缃戝叧娴忚鎷︽埅灞?/span>
1銆佸厛鍒ゆ柇绉掓潃鏄惁宸茬粡寮€濮?/p>
2銆佸埄鐢?Redis 缂撳瓨 incr 鎷︽埅娴侀噺
-
鐢?incr 鏂规硶鍘熷瓙鍔? -
閫氳繃鍘熷瓙鍔犲笗鍔崟褰撳墠 skuId_access 鏄惁杈惧埌鏈€澶у€?
璁㈠崟淇℃伅鏍¢獙灞?/span>
1銆佹牎楠屽綋鍓嶇敤鎴锋槸鍚﹀凡缁忎拱杩囪繖涓晢鍝?/p>
-
闇€瑕佸瓨鍌ㄧ敤鎴风殑uid -
瀛樻暟鎹簱鏁堢巼澶綆 -
瀛楻edis value鏂瑰紡鏁版嵁澶ぇ -
瀛樺竷闅嗚繃婊ゅ櫒鎬ц兘楂樹笖鏁版嵁閲忓皬(鎺ㄨ崘)
2銆佹牎楠岄€氳繃鐩存帴杩斿洖鎶㈣喘鎴愬姛
寮€鍙憀ua鑴氭湰瀹炵幇搴撳瓨鎵i櫎
1銆佸簱瀛樻墸闄ゆ垚鍔燂紝鑾峰彇褰撳墠鏈€鏂板簱瀛?/p>
2銆佸鏋滃簱瀛樺ぇ浜?锛屽嵆椹笂杩涜搴撳瓨鎵i櫎锛屽苟涓旇闂姠璐垚鍔熺粰鐢ㄦ埛
3銆佽€冭檻鍘熷瓙鎬ч棶棰?/p>
-
淇濊瘉鍘熷瓙鎬х殑鏂瑰紡锛岄噰鐢?lua 鑴氭湰 -
閲囩敤lua鑴氭湰鏂瑰紡淇濊瘉鍘熷瓙鎬у甫鏉ョ己鐐癸紝鎬ц兘鏈夋墍涓嬮檷 -
涓嶄繚璇佸師瀛愭€х己鐐癸紝鏀惧叆璇锋眰閲忓彲鑳藉ぇ浜庨鏈? -
褰撳墠鎵i櫎搴撳瓨鍦烘櫙蹇呴』淇濊瘉鍘熷瓙鎬э紝鍚﹀垯浼氬鑷磋秴鍗?
4銆佽繑鍥炴姠璐粨鏋?/p>
-
鎶㈣喘鎴愬姛 -
搴撳瓨娌′簡锛屾姠璐け璐?
鎺у埗灞?/span>
Service 灞?/span>
甯冮殕杩囨护鍣?/span>
鍒濆鍖杛edis缂撳瓨
set skuId_start_1 0_1554045087 --绉掓潃鏍囪瘑
set skuId_access_1 12000 --鍏佽鎶㈣喘鏁?br>set skuId_count_1 0 --鎶㈣喘璁℃暟
set skuId_booked_1 0 --鐪熷疄绉掓潃鏁?br>
绉掓潃楠岃瘉
jmeter 閰嶇疆
鍘嬫祴绉掓潃楠岃瘉鍘熷瓙鎬?/p>
椤圭洰涓嬭浇
閾炬帴: https://pan.baidu.com/s/1hZUPRAljkqO05fYluqJBhQ 瀵嗙爜: 1iwr
灏惧0
婕旂ず鐨勬椂鍊欙紝鎴戜娇鐢ㄧ殑 Redis 鍗曟満鐨勶紝鍚炲悙閲忎笉鏄緢澶э紝鎰熷叴瓒g殑锛屽彲浠ヨ嚜宸辨惌寤轰釜 Redis 涓讳粠澶嶅埗+鍝ㄥ叺+闆嗙兢锛岀劧鍚庡啀娴嬭瘯銆?/p>
鏈€杩戞瘮杈冨繖锛屾病鏃堕棿瀹屽杽寰俊鎶㈢孩鍖呯鏉€鐨勫師瀛愭€с€備笅闈㈤偅涓畬鏁存渚嬫姠搴撳瓨鐨勶紝浜茶嚜浣跨敤 Jmeter 鍘嬫祴鍑犳锛屾槸鍘熷瓙鎬х殑锛屽彲浠ユ嬁鏉ュ€熼壌锛屾劅鍏磋叮鐨勫悓瀛︼紝鍙互鍊熼壌涓嬮潰鎶㈠簱瀛樼殑浠g爜锛屾妸寰俊鎶㈢孩鍖呯殑鍔熻兘鍦ㄥ畬鍠勪笅锛屾垜灏变笉淇敼鍟︺€?/p>
杩戞湡绮惧僵鍐呭鎺ㄨ崘锛?/strong> 鍦ㄧ湅鐐硅繖閲?/strong>濂芥枃鍒嗕韩缁欐洿澶氫汉鈫撯啌
以上是关于Redis 绉掓潃瀹炴垬的主要内容,如果未能解决你的问题,请参考以下文章