Posted DotNetCore瀹炴垬

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了相关的知识,希望对你有一定的参考价值。


FreeRedis 鏄竴娆剧户 CSRedisCore 涔嬪悗閲嶅啓鐨?.NET redis 瀹㈡埛绔紑婧愮粍浠讹紝浠?MIT 鍗忚寮€婧愭墭绠′簬 github锛岀洰鍓嶆敮鎸?.NET 5銆?NETCore 2.1+銆?NETFramework 4.0+銆乆amarin锛屾湁鍙兘宸茬粡鏀寔 AOT 缂栬瘧锛堢洰鍓嶆湭娴嬭瘯锛屼絾浼氬線杩欎釜鏂瑰悜璧帮級銆?/p>

馃寛 鎵€鏈夋柟娉曞悕涓?redis-cli 淇濇寔涓€鑷?/p>

馃寣 鏀寔 Redis 闆嗙兢锛堟湇鍔$瑕佹眰 3.2 鍙婁互涓婄増鏈級

鉀?鏀寔 Redis 鍝ㄥ叺妯″紡

馃帲 鏀寔涓讳粠鍒嗙锛圡aster-Slave锛?/p>

馃摗 鏀寔鍙戝竷璁㈤槄锛圥ub-Sub锛?/p>

馃搩 鏀寔 Redis Lua 鑴氭湰

馃捇 鏀寔绠¢亾锛圥ipeline锛?/p>

馃摪 鏀寔浜嬪姟

馃尨 鏀寔 GEO 鍛戒护锛堟湇鍔$瑕佹眰 3.2 鍙婁互涓婄増鏈級

馃尣 鏀寔 STREAM 绫诲瀷鍛戒护锛堟湇鍔$瑕佹眰 5.0 鍙婁互涓婄増鏈級

鈿?鏀寔鏈湴缂撳瓨锛圕lient-side-cahing锛屾湇鍔$瑕佹眰 6.0 鍙婁互涓婄増鏈級

馃尦 鏀寔 Redis 6 鐨?RESP3 鍗忚

github: https://github.com/2881099/FreeRedis

2|0浜嗚В Redis

Redis鏄竴涓紑婧愮殑浣跨敤C璇█缂栧啓銆佸紑婧愩€佹敮鎸佺綉缁溿€佸彲鍩轰簬鍐呭瓨浜﹀彲鎸佷箙鍖栫殑鏃ュ織鍨嬨€侀珮鎬ц兘鐨凨ey-Value鏁版嵁搴擄紝骞舵彁渚涘绉嶈瑷€鐨凙PI銆傚畠閫氬父琚О涓?鏁版嵁缁撴瀯鏈嶅姟鍣?锛屽洜涓哄€硷紙value锛夊彲浠ユ槸 瀛楃涓?string)銆佸搱甯?map)銆?鍒楄〃(list)銆侀泦鍚?sets)銆佹湁搴忛泦鍚?sorted sets)銆佸湴鐞嗕綅缃?Geo)銆佹秷鎭垪闃?Streams)绛夌被鍨嬨€?/p>

涓庡叾浠?key - value 缂撳瓨浜у搧鏈変互涓嬩笁涓壒鐐癸細

Redis鏀寔鏁版嵁鐨勬寔涔呭寲锛屽彲浠ュ皢鍐呭瓨涓殑鏁版嵁淇濇寔鍦ㄧ鐩樹腑锛岄噸鍚殑鏃跺€欏彲浠ュ啀娆″姞杞借繘琛屼娇鐢ㄣ€?/p>

Redis涓嶄粎浠呮敮鎸佺畝鍗曠殑key-value绫诲瀷鐨勬暟鎹紝鍚屾椂杩樻彁渚沴ist锛宻et锛寊set锛宧ash绛夋暟鎹粨鏋勭殑瀛樺偍銆?/p>

Redis鏀寔鏁版嵁鐨勫浠斤紝鍗砿aster-slave妯″紡鐨勬暟鎹浠姐€?/p>

浼樺娍锛?/p>

鎬ц兘鏋侀珮 鈥?Redis鑳借鐨勯€熷害鏄?10000娆?s,鍐欑殑閫熷害鏄?1000娆?s 銆?/p>

涓板瘜鐨勬暟鎹被鍨?鈥?Redis鏀寔浜岃繘鍒舵渚嬬殑 Strings, Lists, Hashes, Sets, Ordered Sets, Geo, Streams 鏁版嵁绫诲瀷鎿嶄綔銆?/p>

