Memcached鍒嗗竷寮忕紦瀛樺憿锛?/span>涓夈€丮emcached鍩虹鍘熺悊鍥涖€丮emcache涓嬭浇涓庡畨瑁?/span>浜斻€丮encacheHelper.cs 绀轰緥浣跨敤 缁撳悎Session涓庨」鐩厤缃紦瀛?/"/>

鍒嗗竷寮忕紦瀛樺涔犳€荤粨

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了鍒嗗竷寮忕紦瀛樺涔犳€荤粨相关的知识,希望对你有一定的参考价值。

鏍囩锛?/p>

涓€銆佸垎甯冨紡缂撳瓨绠€鍥?/p>

浜屻€佷负浠€涔堜娇鐢?span style="font-family: Calibri;">Memcached鍒嗗竷寮忕紦瀛樺憿锛?/span>

涓夈€丮emcached鍩虹鍘熺悊

鍥涖€丮emcache涓嬭浇涓庡畨瑁?/span>

浜斻€丮encacheHelper.cs 绀轰緥浣跨敤 缁撳悎Session涓庨」鐩厤缃紦瀛?/span>

鍏€丷edis鍜?/span>Memcache鐨勫尯鍒€荤粨

 

涓€銆佸垎甯冨紡缂撳瓨绠€鍥?/strong>

 

鎶€鏈垎浜? src=

 

浜屻€佷负浠€涔堜娇鐢?span style="font-family: Calibri;">Memcached鍒嗗竷寮忕紦瀛樺憿锛?/span>

棣栧厛鍏堣璁蹭负浣曡缂撳瓨锛屽湪鏁版嵁椹卞姩鐨?span style="font-family: Calibri;">web寮€鍙戜腑锛岀粡甯歌閲嶅浠庢暟鎹簱涓彇鍑虹浉鍚岀殑鏁版嵁锛岃繖绉嶉噸澶嶆瀬澶х殑澧炲姞浜嗘暟鎹簱璐熻浇銆傜紦瀛樻槸瑙e喅杩欎釜闂鐨勫ソ鍔炴硶銆備絾鏄?/span>ASP.NET涓殑铏界劧宸茬粡鍙互瀹炵幇瀵归〉闈㈠眬閮ㄨ繘琛岀紦瀛橈紝浣嗚繕鏄笉澶熺伒娲汇€?/span>Memcached搴旇繍鑰岀敓銆?/span>

 

1銆侀珮骞跺彂璁块棶鏁版嵁搴撶殑鐥涙锛氭閿侊紒

2銆佺鐩?/span>IO涔嬬棝锛屾暟鎹簱璇诲啓璇寸櫧浜嗗氨鏄窡纾佺洏鎵撲氦閬擄紝纾佺洏璇诲彇閫熷害鏄湁闄愬埗鐨勶紝涓€鑸珮鐐逛篃灏?/span>7200杞?/span>

3銆佸瀹㈡埛绔叡浜紦瀛?/span>

4銆?/span>Net+Memory  >> IO

5銆佽鍐欐€ц兘瀹岀編 1s 璇诲彇鍙互杈惧埌1w娆?nbsp;鍐欙細10w娆?/span>

6銆佽秴绠€鍗曢泦缇ゆ惌寤?nbsp;Clister

7銆佸紑婧?nbsp;Open Source

8銆佹病鏈夋彁渚涗富浠庤祴鍊煎姛鑳斤紝涔熸病鎻愪緵瀹圭伨绛夊姛鑳斤紙瀹圭伨锛氬嵆鏁版嵁澶囦唤鑳戒娇鎰忓鍙戠敓鍚庢仮澶嶆暟鎹紝Memcached涓嶄細杩涜澶囦唤锛岀敱浜庢槸缂撳瓨鍦ㄥ唴瀛樹腑鐨勶紝涓€鏂數灏变細澶卞幓鏁版嵁

 

锛夛紝鎵€浠ユ墍鏈夌殑浠g爜鍩烘湰閮藉彧鑰冭檻鎬ц兘鏈€浣炽€傚瑕佽€冭檻瀹圭伨锛屽垯鍙娇鐢?span style="font-family: Calibri;">Redis鍒嗗竷寮忕紦瀛?/span>

9銆佸涔犳垚鏈潪甯镐綆锛屽叆闂ㄩ潪甯稿鏄?/span>

10銆佷赴瀵岀殑鎴愬姛鐨勬渚嬨€傚緢澶氬ぇ鍨嬪叕鍙搁兘鏄敤杩欎釜鏉ュ仛鍒嗗竷寮忕紦瀛?/span>

娉細Memcached鍦ㄤ紒涓氫腑涓€鑸兘鏄湪linux涓嬭窇锛屾墠鑳借揪鍒版€ц兘鏈€浣炽€?/span>

 

