標籤:des style blog http color io os 使用 ar
1.安裝:http://www.cnblogs.com/dudu/archive/2009/07/19/1526669.html
2.使用
using Enyim.Caching.Configuration; using Enyim.Caching.Memcached; using Enyim.Caching; using System.IO; public class HomeController : Controller { public ActionResult Index() { string setstring = "abcdefg"; Stopwatch sw = new Stopwatch(); sw.Start(); MemcachedClientConfiguration config = new MemcachedClientConfiguration();//建立配置參數 var obj = new a() { b = "abcd", c = "defg", }; config.Servers.Add(new System.Net.IPEndPoint(IPAddress.Parse("127.0.0.1"), 11211));//增加服務節點 config.Protocol = MemcachedProtocol.Text;//隨便哪個枚舉都行 //config.Authentication.Type = typeof(PlainTextAuthenticator);//設定驗證模式 //config.Authentication.Parameters["userName"] = "memcache";//使用者名稱參數 //config.Authentication.Parameters["password"] = "password";//密碼參數 using (var mac = new MemcachedClient(config))//建立串連 { //for (int a = 0; a < 300000; a++) Parallel.For(0, 300000, a => { //STRING mac.Store(StoreMode.Add, "textbox1" + a.ToString(), setstring);//寫入 mac.Store(StoreMode.Set, "textbox1" + a.ToString(), setstring);//更新 //JSON mac.Store(StoreMode.Add, "aa" + a.ToString(), obj.SerializeObject()); //位元組數組 var bytes = SerializeBinary(obj); mac.Store(StoreMode.Add, "bytes" + a.ToString(), bytes); //string str = mac.Get<string>("textbox1");//擷取資訊 //string cache = mac.Get<string>("aa"); //var cacheObj = cache.DeserializeObject<a>(); //var cachebytes = DeserializeBinary(mac.Get("bytes") as byte[]) as a; }); } sw.Stop(); return Content(sw.ElapsedMilliseconds.ToString()); //return Content(cachebytes.b + "<br />" + cachebytes.c); //return Content(cacheObj.b + "<br />" + cacheObj.c); } /// <summary> /// 序列化為二進位位元組數組 /// </summary> /// <param name="request">要序列化的對象</param> /// <returns>位元組數組</returns> public static byte[] SerializeBinary(object request) { var serializer = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); using (MemoryStream memStream = new System.IO.MemoryStream()) { serializer.Serialize(memStream, request); return memStream.GetBuffer(); } } /// <summary> /// 從位元組還原序列化得到對象 /// </summary> /// <param name="buf">位元組數組</param> /// <returns>得到的對象</returns> public static object DeserializeBinary(byte[] buf) { using (MemoryStream memStream = new MemoryStream(buf)) { memStream.Position = 0; var deserializer = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); object newobj = deserializer.Deserialize(memStream); memStream.Close(); return newobj; } } }
[Serializable] public class a { public string b { get; set; } public string c { get; set; } }
using Newtonsoft.Json; public static class JsonExtensions { /// <summary> /// 對象轉為json字串 /// 序列化 /// </summary> /// <param name="o"></param> /// <returns></returns> public static string SerializeObject(this object o) { if (o == null) { return string.Empty; } return Newtonsoft.Json.JsonConvert.SerializeObject(o); } public static string SerializeObject(this object o, Newtonsoft.Json.Formatting f) { if (o == null) { return string.Empty; } return Newtonsoft.Json.JsonConvert.SerializeObject(o, f); } /// <summary> /// 字串解析為對象 /// 還原序列化 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="str"></param> /// <returns></returns> public static T DeserializeObject<T>(this string str) { try { return Newtonsoft.Json.JsonConvert.DeserializeObject<T>(str); } catch (Exception ex) { return default(T); } } /// <summary> /// 根據key獲得json對象 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="str"></param> /// <param name="key"></param> /// <returns></returns> public static T DeserializeObject<T>(this string str, string key) { try { var p = Newtonsoft.Json.Linq.JObject.Parse(str); return JsonConvert.DeserializeObject<T>(p[key].ToString()); } catch (Exception) { } return default(T); } }
主要是看第一個控制器類,後面2個是輔助類和POCO實體,附帶了對象序列化和還原序列化的方法。樣本示範了用EnyimMemcached儲存字串,位元組數組,json的方法。個人喜好json這種。
想偷懶走csdn,3分拿走:http://download.csdn.net/detail/u012398331/8041697
EnyimMemcached(64位)使用執行個體