鍘熷瓙 鈥?Redis鐨勬墍鏈夋搷浣滈兘鏄師瀛愭€х殑锛屽悓鏃禦edis杩樻敮鎸佸鍑犱釜鎿嶄綔鍏ㄥ苟鍚庣殑鍘熷瓙鎬ф墽琛屻€?/p>

涓板瘜鐨勭壒鎬?鈥?Redis杩樻敮鎸?publish/subscribe, 閫氱煡, key 杩囨湡绛夌瓑鐗规€с€?/p>

3|0鏈枃鐪嬬偣

Redis 6.0 鏄竴涓彲鏈熺殑鐗堟湰锛屽鍔犱簡 RESP3.0 鍗忚锛孉CL 鏉冮檺鎺у埗锛屼粠鍘熸湁鐨勫崟绾跨▼鏀逛负澶氱嚎绋嬶紙鎬ц兘鎻愬崌2-3鍊嶏級绛夎澶氭洿鏂般€備粖澶╁悜澶у浠嬬粛浠栫殑鍙︿竴涓噸瑕佺壒鎬э細瀹㈡埛绔紦瀛樻妧鏈紝璁茶В濡備綍钀借璁″湪 .NET 涓€?/p>

涓轰粈涔堥渶瑕佸鎴风缂撳瓨锛?/p>

鎴戜滑閮界煡閬擄紝浣跨敤 Redis 杩涜鏁版嵁鐨勭紦瀛樼殑涓昏鐩殑鏄噺灏戝 MySQL 绛夋暟鎹簱鐨勮闂紝鎻愪緵鏇村揩鐨勮闂€熷害锛屾瘯绔?銆奟edis in Action銆嬩腑鎻愬埌鐨勶紝 Redis 鐨勬€ц兘澶ц嚧鏄櫘閫氬叧绯诲瀷鏁版嵁搴撶殑 10 ~ 100 鍊嶃€?/p>

鎵€浠ワ紝濡備笅鍥炬墍绀猴紝Redis 鐢ㄦ潵瀛樺偍鐑偣鏁版嵁锛孯edis 鏈懡涓紝鍐嶅幓璁块棶鏁版嵁搴擄紝杩欐牱鍙互搴斾粯澶у鏁版儏鍐典笅鐨勬€ц兘瑕佹眰銆?/p>

.Net 寮€婧愰」鐩?FreeRedis 瀹炵幇鎬濊矾涔?- Redis 6.0 瀹㈡埛绔紦瀛樻妧鏈? class=

浣嗘槸锛孯edis 涔熸湁鍏舵€ц兘涓婇檺锛屽苟涓旇闂?Redis 蹇呯劧鏈変竴瀹氱殑缃戠粶 I/O 浠ュ強搴忓垪鍖栧弽搴忓垪鍖栨崯鑰椼€傛墍浠ワ紝寰€寰€浼氬紩鍏ヨ繘绋嬬紦瀛橈紝灏嗘渶鐑殑鏁版嵁瀛樺偍鍦ㄦ湰鍦帮紝杩涗竴姝ュ姞蹇闂€熷害銆?/p>

.Net 寮€婧愰」鐩?FreeRedis 瀹炵幇鎬濊矾涔?- Redis 6.0 瀹㈡埛绔紦瀛樻妧鏈? class=

濡備笂鍥炬墍绀猴紝Guava Cache 绛夎繘绋嬬紦瀛樹綔涓轰竴绾х紦瀛橈紝Redis 浣滀负浜岀骇缂撳瓨锛?/p>

鍏堝幓 Guava Cache 涓煡璇㈡暟鎹紝濡傛灉鍛戒腑鍒欑洿鎺ヨ繑鍥炪€?/p>

Guava Cache 涓湭鍛戒腑锛屽垯鍐嶅幓 Redis 涓煡璇紝濡傛灉鍛戒腑鍒欒繑鍥炴暟鎹紝骞跺湪 Guava Cache 涓缃鏁版嵁銆?/p>

Redis 涔熸湭鍛戒腑鐨勮瘽锛屽彧鏈夊幓 MySQL 涓煡璇紝鐒跺悗渚濇灏嗘暟鎹缃埌 Redis 鍜?Guava Cache 涓€?/p>

鍙娇鐢?Redis 鍒嗗竷寮忕紦瀛樻椂锛岄亣鍒版暟鎹洿鏂版椂锛屽簲鐢ㄧ▼搴忔洿鏂板畬 MySQL 涓殑鏁版嵁锛屽彲浠ョ洿鎺ュ皢 Redis 涓搴旂紦瀛樺け鏁堟帀锛屼繚鎸佹暟鎹殑涓€鑷存€с€?/p>