涓夈€丮emcached鍩虹鍘熺悊

搴曞眰閫氫俊鏄娇鐢?span style="font-family: Calibri;">Socket

鍙互灏嗙紦瀛樻湇鍔″櫒鐞嗚В涓?span style="font-family: Calibri;">Socket鏈嶅姟绔紝灏?/span>WEB鏈嶅姟鍣ㄧ悊瑙d负瀹㈡埛绔€?/span>

鎶€鏈垎浜? src=

 

鍥涖€丮emcache涓嬭浇涓庡畨瑁?/span>

涓嬭浇锛岀櫨搴︿竴涓嬫垨鑰呯洿鎺ュ湪csdn涓婃悳涓€涓?/span>windows Memcache绋冲畾鐗堝氨琛岋紝0绉垎銆?/span>

1銆佷笅杞藉畬鍚庯紝灏辫繖涔堜釜exe

鎶€鏈垎浜? src=

 

2銆佸畨瑁咃紝鏁插嚑琛?/span>cmd鍛戒护灏辫锛屾埅鍥惧涓嬶紝宸﹁竟鏄垜浠绠楁満鏈嶅姟鍒楄〃锛屽彲浠ョ湅鍒帮紝宸茬粡鍚姩浜嗘垜浠殑Memcached锛?/span>Memcache鏄繖涓紑婧愰」鐩殑鍚嶇О锛屽姞涓?/span>d锛?/span>Memcached鏄叿浣撹繖涓簲鐢ㄧ▼搴忥紝涔熷氨鏄繖涓?/span>exe鐨勫悕绉帮級

鎶€鏈垎浜? src=

 

3銆佺幇鍦ㄥ凡缁忓惎鍔ㄦ湇鍔′簡锛屼笖灏嗗叾瀹夎鍒?span style="font-family: Calibri;">windows鏈嶅姟涓婁簡锛岃繖鏍蜂竴鏉ワ紝灏变笉鐢ㄦ瘡娆℃墜鍔ㄥ幓鍚姩浜嗭紝闅忕數鑴戣€屽惎鍔ㄣ€?/span>

 

鐜板湪鏉ユ祴璇曚笅锛岄殢渚挎柊寤轰釜鎺у埗鍙板簲鐢ㄧ▼搴?/p>

銆€銆€

鎶€鏈垎浜? id=
 1 using Memcached.ClientLibrary;
 2 using System;
 3 using System.Collections.Generic;
 4 using System.Linq;
 5 using System.Text;
 6 using System.Threading.Tasks;
 7 
 8 namespace 娴嬭瘯
 9 {
10     public class MemcacheTset
11     {
12         public static void Test()
13         {
14             string[] serverlist = { "127.0.0.1:11211" }; //鏈嶅姟鍣ㄥ垪琛紝鍙涓?鐢ㄩ€楀彿闅斿紑     
15             
16             //鍒濆鍖栨睜
17             SockIOPool pool = SockIOPool.GetInstance();
18 
19             //鏍规嵁瀹為檯鎯呭喌淇敼涓嬮潰鍙傛暟
20             pool.SetServers(serverlist);
21             pool.InitConnections = 3;
22             pool.MinConnections = 3;
23             pool.MaxConnections = 5;
24             pool.SocketConnectTimeout = 1000;
25             pool.SocketTimeout = 3000;
26             pool.MaintenanceSleep = 30;
27             pool.Failover = true;
28             pool.Nagle = false;
29             pool.Initialize(); // initialize the pool for memcache servers  
30          
31             //鑾峰緱瀹㈡埛绔疄渚?/span>
32             MemcachedClient mc = new MemcachedClient();//鍒濆鍖栦竴涓鎴风 
33             mc.EnableCompression = false;
34 
35             Console.WriteLine("---------娴嬭瘯----------");
36             mc.Set("test","my value");//瀛樺偍鏁版嵁鍒扮紦瀛樻湇鍔″櫒锛岃繖閲屽皢瀛楃涓?my value"缂撳瓨锛宬ey
37 
38             if (mc.KeyExists("test"))//娴嬭瘯缂撳瓨瀛樺湪key涓簍est鐨勯」鐩?/span>
39             {
40                 Console.WriteLine("test is Exists");
41                 Console.WriteLine(mc.Get("test").ToString());//鍦ㄧ紦瀛樹腑鑾峰彇key涓簍est鐨勯」鐩?/span>
42             }
43             else 
44             {
45                 Console.WriteLine("test not Exists");
46             }
47 
48             Console.ReadLine();
49 
50             mc.Delete("test");//绉婚櫎缂撳瓨涓璳ey涓簍est鐨勯」鐩?/span>
51 
52             if (mc.KeyExists("test"))
53             {
54                 Console.WriteLine("test is Exists");
55                 Console.WriteLine(mc.Get("test").ToString());
56             }
57             else
58             {
59                 Console.WriteLine("test not Exists");
60             }
61 
62             Console.ReadLine();
63 
64             SockIOPool.GetInstance().Shutdown();//鍏抽棴姹狅紝鍏抽棴sockets
65         }
66     }
67 }
View Code

 

