1.首先下载memcached 服务端
2.使用Enyim.Caching .Net 客户端
3.配置web.config
<sectionGroup name="QuickBootstrap1"> <section name="memcached" type="Enyim.Caching.Configuration.MemcachedClientSection, Enyim.Caching" /> </sectionGroup>
<QuickBootstrap1> <memcached> <servers> <add address="127.0.0.1" port="11211" /> </servers> <socketPool minPoolSize="10" maxPoolSize="100" connectionTimeout="00:00:10" deadTimeout="00:02:00" /> </memcached> </QuickBootstrap1>
4.开始使用
代码如下
//MemcachedClient mc = new MemcachedClient("QuickBootstrap1/memcached"); MemcachedClient mc = new MemcachedClient("QuickBootstrap1/memcached"); // store a string in the cache //bool res = mc.Store(StoreMode.Set, "MyKey", "Hello World"); User user = new Entities.User { CreateTime = DateTime.Now, IsEnable = false, UserName = "1q2", Nick = "12312", UserPwd = "123" }; string str = Newtonsoft.Json.JsonConvert.SerializeObject(user); bool result = mc.Store(StoreMode.Set, "user66", user); // retrieve the item from the cache // string mykey = mc.Get("MyKey").ToString(); // store some other items //mc.Store(StoreMode.Set, "D1", 1234L); //mc.Store(StoreMode.Set, "D2", DateTime.Now); //mc.Store(StoreMode.Set, "D3", true); //mc.Store(StoreMode.Set, "D4", new Product()); //mc.Store(StoreMode.Set, "D5", new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 });
5.注意:
mc.Store(StoreMode.Set, "user66", user); 会返回false;这是需要重启服务