鑰岃繘绋嬪唴缂撳瓨鐨勬暟鎹竴鑷存€ф瘮鍒嗗竷寮忕殑缂撳瓨闈复鏇村ぇ鐨勬寫鎴樸€傛暟鎹洿鏂扮殑鏃跺€欙紝濡備綍閫氱煡鍏朵粬杩涚▼涔熸洿鏂拌嚜宸辩殑缂撳瓨鍛?

濡傛灉鎸夌収鍒嗗竷寮忕紦瀛樼殑鎬濊矾锛屾垜浠彲浠ヨ缃瀬鐭殑缂撳瓨澶辨晥鏃堕棿锛岃繖鏍蜂笉蹇呭疄鐜板鏉傜殑閫氱煡鏈哄埗銆?/p>

浣嗘槸涓嶅悓杩涚▼鍐呯殑鏁版嵁渚濈劧浼氶潰涓翠笉涓€鑷寸殑闂锛屽苟涓斾笉鍚岃繘绋嬬紦瀛樺け鏁堟椂闂翠笉缁熶竴锛屽悓涓€涓姹傚埌浜嗕笉鍚岀殑杩涚▼锛屽彲鑳藉嚭鐜板弽澶嶅够璇荤殑鎯呭喌銆?/p>

.Net 寮€婧愰」鐩?FreeRedis 瀹炵幇鎬濊矾涔?- Redis 6.0 瀹㈡埛绔紦瀛樻妧鏈? class=

4|0钀藉湴鍒嗘瀽

濡備笂褰?key 澶辨晥鐨勬椂鍊欙紝Redis 6.0 鎻愪緵浜嗕笁绉嶆ā寮忛€氱煡瀹㈡埛绔紝鏅€氭ā寮忋€佸箍鎾ā寮忋€佽浆鍙戞ā寮忋€?/p>

1銆佹櫘閫氭ā寮?/p>

鏅€氭ā寮忎緷璧?RESP3.0 鍗忚锛岄渶瑕佸湪杩炴帴鎴愬姛鏃朵娇鐢?hello 鍛戒护寮€鍚?RESP3.0 妯″紡銆?/p>

hello 3client tracking on

钀藉湴鍦?.NET 涔嬩腑鏃讹紝鎴戜滑蹇呯劧鏄娇鐢ㄨ繛鎺ユ睜鎶€鏈紝閭d箞姣忎釜杩炴帴閮藉繀椤讳娇鐢ㄤ互涓婄殑涓や釜鍛戒护锛屾鏃舵瘡涓繛鎺ユ槸涓€涓惊鐜鐨勬搷浣滐紝濡備笅锛?/p>

while (true)
{
   var msg = await redisSocket.ReceiveAsync(); //绛夊緟 key 澶辨晥鐨勯€氱煡
}

鏈潵鎴戜滑鍙互姣旇緝绠€鍗曠殑杩欐牱鎵ц鍛戒护锛?/p>

await redisSocket.SendAsync("GET key1");
await redisSocket.ReceiveAsync(); //璇诲彇鍝嶅簲鐨勭粨鏋?br>

鍙互鐪嬪嚭鏉ワ紝涓ゆ浠g爜鍚屾椂璇伙紝浼氬鑷磋鍙栫殑缁撴灉閿欎贡銆傚浣曡В鍐宠繕闇€瑕佷笁鎬濓紝鑰屾垜浠?PASS 浜嗚繖绉嶆ā寮忋€?/p>

2銆佸箍鎾ā寮?/p>

骞挎挱妯″紡鍜屾櫘閫氭ā寮忓樊涓嶅锛岄兘闇€瑕佷緷璧?RESP3.0 鍗忚銆傝繖绉嶆柟寮忎笅 Redis 鏈嶅姟绔笉鍐嶆秷鑰楄繃澶氬唴瀛樺瓨鍌ㄤ俊鎭紝鑰屾槸鍙戦€佹洿澶氱殑澶辨晥娑堟伅缁欏鎴风銆?/p>

涓庢櫘閫氭ā寮忓繀椤昏幏鍙栦竴娆¢敭鐨勮鍒欎笉鍚岋紝骞挎挱妯″紡涓嬶紝鍙閿淇敼鎴栧垹闄わ紝绗﹀悎瑙勫垯鐨勫鎴风閮戒細鏀跺埌澶辨晥娑堟伅锛岃€屼笖鏄彲浠ュ娆¤幏鍙栫殑