濡傛灉绋嬪簭杩愯姝e父锛岃鏄庢垜浠殑Memcache鏈嶅姟宸插惎鍔ㄤ笖杩愯姝e父銆?/p>

鏈変竴鐐癸紝鍒繕鍜紝灏辨槸鎴戜滑Memcache鐨勯┍鍔紝鍚屾牱锛岀櫨搴# Memcache瀹夎灏監K銆?/p>

濂戒簡锛岀幇鍦ㄦ垜浠氨瀹為檯鏉ュ簲鐢ㄤ笅鍚с€?/p>

鎶€鏈垎浜? src=

浜斻€丮encacheHelper.cs 绀轰緥浣跨敤 缁撳悎椤圭洰閰嶇疆缂撳瓨

瀵逛簬浠€涔堟槸椤圭洰閰嶇疆锛岀浉淇″ぇ瀹惰偗瀹氶兘鐔熸倝鐨勶紝灏辨槸灏嗕笟鍔′腑涓嶇粡甯告洿鏀圭殑鏁版嵁濡傜郴缁熼偖浠跺湴鍧€锛屽皢鍏跺瓨鍦ㄦ暟鎹簱涓紝鏂逛究鏇存敼銆?/span>

鐒跺悗锛屽洖澶寸湅杩欏嚑涓瓧锛?ldquo;涓嶇粡甯告洿鏀?rdquo;锛屾垜浠氨寰楁敞鎰忎簡锛岃繖鏍风殑鏁版嵁锛屾垜浠氨寰楁兂鍒扮紦瀛樹簡锛岀紦瀛樺氨鏄敤鏉ョ鐞嗚繖鏍风殑鏁版嵁銆傜劧鍚庡憿锛屾垜浠氨鍙互浣跨敤鎴戜滑鐨凪emcache鏉ョ鐞嗗畠浜嗐€?/span>

椤圭洰涓紝鎴戜滑鍙互鏂板缓涓狹emcacheHelper绫绘潵灏佽涓嬩唬鐮侊紝鎴戝啓浜嗕釜鏈€绠€鍗曠殑瀛樺彇鍒犻櫎銆?/span>

 

鎶€鏈垎浜? id=
 1 using Memcached.ClientLibrary;
 2 using System;
 3 using System.Collections.Generic;
 4 using System.Linq;
 5 using System.Text;
 6 using System.Threading.Tasks;
 7 
 8 namespace JOEY.BookShop.Common
 9 {
10     public class MemcacheHelper
11     {
12         private static readonly MemcachedClient mc;
13         static MemcacheHelper()
14         {
15             string[] serverlist = { "127.0.0.1:11211" }; //鏈嶅姟鍣ㄥ垪琛紝鍙涓?鐢ㄩ€楀彿闅斿紑     
16 
17             //鍒濆鍖栨睜
18             SockIOPool pool = SockIOPool.GetInstance();
19 
20             //鏍规嵁瀹為檯鎯呭喌淇敼涓嬮潰鍙傛暟
21             pool.SetServers(serverlist);
22             pool.InitConnections = 3;
23             pool.MinConnections = 3;
24             pool.MaxConnections = 5;
25             pool.SocketConnectTimeout = 1000;
26             pool.SocketTimeout = 3000;
27             pool.MaintenanceSleep = 30;
28             pool.Failover = true;
29             pool.Nagle = false;
30             pool.Initialize(); // initialize the pool for memcache servers  
31 
32             //鑾峰緱瀹㈡埛绔疄渚?/span>
33             mc = new MemcachedClient();//鍒濆鍖栦竴涓鎴风 
34             mc.EnableCompression = false;
35         }
36 
37         /// <summary>
38         /// 瀛?
39         /// </summary>
40         /// <param name="key"></param>
41         /// <param name="value"></param>
42         public static void Set(string key, object value)
43         {
44             mc.Set(key, value);
45         }
46 
47         public static void Set(string key, object value, DateTime time)
48         {
49             mc.Set(key, value, time);
50         }
51 
52         /// <summary>
53         /// 鍙?
54         /// </summary>
55         /// <param name="key"></param>
56         /// <returns></returns>
57         public static object Get(string key)
58         {
59             if (mc.KeyExists(key))
60             {
61                 return mc.Get(key);
62             }
63             else
64             {
65                 return null;
66             }
67             
68         }
69 
70         /// <summary>
71         /// 鍒犻櫎
72         /// </summary>
73         /// <param name="key"></param>
74         /// <returns></returns>
75         public static bool Delete(string key)
76         {
77             if (mc.KeyExists(key))
78             {
79                 mc.Delete(key);
80                 return true;
81             }
82             return false;
83         }
84     }
85 }
View Code

 

