鍒嗗竷寮忕紦瀛樺涔犳€荤粨
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了鍒嗗竷寮忕紦瀛樺涔犳€荤粨相关的知识,希望对你有一定的参考价值。
鏍囩锛?/p>
涓€銆佸垎甯冨紡缂撳瓨绠€鍥?/p>
浜屻€佷负浠€涔堜娇鐢?span style="font-family: Calibri;">Memcached
鍒嗗竷寮忕紦瀛樺憿锛?/span>涓夈€丮emcached鍩虹鍘熺悊
鍥涖€丮emcache涓嬭浇涓庡畨瑁?/span>
浜斻€丮encacheHelper.cs 绀轰緥浣跨敤 缁撳悎Session涓庨」鐩厤缃紦瀛?/span>
鍏€丷edis鍜?/span>Memcache鐨勫尯鍒€荤粨
涓€銆佸垎甯冨紡缂撳瓨绠€鍥?/strong>
浜屻€佷负浠€涔堜娇鐢?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>
鍥涖€丮emcache涓嬭浇涓庡畨瑁?/span>
涓嬭浇锛岀櫨搴︿竴涓嬫垨鑰呯洿鎺ュ湪csdn涓婃悳涓€涓?/span>windows Memcache绋冲畾鐗堝氨琛岋紝0绉垎銆?/span>
1銆佷笅杞藉畬鍚庯紝灏辫繖涔堜釜exe
2銆佸畨瑁咃紝鏁插嚑琛?/span>cmd鍛戒护灏辫锛屾埅鍥惧涓嬶紝宸﹁竟鏄垜浠绠楁満鏈嶅姟鍒楄〃锛屽彲浠ョ湅鍒帮紝宸茬粡鍚姩浜嗘垜浠殑Memcached锛?/span>Memcache鏄繖涓紑婧愰」鐩殑鍚嶇О锛屽姞涓?/span>d锛?/span>Memcached鏄叿浣撹繖涓簲鐢ㄧ▼搴忥紝涔熷氨鏄繖涓?/span>exe鐨勫悕绉帮級
3銆佺幇鍦ㄥ凡缁忓惎鍔ㄦ湇鍔′簡锛屼笖灏嗗叾瀹夎鍒?span style="font-family: Calibri;">windows
鏈嶅姟涓婁簡锛岃繖鏍蜂竴鏉ワ紝灏变笉鐢ㄦ瘡娆℃墜鍔ㄥ幓鍚姩浜嗭紝闅忕數鑴戣€屽惎鍔ㄣ€?/span>
鐜板湪鏉ユ祴璇曚笅锛岄殢渚挎柊寤轰釜鎺у埗鍙板簲鐢ㄧ▼搴?/p>
銆€銆€
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 }
濡傛灉绋嬪簭杩愯姝e父锛岃鏄庢垜浠殑Memcache鏈嶅姟宸插惎鍔ㄤ笖杩愯姝e父銆?/p>
鏈変竴鐐癸紝鍒繕鍜紝灏辨槸鎴戜滑Memcache鐨勯┍鍔紝鍚屾牱锛岀櫨搴# Memcache瀹夎灏監K銆?/p>
濂戒簡锛岀幇鍦ㄦ垜浠氨瀹為檯鏉ュ簲鐢ㄤ笅鍚с€?/p>
浜斻€丮encacheHelper.cs 绀轰緥浣跨敤 缁撳悎椤圭洰閰嶇疆缂撳瓨
瀵逛簬浠€涔堟槸椤圭洰閰嶇疆锛岀浉淇″ぇ瀹惰偗瀹氶兘鐔熸倝鐨勶紝灏辨槸灏嗕笟鍔′腑涓嶇粡甯告洿鏀圭殑鏁版嵁濡傜郴缁熼偖浠跺湴鍧€锛屽皢鍏跺瓨鍦ㄦ暟鎹簱涓紝鏂逛究鏇存敼銆?/span>
鐒跺悗锛屽洖澶寸湅杩欏嚑涓瓧锛?ldquo;涓嶇粡甯告洿鏀?rdquo;锛屾垜浠氨寰楁敞鎰忎簡锛岃繖鏍风殑鏁版嵁锛屾垜浠氨寰楁兂鍒扮紦瀛樹簡锛岀紦瀛樺氨鏄敤鏉ョ鐞嗚繖鏍风殑鏁版嵁銆傜劧鍚庡憿锛屾垜浠氨鍙互浣跨敤鎴戜滑鐨凪emcache鏉ョ鐞嗗畠浜嗐€?/span>
椤圭洰涓紝鎴戜滑鍙互鏂板缓涓狹emcacheHelper绫绘潵灏佽涓嬩唬鐮侊紝鎴戝啓浜嗕釜鏈€绠€鍗曠殑瀛樺彇鍒犻櫎銆?/span>
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 }
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>
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 }
寰堝鏄撳氨鑳界湅鎳傦紝瀵瑰惂銆傚綋鐒堕┍鍔ㄤ篃鏄渶瑕佺殑銆傚悓鏍风櫨搴﹀摝銆?/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>