2020闃块噷宸村反瀹樻柟鏈€鏂癛edis寮€鍙戣鑼冿紒
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了2020闃块噷宸村反瀹樻柟鏈€鏂癛edis寮€鍙戣鑼冿紒相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/%e6%95%b0%e6%8d%ae%e7%b1%bb%e5%9e%8b' title='鏁版嵁绫诲瀷'>鏁版嵁绫诲瀷
new memory counter ring 涓嶈兘 鍖呭惈鏈枃涓昏浠嬬粛鍦ㄤ娇鐢ㄩ樋閲屼簯Redis鐨勫紑鍙戣鑼冿紝浠庝笅闈㈠嚑涓柟闈㈣繘琛岃鏄庛€?/p>
- 閿€艰璁?/li>
- 鍛戒护浣跨敤
- 瀹㈡埛绔娇鐢?/li>
- 鐩稿叧宸ュ叿
閫氳繃鏈枃鐨勪粙缁嶅彲浠ュ噺灏戜娇鐢≧edis杩囩▼甯︽潵鐨勯棶棰樸€?/p>
1銆乲ey鍚嶈璁?/h3>
鍙鎬у拰鍙鐞嗘€?/strong>
浠ヤ笟鍔″悕(鎴栨暟鎹簱鍚?涓哄墠缂€(闃叉key鍐茬獊)锛岀敤鍐掑彿鍒嗛殧锛屾瘮濡備笟鍔″悕:琛ㄥ悕:id
ugc:video:1
绠€娲佹€?/strong>
淇濊瘉璇箟鐨勫墠鎻愪笅锛屾帶鍒秌ey鐨勯暱搴︼紝褰搆ey杈冨鏃讹紝鍐呭瓨鍗犵敤涔熶笉瀹瑰拷瑙嗭紝渚嬪锛?/p>
user:{uid}:friends:messages:{mid}绠€鍖栦负u:{uid}:fr:m:{mid}銆?
涓嶈鍖呭惈鐗规畩瀛楃
鍙嶄緥锛氬寘鍚┖鏍笺€佹崲琛屻€佸崟鍙屽紩鍙蜂互鍙婂叾浠栬浆涔夊瓧绗?/p>
2銆乿alue璁捐
鎷掔粷bigkey
闃叉缃戝崱娴侀噺銆佹參鏌ヨ锛宻tring绫诲瀷鎺у埗鍦?0KB浠ュ唴锛宧ash銆乴ist銆乻et銆亃set鍏冪礌涓暟涓嶈瓒呰繃5000銆?/p>
鍙嶄緥锛氫竴涓寘鍚?00涓囦釜鍏冪礌鐨刲ist銆?/p>
闈炲瓧绗︿覆鐨刡igkey锛屼笉瑕佷娇鐢╠el鍒犻櫎锛屼娇鐢╤scan銆乻scan銆亃scan鏂瑰紡娓愯繘寮忓垹闄わ紝鍚屾椂瑕佹敞鎰忛槻姝igkey杩囨湡鏃堕棿鑷姩鍒犻櫎闂(渚嬪涓€涓?00涓囩殑zset璁剧疆1灏忔椂杩囨湡锛屼細瑙﹀彂del鎿嶄綔锛岄€犳垚闃诲锛岃€屼笖璇ユ搷浣滀笉浼氫笉鍑虹幇鍦ㄦ參鏌ヨ涓?latency鍙煡))锛屾煡鎵炬柟娉曞拰鍒犻櫎鏂规硶
閫夋嫨閫傚悎鐨勬暟鎹被鍨?/p>
渚嬪锛氬疄浣撶被鍨?瑕佸悎鐞嗘帶鍒跺拰浣跨敤鏁版嵁缁撴瀯鍐呭瓨缂栫爜浼樺寲閰嶇疆,渚嬪ziplist锛屼絾涔熻娉ㄦ剰鑺傜渷鍐呭瓨鍜屾€ц兘涔嬮棿鐨勫钩琛?
鍙嶄緥锛?/p>
set user:1:name tom
set user:1:age =19
set user:1:favor football
姝d緥锛?/p>
hmset user:1 name tom age =19 favor football
鎺у埗key鐨勭敓鍛藉懆鏈?/strong>
redis涓嶆槸鍨冨溇妗讹紝寤鸿浣跨敤expire璁剧疆杩囨湡鏃堕棿(鏉′欢鍏佽鍙互鎵撴暎杩囨湡鏃堕棿锛岄槻姝㈤泦涓繃鏈?锛屼笉杩囨湡鐨勬暟鎹噸鐐瑰叧娉╥dletime銆?/p>
涓夈€佸鎴风浣跨敤
1銆侀伩鍏嶅涓簲鐢ㄤ娇鐢ㄤ竴涓猂edis瀹炰緥
涓嶇浉骞茬殑涓氬姟鎷嗗垎锛屽叕鍏辨暟鎹仛鏈嶅姟鍖栥€?/p>
2銆佷娇鐢ㄨ繛鎺ユ睜
鍙互鏈夋晥鎺у埗杩炴帴锛屽悓鏃舵彁楂樻晥鐜囷紝鏍囧噯浣跨敤鏂瑰紡锛?/p>
鎵ц鍛戒护濡備笅锛?Jedis jedis = null;
try {
jedis = jedisPool.getResource();
//鍏蜂綋鐨勫懡浠? jedis.executeCommand()
} catch (Exception e) {
logger.error("op key {} error: " + e.getMessage(), key, e);
} finally {
//娉ㄦ剰杩欓噷涓嶆槸鍏抽棴杩炴帴锛屽湪JedisPool妯″紡涓嬶紝Jedis浼氳褰掕繕缁欒祫婧愭睜銆? if (jedis != null)
jedis.close();
}
3銆佺啍鏂姛鑳?/strong>
楂樺苟鍙戜笅寤鸿瀹㈡埛绔坊鍔犵啍鏂姛鑳?渚嬪netflix hystrix)
4銆佸悎鐞嗙殑鍔犲瘑
璁剧疆鍚堢悊鐨勫瘑鐮侊紝濡傛湁蹇呰鍙互浣跨敤SSL鍔犲瘑璁块棶锛堥樋閲屼簯Redis鏀寔锛?/p>
5銆佹窐姹扮瓥鐣?/strong>
鏍规嵁鑷韩涓氬姟绫诲瀷锛岄€夊ソmaxmemory-policy(鏈€澶у唴瀛樻窐姹扮瓥鐣?锛岃缃ソ杩囨湡鏃堕棿銆?/p>
榛樿绛栫暐鏄痸olatile-lru锛屽嵆瓒呰繃鏈€澶у唴瀛樺悗锛屽湪杩囨湡閿腑浣跨敤lru绠楁硶杩涜key鐨勫墧闄わ紝淇濊瘉涓嶈繃鏈熸暟鎹笉琚垹闄わ紝浣嗘槸鍙兘浼氬嚭鐜癘OM闂銆?/p>
鍏朵粬绛栫暐濡備笅锛?/strong>
- allkeys-lru锛氭牴鎹甃RU绠楁硶鍒犻櫎閿紝涓嶇鏁版嵁鏈夋病鏈夎缃秴鏃跺睘鎬э紝鐩村埌鑵惧嚭瓒冲绌洪棿涓烘銆?/li>
- allkeys-random锛氶殢鏈哄垹闄ゆ墍鏈夐敭锛岀洿鍒拌吘鍑鸿冻澶熺┖闂翠负姝€?/li>
- volatile-random:闅忔満鍒犻櫎杩囨湡閿紝鐩村埌鑵惧嚭瓒冲绌洪棿涓烘銆?/li>
- volatile-ttl锛氭牴鎹敭鍊煎璞$殑ttl灞炴€э紝鍒犻櫎鏈€杩戝皢瑕佽繃鏈熸暟鎹€傚鏋滄病鏈夛紝鍥為€€鍒皀oeviction绛栫暐銆?/li>
- noeviction锛氫笉浼氬墧闄や换浣曟暟鎹紝鎷掔粷鎵€鏈夊啓鍏ユ搷浣滃苟杩斿洖瀹㈡埛绔敊璇俊鎭?quot;(error) OOM command not allowed when used memory"锛屾鏃禦edis鍙搷搴旇鎿嶄綔銆?/li>
浜斻€佸垹闄igkey
涓嬮潰鎿嶄綔鍙互浣跨敤pipeline鍔犻€熴€?br> redis 4.0宸茬粡鏀寔key鐨勫紓姝ュ垹闄わ紝娆㈣繋浣跨敤銆?/p>
1銆丠ash鍒犻櫎: hscan + hdel
public void delBigHash(String host, int port, String password, String bigHashKey) {
Jedis jedis = new Jedis(host, port);
if (password != null && !"".equals(password)) {
jedis.auth(password);
}
ScanParams scanParams = new ScanParams().count(100);
String cursor = "0";
do {
ScanResult<Entry<String, String>> scanResult = jedis.hscan(bigHashKey, cursor, scanParams);
List<Entry<String, String>> entryList = scanResult.getResult();
if (entryList != null && !entryList.isEmpty()) {
for (Entry<String, String> entry : entryList) {
jedis.hdel(bigHashKey, entry.getKey());
}
}
cursor = scanResult.getStringCursor();
} while (!"0".equals(cursor));
//鍒犻櫎bigkey
jedis.del(bigHashKey);
}
2銆丩ist鍒犻櫎: ltrim
public void delBigList(String host, int port, String password, String bigListKey) {
Jedis jedis = new Jedis(host, port);
if (password != null && !"".equals(password)) {
jedis.auth(password);
}
long llen = jedis.llen(bigListKey);
int counter = 0;
int left = 100;
while (counter < llen) {
//姣忔浠庡乏渚ф埅鎺?00涓? jedis.ltrim(bigListKey, left, llen);
counter += left;
}
//鏈€缁堝垹闄ey
jedis.del(bigListKey);
}
3銆丼et鍒犻櫎: sscan + srem
public void delBigSet(String host, int port, String password, String bigSetKey) {
Jedis jedis = new Jedis(host, port);
if (password != null && !"".equals(password)) {
jedis.auth(password);
}
ScanParams scanParams = new ScanParams().count(100);
String cursor = "0";
do {
ScanResult<String> scanResult = jedis.sscan(bigSetKey, cursor, scanParams);
List<String> memberList = scanResult.getResult();
if (memberList != null && !memberList.isEmpty()) {
for (String member : memberList) {
jedis.srem(bigSetKey, member);
}
}
cursor = scanResult.getStringCursor();
} while (!"0".equals(cursor));
//鍒犻櫎bigkey
jedis.del(bigSetKey);
}
4銆丼ortedSet鍒犻櫎: zscan + zrem
public void delBigZset(String host, int port, String password, String bigZsetKey) {
Jedis jedis = new Jedis(host, port);
if (password != null && !"".equals(password)) {
jedis.auth(password);
}
ScanParams scanParams = new ScanParams().count(100);
String cursor = "0";
do {
ScanResult<Tuple> scanResult = jedis.zscan(bigZsetKey, cursor, scanParams);
List<Tuple> tupleList = scanResult.getResult();
if (tupleList != null && !tupleList.isEmpty()) {
for (Tuple tuple : tupleList) {
jedis.zrem(bigZsetKey, tuple.getElement());
}
}
cursor = scanResult.getStringCursor();
} while (!"0".equals(cursor));
//鍒犻櫎bigkey
jedis.del(bigZsetKey);
}
绉佷俊鍥炲 璧勬枡 棰嗗彇涓€绾垮ぇ鍘侸ava闈㈣瘯棰樻€荤粨+鍚勭煡璇嗙偣瀛︿範鎬濈淮瀵?涓€浠?00椤祊df鏂囨。鐨凧ava鏍稿績鐭ヨ瘑鐐规€荤粨锛?/p>
杩欎簺璧勬枡鐨勫唴瀹归兘鏄潰璇曟椂闈㈣瘯瀹樺繀闂殑鐭ヨ瘑鐐癸紝绡囩珷鍖呮嫭浜嗗緢澶氱煡璇嗙偣锛屽叾涓寘鎷簡鏈夊熀纭€鐭ヨ瘑銆丣ava闆嗗悎銆丣VM銆佸绾跨▼骞跺彂銆乻pring鍘熺悊銆佸井鏈嶅姟銆丯etty 涓嶳PC 銆並afka銆佹棩璁般€佽璁℃ā寮忋€丣ava绠楁硶銆佹暟鎹簱銆乑ookeeper銆佸垎甯冨紡缂撳瓨銆佹暟鎹粨鏋勭瓑绛夈€?/p>
以上是关于2020闃块噷宸村反瀹樻柟鏈€鏂癛edis寮€鍙戣鑼冿紒的主要内容,如果未能解决你的问题,请参考以下文章