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寮€鍙戣鑼冿紒的主要内容,如果未能解决你的问题,请参考以下文章

浜戞爾骞茶揣鍥為 | 琛屼笟椤剁骇NoSQL鎴愬憳鍧愰晣锛孨oSQL鏁版嵁搴撲笓鍦洪噸鐐硅В鏋愶紒

UE4鎻掍欢

闃块噷寰湇鍔℃媶鍒?鏈嶅姟娌荤悊鍘熷垯

闃块噷浜戜富鏈猴紙ECS锛変笌CentOS7瀹炴垬

web缃戠珯闃块噷浜戞湇鍔″櫒椤圭洰閮ㄧ讲