涓庢櫘閫氭ā寮忕浉姣旓紝铏界劧灏戝瓨鍌ㄤ簡涓€浜涙暟鎹紝浣嗘槸鐢变簬闇€瑕佸鍓嶇紑瑙勫垯杩涜鍖归厤锛屼細娑堣€椾竴瀹氱殑 CPU 璧勬簮锛屾墍浠ユ敞鎰忓埆浣跨敤杩囬暱鐨勫墠缂€銆?/p>

骞挎挱妯″紡鍜屾櫘閫氭ā寮忎竴鏍凤紝闇€瑕佽В鍐冲懡浠ゅ悓鏃惰鍙栫殑闂锛堣瑙佷笂闈㈢殑涓ゆ浠g爜锛夈€?/p>

3銆佽浆鍙戞ā寮?/p>

Redis 涓轰簡鍏煎 RESP2 鍗忚鎻愪緵浜嗚浆鍙?Redirect)妯″紡锛屼笉鍐嶄娇鐢?RESP3 鍘熺敓鏀寔 PUSH 娑堟伅锛岃€屾槸灏嗘秷鎭€氳繃 Pub/Sub 閫氱煡缁欏彟澶栦竴涓鎴风锛屽叿浣撴祦绋嬪涓嬪浘鎵€绀恒€?/p>

public void Start()
{
   //璁㈤槄 __redis__:invalidate
   _sub = _cli.Subscribe("__redis__:invalidate", InValidate) as IPubSubSubscriber;

   //鎷︽埅缂撳瓨
   _cli.Interceptors.Add(() => new MemoryCacheAop(this));

   //褰撶綉缁滄柇寮€鐨勬椂鍊欙紝娓呯┖鏈湴缂撳瓨
   _cli.Unavailable += (_, e) =>
   {
       lock (_dictLock) _dictSort.Clear();
       _dict.Clear();
   };
   _cli.Connected += (_, e) =>
   {
       //鏈€鍏抽敭鐨勪竴涓懡浠わ紝鍚﹀垯 __redis__:invalidate 鏃犳硶鏀跺埌璁㈤槄娑堟伅
       e.Client.ClientTracking(true, _sub.RedisSocket.ClientId, null, false, false, false, false);
   };
}

void InValidate(string chan, object msg)
{
   var keys = msg as object[];
   foreach (var key in keys) //绉婚櫎鏈湴缂撳瓨
       RemoveCache(string.Concat(key));
}

MemoryCacheAop 鏄?FreeRedis 宸茬粡瀹炵幇濂界殑鎷︽埅鍣紝涓昏瀹炵幇鎷︽埅鍛戒护鎵ц锛岃幏鍙栨湰鍦板唴瀛樸€傚畬鏁翠唬鐮侊細https://github.com/2881099/FreeRedis/blob/master/src/FreeRedis/ClientSideCaching.cs

5|0娴嬭瘯鍔熻兘

static Lazy _cliLazy = new Lazy (() =>
{
   var r = new RedisClient("192.168.164.10:6379,database=1"); //redis 6.0
   r.Serialize = obj => JsonConvert.SerializeObject(obj);
   r.Deserialize = (json, type) => JsonConvert.DeserializeObject(json, type);
   r.Notice += (s, e) => Console.WriteLine(e.Log);
   return r;
});
static RedisClient cli => _cliLazy.Value;