1 var setting = this.DbSession.SettingsDal.LoadEntities(c => c.Name == "绯荤粺閭欢鍦板潃").FirstOrDefault();
2 string value = setting.Value.Trim();
3 MemcacheHelper.Set("setting_" + key, value);

杩欐牱灏卞皢鎴戜滑鐨勪粠鏁版嵁搴撲腑鍙栧嚭鐨勭郴缁熼偖浠跺湴鍧€瀛樺偍鍒颁簡Memcache涓紝寰堟柟渚垮惂銆傚彇鐨勮瘽灏辨槸锛?/p>

object obj = MemcacheHelper.Get("setting_" + "绯荤粺閭欢鍦板潃");

杩欓噷鐢变簬瀛樺彇鐨勬暟鎹潎涓哄瓧绗︿覆锛屼笉瀛樺湪搴忓垪鍖栫殑闂锛屽鏋滃瓨鍙栫殑瀵硅薄绫诲瀷涓嶆槸瀛楃涓诧紝濡傛煇涓〃Model锛岄偅涔堝氨寰楅€氳繃搴忓垪鍖栨潵杩涜鎿嶄綔锛屽浜庡簭鍒楀寲锛屾湰浜烘槸浣跨敤Json.Net鏉ユ搷浣溿€?/p>

鍐嶆潵涓緟鍔╃被鍚с€?/p>

鎶€鏈垎浜? id=
 1 using Newtonsoft.Json;
 2 using System;
 3 using System.Collections.Generic;
 4 using System.Linq;
 5 using System.Text;
 6 using System.Threading.Tasks;
 7 
 8 namespace JOEY.BookShop.Common
 9 {
10     /// <summary>
11     /// Json.Net 搴忓垪鍖栵紝瀵逛簬鐢变簬鐩镐簰寮曠敤绫诲瀷瀵艰嚧鐨勫簭鍒楀寲姝诲惊鐜紝鍙湪璇ュ璞′笂鍔犱釜鐗规€ф爣绛綶JsonIgnore] 濡傚湪Model涓湁澶栭敭锛屼袱涓ā鍨嬮棿鐩镐簰寮曠敤鍗抽€犳垚璇ラ棶棰?
12     /// </summary>
13     public class SerializeHelper
14     {
15         /// <summary>
16         /// 浼犲叆瀵硅薄锛屽簭鍒楀寲鎴愬瓧绗︿覆杩斿洖
17         /// </summary>
18         /// <param name="obj"></param>
19         /// <returns></returns>
20         public static string SerializeToString(object obj)
21         {
22             return JsonConvert.SerializeObject(obj);
23         }
24         /// <summary>
25         /// 浼犲叆搴忓垪鍖栧瓧绗︿覆锛屽弽搴忓垪鍖栨垚瀵瑰簲瀵硅薄杩斿洖
26         /// </summary>
27         /// <typeparam name="T">娉涘瀷锛屽搴斿璞$被鍨?/span></typeparam>
28         /// <param name="serializeStr">搴忓垪鍖栧悗鐨勫瓧绗︿覆</param>
29         /// <returns></returns>
30         public static T DeserializeToObject<T>(string serializeStr)
31         {
32             return JsonConvert.DeserializeObject<T>(serializeStr);
33         }
34     }
35 }
View Code

寰堝鏄撳氨鑳界湅鎳傦紝瀵瑰惂銆傚綋鐒堕┍鍔ㄤ篃鏄渶瑕佺殑銆傚悓鏍风櫨搴﹀摝銆?/p>

