Enyimmemcached (64-bit) Usage instance

Source: Internet
Author: User
Tags serialization

1. Installation: http://www.cnblogs.com/dudu/archive/2009/07/19/1526669.html

1. Role

    usingEnyim.Caching.Configuration; usingEnyim.Caching.Memcached; usingenyim.caching; usingSystem.IO;  Public classHomecontroller:controller { PublicActionResult Index () {stringSetString ="ABCDEFG"; Stopwatch SW=NewStopwatch (); Sw.            Start (); Memcachedclientconfiguration Config=NewMemcachedclientconfiguration ();//Creating configuration Parameters            varobj =NewA () {b="ABCD", C="DEFG",            }; Config. Servers.add (NewSystem.Net.IPEndPoint (Ipaddress.parse ("127.0.0.1"),11211));//Add Service nodeConfig. Protocol = Memcachedprotocol.text;//any enumeration, whatever.//CONFIG. Authentication.type = typeof (Plaintextauthenticator);//Setting the validation mode//CONFIG. authentication.parameters["userName"] = "memcache";//User Name Parameters//CONFIG. authentication.parameters["password"] = "password";//Password Parameters            using(varMac =NewMemcachedclient (config))//Create a connection            {                //for (int a = 0; a < 300000; a++)Parallel.For (0,300000, a =                {                    //STRINGMac. Store (Storemode.add,"TextBox1"+ a.tostring (), setstring);//WriteMac. Store (Storemode.set,"TextBox1"+ a.tostring (), setstring);//Update//JSONMac. Store (Storemode.add,"AA"+a.tostring (), obj.                    SerializeObject ()); //byte array                    varbytes =serializebinary (obj); Mac. Store (Storemode.add,"bytes"+a.tostring (), bytes); //string str = Mac. Get<string> ("TextBox1");//Get information//string cache = Mac.                    Get<string> ("AA"); //var cacheobj = cache.                    Deserializeobject<a> (); //var cachebytes = Deserializebinary (Mac. Get ("bytes") as byte[]) as A;                }); } SW.            Stop (); returnContent (SW.            Elapsedmilliseconds.tostring ()); //return Content (cachebytes.b + "<br/>" + cachebytes.c); //return Content (cacheobj.b + "<br/>" + cacheobj.c);        }        ///   <summary>           ///serialize to a binary byte array///   </summary>           ///   <param name= "Request" >the object to serialize</param>           ///   <returns>byte array</returns>            Public Static byte[] Serializebinary (Objectrequest) {            varSerializer =NewSystem.Runtime.Serialization.Formatters.Binary.BinaryFormatter (); using(MemoryStream Memstream =NewSystem.IO.MemoryStream ()) {Serializer.                Serialize (Memstream, request); returnMemstream.getbuffer (); }        }        ///   <summary>           ///deserialize an object from a binary array///   </summary>           ///   <param name= "BUF" >byte array</param>           ///   <returns>get the object</returns>            Public Static ObjectDeserializebinary (byte[] buf) {            using(MemoryStream Memstream =NewMemoryStream (BUF)) {memstream.position=0; varDeserializer =NewSystem.Runtime.Serialization.Formatters.Binary.BinaryFormatter (); Objectnewobj =Deserializer.                Deserialize (Memstream);                Memstream.close (); returnnewobj; }        }    }

    [Serializable]    public Class A    {public        string B {get; set;}        public string C {get; set;}    }

    usingNewtonsoft.json;  Public Static classjsonextensions {/// <summary>        ///object to JSON string///Serialization of/// </summary>        /// <param name= "O" ></param>        /// <returns></returns>         Public Static stringSerializeObject ( This Objecto) {if(O = =NULL)            {                return string.            Empty; }            returnNewtonsoft.Json.JsonConvert.SerializeObject (o); }         Public Static stringSerializeObject ( This ObjectO, Newtonsoft.Json.Formatting f) {            if(O = =NULL)            {                return string.            Empty; }            returnNewtonsoft.Json.JsonConvert.SerializeObject (o, f); }        /// <summary>        ///string parsing to object///deserialization/// </summary>        /// <typeparam name= "T" ></typeparam>        /// <param name= "str" ></param>        /// <returns></returns>         Public StaticT deserializeobject<t> ( This stringstr) {            Try            {                returnNewtonsoft.json.jsonconvert.deserializeobject<t>(str); }            Catch(Exception ex) {return default(T); }        }        /// <summary>        ///get the JSON object based on key/// </summary>        /// <typeparam name= "T" ></typeparam>        /// <param name= "str" ></param>        /// <param name= "key" ></param>        /// <returns></returns>         Public StaticT deserializeobject<t> ( This stringStrstringkey) {            Try            {                varp =Newtonsoft.Json.Linq.JObject.Parse (str); returnJsonconvert.deserializeobject<t>(P[key].            ToString ()); }            Catch(Exception) {}return default(T); }    }

The main one is to see the first controller class, followed by 2 helper classes and Poco entities, with methods for serializing and deserializing objects. The example shows a method for storing strings, byte arrays, and JSON with enyimmemcached. Personal preference json this.

Want to lazy walk csdn,3 points take away: http://download.csdn.net/detail/u012398331/8041697

  

Enyimmemcached (64-bit) Usage instance

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.