static void Main(string[] args)
{
   cli.UseClientSideCaching(new ClientSideCachingOptions
   {
       //鏈湴缂撳瓨鐨勫閲?
       Capacity = 3,
       //杩囨护鍝簺閿兘琚湰鍦扮紦瀛?
       KeyFilter = key => key.StartsWith("Interceptor"),
       //妫€鏌ラ暱鏈熸湭浣跨敤鐨勭紦瀛?
       CheckExpired = (key, dt) => DateTime.Now.Subtract(dt) > TimeSpan.FromSeconds(2)
   });

   cli.Set("Interceptor01", "123123"); //redis-server

   var val1 = cli.Get("Interceptor01"); //redis-server
   var val2 = cli.Get("Interceptor01"); //鏈湴
   var val3 = cli.Get("Interceptor01"); //鏂偣绛?绉掞紝redis-server

   cli.Set("Interceptor01", "234567"); //redis-server
   var val4 = cli.Get("Interceptor01"); //redis-server
   var val5 = cli.Get("Interceptor01"); //鏈湴

   var val6 = cli.MGet("Interceptor01", "Interceptor02", "Interceptor03"); //redis-server
   var val7 = cli.MGet("Interceptor01", "Interceptor02", "Interceptor03"); //鏈湴
   var val8 = cli.MGet("Interceptor01", "Interceptor02", "Interceptor03"); //鏈湴

   cli.MSet("Interceptor01", "Interceptor01Value", "Interceptor02", "Interceptor02Value", "Interceptor03", "Interceptor03Value");  //redis-server
   var val9 = cli.MGet("Interceptor01", "Interceptor02", "Interceptor03");  //redis-server
   var val10 = cli.MGet("Interceptor01", "Interceptor02", "Interceptor03");  //鏈湴

   //浠ヤ笅 KeyFilter 杩斿洖 false锛屼粠鑰屼笉浣跨敤鏈湴缂撳瓨
   cli.Set("123Interceptor01", "123123"); //redis-server

   var val11 = cli.Get("123Interceptor01"); //redis-server
   var val12 = cli.Get("123Interceptor01"); //redis-server
   var val23 = cli.Get("123Interceptor01"); //redis-server
   Console.ReadKey();
}

cli.Notice 浜嬩欢鍦ㄦ帶鍒跺彴杈撳嚭鍐呭锛?/p>

Not connected 浠h〃娌℃湁缁忚繃 redis-server


192.168.164.10:6379   > CLIENT TRACKING ON REDIRECT 46
FreeRedis.RedisResult
(0ms)

192.168.164.10:6379   > SET Interceptor01 123123
OK
(24ms)

192.168.164.10:6379   > GET Interceptor01
123123
(2ms)

Not connected         > GET Interceptor01
123123
(0ms)

192.168.164.10:6379   > GET Interceptor01
123123
(0ms)

192.168.164.10:6379   > SET Interceptor01 234567
OK
(0ms)

192.168.164.10:6379   > GET Interceptor01
234567
(0ms)

Not connected         > GET Interceptor01
234567
(0ms)

192.168.164.10:6379   > MGET Interceptor01 Interceptor02 Interceptor03
[234567, Interceptor02Value, Interceptor03Value]
(0ms)

Not connected         > MGET Interceptor01 Interceptor02 Interceptor03
[234567, Interceptor02Value, Interceptor03Value]
(0ms)

Not connected         > MGET Interceptor01 Interceptor02 Interceptor03
[234567, Interceptor02Value, Interceptor03Value]
(0ms)

192.168.164.10:6379   > MSET Interceptor01 Interceptor01Value Interceptor02 Interceptor02Value Interceptor03 Interceptor03Value
False
(3ms)

192.168.164.10:6379   > MGET Interceptor01 Interceptor02 Interceptor03
[Interceptor01Value, Interceptor02Value, Interceptor03Value]
(1ms)

Not connected         > MGET Interceptor01 Interceptor02 Interceptor03
[Interceptor01Value, Interceptor02Value, Interceptor03Value]
(0ms)

192.168.164.10:6379   > SET 123Interceptor01 123123
OK
(0ms)

192.168.164.10:6379   > GET 123Interceptor01
123123
(0ms)

192.168.164.10:6379   > GET 123Interceptor01
123123
(0ms)

192.168.164.10:6379   > GET 123Interceptor01
123123
(0ms)

6|0鍐欏湪鏈€鍚?/p>

FreeRedis 鏄竴娆剧户 CSRedisCore 涔嬪悗閲嶅啓鐨?.NET redis 瀹㈡埛绔紑婧愮粍浠讹紝浠?MIT 鍗忚寮€婧愭墭绠′簬 github锛岀洰鍓嶆敮鎸?.NET 5銆?NETCore 2.1+銆?NETFramework 4.0+銆乆amarin锛屾湁鍙兘宸茬粡鏀寔 AOT 缂栬瘧锛堢洰鍓嶆湭娴嬭瘯锛屼絾浼氬線杩欎釜鏂瑰悜璧帮級銆?/p>

github: https://github.com/2881099/FreeRedis

璋㈣阿鏀寔锛侊紒


寰€鏈熺簿褰╁洖椤?/p>















以上是关于的主要内容,如果未能解决你的问题,请参考以下文章

Python 操作Redis

python爬虫入门----- 阿里巴巴供应商爬虫

Python词典设置默认值小技巧

《python学习手册(第4版)》pdf

Django settings.py 的media路径设置

Python中的赋值,浅拷贝和深拷贝的区别