EnyimMemcached(64位)使用实例

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

  

原文地址:https://www.cnblogs.com/zeusro/p/4026791.html