memcached是一個以key-value的形式緩存數據的緩存系統。經過將數據緩存到內存中,從而提升數據的獲取速度。
memcached以key-value的形式來保存數據,你能夠爲你每一段數據關聯一個key,而後之後能夠經過這個key獲取
這段數據。 html
memcached是一個庫仍是什麼?memcached實際上是一個單獨的網絡服務器程序。它的網絡底層基於libevent,你能夠
將其運行在網絡中的一臺服務器上,經過網絡,在遵循memcached的協議的基礎上與memcached服務器進行通訊。git
1、安裝Memcached服務
1. copy to c:\
2. start -> run -> cmd
3.
C:\memcached -d install -m 500
C:\memcached -d startgithub
2、引用Enyim.Caching.dll緩存
3、配置Config文件服務器
1 <?xml version="1.0" encoding="utf-8" ?> 2 <configuration> 3 <configSections> 4 <sectionGroup name="enyim.com"> 5 <section name="memcached" type="Enyim.Caching.Configuration.MemcachedClientSection, Enyim.Caching"/> 6 </sectionGroup> 7 <section name="memcached" type="Enyim.Caching.Configuration.MemcachedClientSection, Enyim.Caching"/> 8 </configSections> 9 10 <enyim.com> 11 <memcached configSource="ConfigFiles\enyim.com.config"/> 12 </enyim.com> 13 <memcached configSource="ConfigFiles\memcached.config"/> 14 15 <startup> 16 <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> 17 </startup> 18 </configuration>
enyim.com.config網絡
1 <?xml version="1.0" encoding="utf-8" standalone="yes"?> 2 <!-- 3 Please read the documentation first: 4 http://github.com/enyim/EnyimMemcached/wiki/MemcachedClient-Configuration 5 6 Use this section as a template if you're connecting to regular memcached servers. 7 Note: you must have the enyim.com/memcached section if you're using the parameterless constructor of EnyimMemcachedClient. 8 --> 9 <memcached protocol="Text"> 10 <servers> 11 <add address="127.0.0.1" port="11211" /> 12 </servers> 13 <socketPool minPoolSize="50" 14 maxPoolSize="1000" 15 connectionTimeout="00:01:10" 16 deadTimeout="00:02:00" 17 /> 18 </memcached>
memcached.configless
1 <?xml version="1.0" encoding="utf-8" standalone="yes"?> 2 <memcached keyTransformer="Enyim.Caching.TigerHashTransformer, Enyim.Caching"> 3 <servers> 4 <add address="127.0.0.1" port="11211" /> 5 </servers> 6 <socketPool minPoolSize="50" maxPoolSize="1000" connectionTimeout="00:01:10" deadTimeout="00:02:00" /> 7 </memcached>
4、實例代碼socket
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 using Enyim.Caching.Configuration; 7 using Enyim.Caching; 8 using Enyim.Caching.Memcached; 9 10 11 namespace MemcachedConsole 12 { 13 class Program 14 { 15 static void Main(string[] args) 16 { 17 MemcachedClient mc = new MemcachedClient(); 18 19 // store a string in the cache 20 mc.Store(StoreMode.Set, "MyKey", "Hello World"); 21 // retrieve the item from the cache 22 Console.WriteLine(mc.Get("MyKey")); 23 // store some other items 24 mc.Store(StoreMode.Set, "D1", 1234L); 25 mc.Store(StoreMode.Set, "D2", DateTime.Now); 26 mc.Store(StoreMode.Set, "D3", true); 27 mc.Store(StoreMode.Set, "D4", new Product()); 28 mc.Store(StoreMode.Set, "D5", new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }); 29 Console.WriteLine("D1: {0}", mc.Get("D1")); 30 Console.WriteLine("D2: {0}", mc.Get("D2")); 31 Console.WriteLine("D3: {0}", mc.Get("D3")); 32 Console.WriteLine("D4: {0}", mc.Get("D4")); 33 34 byte[] tmp = mc.Get<byte[]>("D5"); 35 // delete them from the cache 36 mc.Remove("D1"); 37 mc.Remove("D2"); 38 mc.Remove("D3"); 39 mc.Remove("D4"); 40 // add an item which is valid for 10 mins 41 mc.Store(StoreMode.Set, "D4", new Product(), new TimeSpan(0, 10, 0)); 42 Console.ReadLine(); 43 } 44 } 45 46 [Serializable] 47 class Product 48 { 49 public double Price = 1.24; 50 public string Name = "Mineral Water"; 51 public override string ToString() 52 { 53 return String.Format("Product {{{0}: {1}}}", this.Name, this.Price); 54 } 55 } 56 }
參考:http://kb.cnblogs.com/page/42777/ide
參考:http://www.cnblogs.com/czh-liyu/archive/2010/04/27/1722084.htmlmemcached
代碼地址:http://pan.baidu.com/share/link?shareid=796487540&uk=3658066951