[转载].Net Memcached缓存用法 – merrick – 博客园.
memcached是一个以key-value的形式缓存数据的缓存系统。通过将数据缓存到内存中,从而提高数据的获取速度。
memcached以key-value的形式来保存数据,你可以为你每一段数据关联一个key,然后以后可以通过这个key获取
这段数据。
memcached是一个库还是什么?memcached其实是一个单独的网络服务器程序。它的网络底层基于libevent,你可以
将其运行在网络中的一台服务器上,通过网络,在遵循memcached的协议的基础上与memcached服务器进行通信。
一、安装Memcached服务
1. copy to c:\
2. start -> run -> cmd
3.
C:\memcached -d install -m 500
C:\memcached -d start
二、引用Enyim.Caching.dll
三、配置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.config
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>
四、实例代码
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/
参考:http://www.cnblogs.com/czh-liyu/archive/2010/04/27/1722084.html
代码地址:http://pan.baidu.com/share/link?shareid=796487540&uk=3658066951