杩欓噷鏈変釜灏忛棶棰橈紝杩欎釜绋嬪簭闆嗭紝Newtonsoft.Json鍦ㄦ垜鐨凪VC椤圭洰涓湰韬氨瀛樺湪锛岃€屾垜鍦ㄥ叾浠栭」鐩紙鍗抽」鐩瓹ommon锛変腑鐢ㄧ殑鏃跺€欑敤鐨勭綉涓婁笅鐨勶紝閫夌殑鐗堟湰鏄?.5锛岀敱浜嶮VC椤圭洰寮曠敤浜咰ommon锛岃繖鏍疯矊浼煎氨鍑虹幇浜嗙増鏈笉涓€鑷寸殑鎯呭喌锛岃矊浼兼槸杩欐牱锛屼細鎻愮ず閿欒銆備簬鏄垜鎶奙VC涓殑dll缁欏垹闄や簡锛岄噸鏂板姞杞紺ommon涓嬬殑dll锛堢幇鍦ㄦ兂鎯虫垜涓轰粈涔堜笉鎶奀ommon涓嬬殑鍒犱簡锛屽幓寮曠敤MVC涓嬭繖涓憿- -锛夛紝杩欐牱涓€鏉ワ紝鍙堝嚭鐜颁竴涓棶棰橈紝鏈兘鍔犺浇鏂囦欢鎴栫▼搴忛泦“Newtonsoft.Json,Version=4.5.0.0銆備及璁℃槸閰嶇疆椤圭殑鍘熷洜锛屼簬鏄紝鐧惧害浜嗕笅锛屽湪web.config runtime鑺傜偣涓嬫坊鍔犱簡杩欎箞鍑犺锛屼慨鏀瑰悗涓猴細

<dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-8.0.0.0" newVersion="8.0.0.0" />
      </dependentAssembly>

8.0.0.0鏄綋鍓嶈繖涓猟ll鐨勭増鏈紝浼拌鏄増鏈洿鏂伴€氱煡鍚с€傚叿浣撲负浣曡繖鏍峰仛鏄湁鐐硅糠绯婄殑銆傝皝鑳芥寚鐐逛笅鍛紝鎰熸縺~~~

鍏€?span style="font-family: Calibri;">Redis鍜?/span>Memcache鐨勫尯鍒€荤粨锛堟憳鑷櫨搴︾煡閬擄級

1. Redis鏄粈涔?/span>

杩欎釜闂鐨勭粨鏋滃奖鍝嶄簡鎴戜滑鎬庝箞鐢?span style="font-family: Calibri;">Redis銆傚鏋滀綘璁や负Redis鏄竴涓?/span>key value store, 閭e彲鑳戒細鐢ㄥ畠鏉ヤ唬鏇?/span>mysql锛涘鏋滆涓哄畠鏄竴涓彲浠ユ寔涔呭寲鐨?/span>cache, 鍙兘鍙槸瀹冧繚瀛樹竴浜涢绻佽闂殑涓存椂鏁版嵁銆?/span>Redis鏄?/span>REmote DIctionary Server鐨勭缉鍐欙紝鍦?/span>Redis鍦ㄥ畼鏂圭綉绔欑殑鐨勫壇鏍囬鏄?/span>A persistent key-value database with built-in net interface written in ANSI-C for Posix systems锛岃繖涓畾涔夊亸鍚?/span>key value store銆傝繕鏈変竴浜涚湅娉曞垯璁や负Redis鏄竴涓?/span>memory database锛屽洜涓哄畠鐨勯珮鎬ц兘閮芥槸鍩轰簬鍐呭瓨鎿嶄綔鐨勫熀纭€銆傚彟澶栦竴浜涗汉鍒欒涓?/span>Redis鏄竴涓?/span>data structure server锛屽洜涓?/span>Redis鏀寔澶嶆潅鐨勬暟鎹壒鎬э紝姣斿List, Set绛夈€傚Redis鐨勪綔鐢ㄧ殑涓嶅悓瑙h鍐冲畾浜嗕綘瀵?/span>Redis鐨勪娇鐢ㄦ柟寮忋€?/span>

浜掕仈缃戞暟鎹洰鍓嶅熀鏈娇鐢ㄤ袱绉嶆柟寮忔潵瀛樺偍锛屽叧绯绘暟鎹簱鎴栬€?span style="font-family: Calibri;">key value銆備絾鏄繖浜涗簰鑱旂綉涓氬姟鏈韩骞朵笉灞炰簬杩欎袱绉嶆暟鎹被鍨嬶紝姣斿鐢ㄦ埛鍦ㄧぞ浼氬寲骞冲彴涓殑鍏崇郴锛屽畠鏄竴涓?/span>list锛屽鏋滆鐢ㄥ叧绯绘暟鎹簱瀛樺偍灏遍渶瑕佽浆鎹㈡垚涓€绉嶅琛岃褰曠殑褰㈠紡锛岃繖绉嶅舰寮忓瓨鍦ㄥ緢澶氬啑浣欐暟鎹紝姣忎竴琛岄渶瑕佸瓨鍌ㄤ竴浜涢噸澶嶄俊鎭€傚鏋滅敤key value瀛樺偍鍒欎慨鏀瑰拰鍒犻櫎姣旇緝楹荤儲锛岄渶瑕佸皢鍏ㄩ儴鏁版嵁璇诲嚭鍐嶅啓鍏ャ€?/span>Redis鍦ㄥ唴瀛樹腑璁捐浜嗗悇绉嶆暟鎹被鍨嬶紝璁╀笟鍔¤兘澶熼珮閫熷師瀛愮殑璁块棶杩欎簺鏁版嵁缁撴瀯锛屽苟涓斾笉闇€瑕佸叧蹇冩寔涔呭瓨鍌ㄧ殑闂锛屼粠鏋舵瀯涓婅В鍐充簡鍓嶉潰涓ょ瀛樺偍闇€瑕佽蛋涓€浜涘集璺殑闂銆?/span>

2. Redis涓嶅彲鑳芥瘮Memcache蹇?/span>

寰堝寮€鍙戣€呴兘璁や负Redis涓嶅彲鑳芥瘮Memcached蹇紝Memcached瀹屽叏鍩轰簬鍐呭瓨锛岃€?/span>Redis鍏锋湁鎸佷箙鍖栦繚瀛樼壒鎬э紝鍗充娇鏄紓姝ョ殑锛?/span>Redis涔熶笉鍙兘姣?/span>Memcached蹇€備絾鏄祴璇曠粨鏋滃熀鏈槸Redis鍗犵粷瀵逛紭鍔裤€備竴鐩村湪鎬濊€冭繖涓師鍥狅紝鐩墠鎯冲埌鐨勫師鍥犳湁杩欏嚑鏂归潰銆?/span>

Libevent銆傚拰Memcached涓嶅悓锛?/span>Redis骞舵病鏈夐€夋嫨libevent銆?/span>Libevent涓轰簡杩庡悎閫氱敤鎬ч€犳垚浠g爜搴炲ぇ(鐩墠Redis浠g爜杩樹笉鍒?/span>libevent鐨?/span>1/3)鍙婄壓鐗蹭簡鍦ㄧ壒瀹氬钩鍙扮殑涓嶅皯鎬ц兘銆?/span>Redis鐢?/span>libevent涓袱涓枃浠朵慨鏀瑰疄鐜颁簡鑷繁鐨?/span>epoll event loop(4)銆備笟鐣屼笉灏戝紑鍙戣€呬篃寤鸿Redis浣跨敤鍙﹀涓€涓?/span>libevent楂樻€ц兘鏇夸唬libev锛屼絾鏄綔鑰呰繕鏄潥鎸?/span>Redis搴旇灏忓阀骞跺幓渚濊禆鐨勬€濊矾銆備竴涓嵃璞℃繁鍒荤殑缁嗚妭鏄紪璇?/span>Redis涔嬪墠骞朵笉闇€瑕佹墽琛?/span>./configure銆?/span>

CAS闂銆?/span>CAS鏄?/span>Memcached涓瘮杈冩柟渚跨殑涓€绉嶉槻姝㈢珵浜変慨鏀硅祫婧愮殑鏂规硶銆?/span>CAS瀹炵幇闇€瑕佷负姣忎釜cache key璁剧疆涓€涓殣钘忕殑cas token锛?/span>cas鐩稿綋value鐗堟湰鍙凤紝姣忔set浼?/span>token闇€瑕侀€掑锛屽洜姝ゅ甫鏉?/span>CPU鍜屽唴瀛樼殑鍙岄噸寮€閿€锛岃櫧鐒惰繖浜涘紑閿€寰堝皬锛屼絾鏄埌鍗曟満10G+ cache浠ュ強QPS涓婁竾涔嬪悗杩欎簺寮€閿€灏变細缁欏弻鏂圭浉瀵瑰甫鏉ヤ竴浜涚粏寰€ц兘宸埆(5)銆?/span>

3. 鍗曞彴Redis鐨勫瓨鏀炬暟鎹繀椤绘瘮鐗╃悊鍐呭瓨灏?/span>

Redis鐨勬暟鎹叏閮ㄦ斁鍦ㄥ唴瀛樺甫鏉ヤ簡楂橀€熺殑鎬ц兘锛屼絾鏄篃甯︽潵涓€浜涗笉鍚堢悊涔嬪銆傛瘮濡備竴涓腑鍨嬬綉绔欐湁100涓囨敞鍐岀敤鎴凤紝濡傛灉杩欎簺璧勬枡瑕佺敤Redis鏉ュ瓨鍌紝鍐呭瓨鐨勫閲忓繀椤昏兘澶熷绾宠繖100涓囩敤鎴枫€備絾鏄笟鍔″疄闄呮儏鍐垫槸100涓囩敤鎴峰彧鏈?/span>5涓囨椿璺冪敤鎴凤紝1鍛ㄦ潵璁块棶杩?/span>1娆$殑涔熷彧鏈?/span>15涓囩敤鎴凤紝鍥犳鍏ㄩ儴100涓囩敤鎴风殑鏁版嵁閮芥斁鍦ㄥ唴瀛樻湁涓嶅悎鐞嗕箣澶勶紝RAM闇€瑕佷负鍐锋暟鎹拱鍗曘€?/span>

杩欒窡鎿嶄綔绯荤粺闈炲父鐩镐技锛屾搷浣滅郴缁熸墍鏈夊簲鐢ㄨ闂殑鏁版嵁閮藉湪鍐呭瓨锛屼絾鏄鏋滅墿鐞嗗唴瀛樺绾充笉涓嬫柊鐨勬暟鎹紝鎿嶄綔绯荤粺浼氭櫤鑳藉皢閮ㄥ垎闀挎湡娌℃湁璁块棶鐨勬暟鎹氦鎹㈠埌纾佺洏锛屼负鏂扮殑搴旂敤鐣欏嚭绌洪棿銆傜幇浠f搷浣滅郴缁熺粰搴旂敤鎻愪緵鐨勫苟涓嶆槸鐗╃悊鍐呭瓨锛岃€屾槸铏氭嫙鍐呭瓨(Virtual Memory)鐨勬蹇点€?/span>

鍩轰簬鐩稿悓鐨勮€冭檻锛?span style="font-family: Calibri;">Redis 2.0涔熷鍔犱簡VM鐗规€с€傝Redis鏁版嵁瀹归噺绐佺牬浜嗙墿鐞嗗唴瀛樼殑闄愬埗銆傚苟瀹炵幇浜嗘暟鎹喎鐑垎绂汇€?/span>

4. Redis鐨?/span>VM瀹炵幇鏄噸澶嶉€犺疆瀛?/span>

Redis鐨?/span>VM渚濈収涔嬪墠鐨?/span>epoll瀹炵幇鎬濊矾渚濇棫鏄嚜宸卞疄鐜般€備絾鏄湪鍓嶉潰鎿嶄綔绯荤粺鐨勪粙缁嶆彁鍒?/span>OS涔熷彲浠ヨ嚜鍔ㄥ府绋嬪簭瀹炵幇鍐风儹鏁版嵁鍒嗙锛?/span>Redis鍙渶瑕?/span>OS鐢宠涓€鍧楀ぇ鍐呭瓨锛?/span>OS浼氳嚜鍔ㄥ皢鐑暟鎹斁鍏ョ墿鐞嗗唴瀛橈紝鍐锋暟鎹氦鎹㈠埌纭洏锛屽彟澶栦竴涓煡鍚嶇殑“鐞嗚В浜嗙幇浠f搷浣滅郴缁?/span>(3)”鐨?/span>Varnish灏辨槸杩欐牱瀹炵幇锛屼篃鍙栧緱浜嗛潪甯告垚鍔熺殑鏁堟灉銆?/span>

浣滆€?span style="font-family: Calibri;">antirez鍦ㄨВ閲婁负浠€涔堣鑷繁瀹炵幇VM涓彁鍒板嚑涓師鍥?/span>(6)銆備富瑕?/span>OS鐨?/span>VM鎹㈠叆鎹㈠嚭鏄熀浜?/span>Page姒傚康锛屾瘮濡?/span>OS VM1涓?/span>Page鏄?/span>4K, 4K涓彧瑕佽繕鏈変竴涓厓绱犲嵆浣垮彧鏈?/span>1涓瓧鑺傝璁块棶锛岃繖涓〉涔熶笉浼氳SWAP, 鎹㈠叆涔熷悓鏍烽亾鐞嗭紝璇诲埌涓€涓瓧鑺傚彲鑳戒細鎹㈠叆4K鏃犵敤鐨勫唴瀛樸€傝€?/span>Redis鑷繁瀹炵幇鍒欏彲浠ヨ揪鍒版帶鍒舵崲鍏ョ殑绮掑害銆傚彟澶栬闂搷浣滅郴缁?/span>SWAP鍐呭瓨鍖哄煙鏃?/span>block杩涚▼锛屼篃鏄鑷?/span>Redis瑕佽嚜宸卞疄鐜?/span>VM鍘熷洜涔嬩竴銆?/span>

5. 鐢?/span>get/set鏂瑰紡浣跨敤Redis

浣滀负涓€涓?span style="font-family: Calibri;">key value瀛樺湪锛屽緢澶氬紑鍙戣€呰嚜鐒剁殑浣跨敤set/get鏂瑰紡鏉ヤ娇鐢?/span>Redis锛屽疄闄呬笂杩欏苟涓嶆槸鏈€浼樺寲鐨勪娇鐢ㄦ柟娉曘€傚挨鍏跺湪鏈惎鐢?/span>VM鎯呭喌涓嬶紝Redis鍏ㄩ儴鏁版嵁闇€瑕佹斁鍏ュ唴瀛橈紝鑺傜害鍐呭瓨灏ゅ叾閲嶈銆?/span>

鍋囧涓€涓?span style="font-family: Calibri;">key-value鍗曞厓闇€瑕佹渶灏忓崰鐢?/span>512瀛楄妭锛屽嵆浣垮彧瀛樹竴涓瓧鑺備篃鍗犱簡512瀛楄妭銆傝繖鏃跺€欏氨鏈変竴涓璁℃ā寮忥紝鍙互鎶?/span>key澶嶇敤锛屽嚑涓?/span>key-value鏀惧叆涓€涓?/span>key涓紝value鍐嶄綔涓轰竴涓?/span>set瀛樺叆锛岃繖鏍峰悓鏍?/span>512瀛楄妭灏变細瀛樻斁10-100鍊嶇殑瀹归噺銆?/span>

杩欏氨鏄负浜嗚妭绾﹀唴瀛橈紝寤鸿浣跨敤hashset鑰屼笉鏄?/span>set/get鐨勬柟寮忔潵浣跨敤Redis锛岃缁嗘柟娉曡鍙傝€冩枃鐚?/span>(7)銆?/span>

6. 浣跨敤aof浠f浛snapshot

Redis鏈変袱绉嶅瓨鍌ㄦ柟寮忥紝榛樿鏄?/span>snapshot鏂瑰紡锛屽疄鐜版柟娉曟槸瀹氭椂灏嗗唴瀛樼殑蹇収(snapshot)鎸佷箙鍖栧埌纭洏锛岃繖绉嶆柟娉曠己鐐规槸鎸佷箙鍖栦箣鍚庡鏋滃嚭鐜?/span>crash鍒欎細涓㈠け涓€娈垫暟鎹€傚洜姝ゅ湪瀹岀編涓讳箟鑰呯殑鎺ㄥ姩涓嬩綔鑰呭鍔犱簡aof鏂瑰紡銆?/span>aof鍗?/span>append only mode锛屽湪鍐欏叆鍐呭瓨鏁版嵁鐨勫悓鏃跺皢鎿嶄綔鍛戒护淇濆瓨鍒版棩蹇楁枃浠讹紝鍦ㄤ竴涓苟鍙戞洿鏀逛笂涓囩殑绯荤粺涓紝鍛戒护鏃ュ織鏄竴涓潪甯稿簽澶х殑鏁版嵁锛岀鐞嗙淮鎶ゆ垚鏈潪甯搁珮锛屾仮澶嶉噸寤烘椂闂翠細闈炲父闀匡紝杩欐牱瀵艰嚧澶卞幓aof楂樺彲鐢ㄦ€ф湰鎰忋€傚彟澶栨洿閲嶈鐨勬槸Redis鏄竴涓唴瀛樻暟鎹粨鏋勬ā鍨嬶紝鎵€鏈夌殑浼樺娍閮芥槸寤虹珛鍦ㄥ鍐呭瓨澶嶆潅鏁版嵁缁撴瀯楂樻晥鐨勫師瀛愭搷浣滀笂锛岃繖鏍峰氨鐪嬪嚭aof鏄竴涓潪甯镐笉鍗忚皟鐨勯儴鍒嗐€?/span>

鍏跺疄aof鐩殑涓昏鏄暟鎹彲闈犳€у強楂樺彲鐢ㄦ€э紝鍦?/span>Redis涓湁鍙﹀涓€绉嶆柟娉曟潵杈惧埌鐩殑锛?/span>Replication銆傜敱浜?/span>Redis鐨勯珮鎬ц兘锛屽鍒跺熀鏈病鏈夊欢杩熴€傝繖鏍疯揪鍒颁簡闃叉鍗曠偣鏁呴殰鍙婂疄鐜颁簡楂樺彲鐢ㄣ€?/span>

灏忕粨

瑕佹兂鎴愬姛浣跨敤涓€绉嶄骇鍝侊紝鎴戜滑闇€瑕佹繁鍏ヤ簡瑙e畠鐨勭壒鎬с€?span style="font-family: Calibri;">Redis鎬ц兘绐佸嚭锛屽鏋滆兘澶熺啛缁冪殑椹鹃┉锛屽鍥藉唴寰堝澶у瀷搴旂敤鍏锋湁寰堝ぇ甯姪銆?/span>

 

以上是关于鍒嗗竷寮忕紦瀛樺涔犳€荤粨的主要内容,如果未能解决你的问题,请参考以下文章

鍒嗗竷寮忎簨鍔$殑BASE鐞嗚

redis setnx 鍒嗗竷寮忛攣

鍒嗗竷寮忓瓨鍌↖PFS鏁板瓧璧勪骇

浜窐椤圭洰Day01馃毄鈽呪槄鈽咅煉?

鍒嗗竷寮忎簨鍔?浜?

鍒嗗竷寮廔D鐢熸垚绛栫暐 · fossi