Memcached. NET client (i) ". NET Memcached Client library use" __.net

Source: Internet
Author: User
Tags memcached

Download the client's 3 DLLs,

Icsharpcode.sharpziplib.dll,log4net.dll,memcached.clientlibrary.dll

Create a new simple console application:

Class Program {private static void Main (string[] args) {Sockiopool pool = Sockiopool.get
            Instance ();
            String[] Servers = {"127.0.0.1:11211"}; Pool.
            Setservers (servers); Pool.
            Minconnections = 3; Pool.
            MaxConnections = 5; Pool.
            Initconnections = 3; Pool.
            Socketconnecttimeout = 5000; Pool.

            Initialize ();
            
            Memcachedclient client = new Memcachedclient (); Client.
            EnableCompression = false;
            Console.WriteLine ("-----------------------Memcached set setting Value--------------------------"); Client.
            Set ("Key1", "value1"); Console.WriteLine (client.
            Get ("Key1"));
            Console.WriteLine ("-----------------------Memcached Add Set Value--------------------------"); Client.
            ADD ("Key2", "value2"); Console.WriteLine (client.
            Get ("Key2")); Client.
            Set ("Key2", "value1 value2"); Console.WriteLine (Client.
            Get ("Key2"));
            Console.WriteLine ("-----------------------Memcached Replace Set Value--------------------------"); Client.
            Replace ("Key2", "value3"); Console.WriteLine (client.

            Get ("Key2"));
            Console.WriteLine ("-----------------------Whether there is a--------------------------for the Memcached key value"); if (client.
            Keyexists ("Key2")) {Console.WriteLine ("Key key2 exist"); } if (client.
            Keyexists ("hechen") = = False) {Console.WriteLine ("Key hechen does not exist");
            } Console.WriteLine ("-----------------------Memcached delete Data--------------------------"); Client.
            ADD ("Key4", "value4"); Console.WriteLine ("Key4==>" + client.
            Get ("Key4")); Client.
            Delete ("Key4"); if (!client.
            Keyexists ("Key4")) {Console.WriteLine ("Key4 deleted"); } Console.WriteLine ("-----------------------Memcached dataExpired--------------------------"); Client.
            ADD ("Key5", "Value5", DateTime.Now.AddMilliseconds (5000)); Console.WriteLine (client.
            Get ("Key5"));
            System.Threading.Thread.Sleep (6000); Console.WriteLine ("Expired:" + client.
             

            Get ("Key5"));
            Custom instantiation encapsulation, interface Icache cache = Memcache.instance (); Console.WriteLine (Cache.
            Containkey ("Hechen"));
            Student Student = new Student () {ID = 1, Name = "Hechen"}; Console.WriteLine (Cache.
            Add<student> ("Hechen", Student)); Console.WriteLine (Cache.
            Containkey ("Hechen")); Student stu = cache.
            Get<student> ("Hechen"); Console.WriteLine (Stu.id + ":" + stu.)
            Name); Cache.
            ADD ("QA", "ddddddddd"); Cache.
            Remove (); Student STU1 = cache.
            Get<student> ("Hechen"); if (stu1!= null) {Console.WriteLine (stu1.id + ":" + stu1.)
            Name);
     }       Console.WriteLine (Cache.


        Get ("QA")); }
    }
Customizing the Instantiated Interface ICache.cs code:

 Public interface Icache {bool Containkey (string argkey);

        BOOL Add (string Argkey,object argvalue);

        BOOL Add (String Argkey, Object Argvalue, DateTime argdateexpiration);

        BOOL Add<t> (String argkey, T entity) where T:class;

        BOOL Add<t> (String argkey, T entity, DateTime argdateexpiration) where T:class;

        BOOL Set (String Argkey, Object argvalue);

        BOOL Set (String Argkey, Object Argvalue, DateTime argdateexpiration);

        BOOL Set<t> (String argkey, T entity) where T:class;

        BOOL Set<t> (String argkey, T entity, DateTime argdateexpiration) where T:class;

        BOOL Replace (string Argkey,object argvalue);

        BOOL Replace (string argkey,object argvalue,datetime argdateexpiration);

        BOOL Replace<t> (String argkey, T entity) where T:class;

        BOOL Replace<t> (String argkey, T entity, DateTime argdateexpiration) where T:class; Object Get (String arGkey);

        T get<t> (string argkey);

        BOOL Remove (string argkey);

        BOOL Remove (String argkey, DateTime argdateexpiration);

        BOOL Remove ();

    BOOL Remove (ArrayList servers); }
The Memcache.cs code is as follows:

 public class Memcache:icache {private memcachedclient client;

        private static Memcache Memcache; <summary>///Construction Method///</summary> protected Memcache () {Socki
            Opool pool = sockiopool.getinstance ();
            String[] Servers = {"127.0.0.1:11211"}; Pool.
            Setservers (servers); Pool.
            Minconnections = 3; Pool.
            MaxConnections = 5; Pool.
            Initconnections = 3; Pool.
            Socketconnecttimeout = 5000; Pool.
            Initialize ();
            This.client = new Memcachedclient (); Client.
        EnableCompression = false; public static Memcache Instance () {if (Memcache = null) {MEM
            cache = new Memcache ();
        return memcache; ///<summary>///Determine if a key is included///</summary>///<param name= "Argkey" > Key value </param>///<returns></returns> public bool Containkey (string argkey) {RE Turn client.
        Keyexists (Argkey); ///<summary>///Add cached data///</summary>///<param name= "Argkey" > Key value &L
        t;/param>///<param name= "Argvalue" > Storage value </param>///<returns></returns> public boolean ADD (String Argkey, Object Argvalue) {return client.
        ADD (Argkey,argvalue); ///<summary>///Add cached data///</summary>///<param name= "Argkey" > Key value &L t;/param>///<param name= "Argvalue" > Store value </param>///<param name= "Argdateexpiration" > Expiration </param>///<returns></returns> public bool Add (string Argkey, Object Argvalue, Dat ETime argdateexpiration) {return client.
        ADD (Argkey, Argvalue, argdateexpiration);
}
        <summary>///Add cached data///</summary>///<typeparam name= "T" > Storage Object Type & lt;/typeparam>///<param name= "Argkey" > Key value </param>///<param name= "entity" > Store value </p aram>///<returns></returns> public bool Add<t> (string argkey, T entity) where T:C Lass {return client.
        ADD (Argkey, entity); ///<summary>///Add cached data///</summary>///<typeparam name= "T" > Storage object classes Type </typeparam>///<param name= "Argkey" > Key value </param>///<param name= "entity" > Store value < /param>///<param name= "argdateexpiration" > Expiration </param>///<returns></returns&gt
        ;  public bool Add<t> (string argkey, T entity, DateTime argdateexpiration) where T:class {return Client.
        ADD (Argkey, entity, argdateexpiration);

       } <summary>///Add cached data, replace existing data if present///</summary>///<param name= "Argkey" > Key value
        </param>///<param name= "Argvalue" > Storage value </param>///<returns></returns>
                public bool Set (string Argkey, Object Argvalue) {if (Containkey (Argkey)) {
            return false; Return to client.
        Set (Argkey, Argvalue); ///<summary>///Add cached data, replace existing data if present///</summary>///<param name= "arg Key > Key </param>///<param name= "Argvalue" > Store value </param>///<param name= "Argdateexp Iration "> Expiration </param>///<returns></returns> public bool Set (string Argkey, Object a Rgvalue, DateTime argdateexpiration) {if (Containkey (Argkey)) {return FA
            Lse Return to client. Set (Argkey, Argvalue, argdateexpiration); ///<summary>///Add cached data, replace existing data if present///</summary>///<typeparam name= "T" > Storage object Type </typeparam>///<param name= "Argkey" > Key value </param>///<param name= "entity "> Store value </param>///<returns></returns> public bool Set<t> (string argkey, T entit
            Y) where T:class {if (Containkey (Argkey)) {return false; Return to client.
        Set (Argkey, entity); ///<summary>///Add cached data, replace existing data if present///</summary>///<typeparam name= "T" > Storage object Type </typeparam>///<param name= "Argkey" > Key value </param>///<param name= "entity "> Store value </param>///<param name=" argdateexpiration "> Expiration </param>///<returns>&lt ;/returns> public bool Set<t> (string Argkey, T entity, DateTime argdateexpiration) where T:class {if (Containkey (Argkey))
            {return false; Return to client.
        Set (Argkey, entity, argdateexpiration); ///<summary>///Replace the original cache///</summary>///<param name= "Argkey" > Key value &
        lt;/param>///<param name= "Argvalue" > Storage value </param>///<returns></returns> public bool Replace (string Argkey, Object Argvalue) {return client.
        Replace (Argkey,argvalue); ///<summary>///replaces the original cache///</summary>///<param name= "Argkey" > Key value &L t;/param>///<param name= "Argvalue" > Store value </param>///<param name= "Argdateexpiration" >  Expiration Time </param>///<returns></returns> public bool Replace (string Argkey, Object Argvalue, DateTime ARGDATEEXPIRation) {return client.
        Replace (argkey,argvalue,argdateexpiration); ///<summary>///Replace the original cache///</summary>///<typeparam name= "T" > Storage type & lt;/typeparam>///<param name= "Argkey" > Key value </param>///<param name= "entity" > Store value </p  aram>///<returns></returns> public bool Replace<t> (string argkey, t entity) where T : class {return client.
        Replace (argkey,entity); ///<summary>///Replace the original cache///</summary>///<typeparam name= "T" > Storage type & lt;/typeparam>///<param name= "Argkey" > Key value </param>///<param name= "entity" > Store value </p
        aram>///<param name= "argdateexpiration" > Expiration </param>///<returns></returns> public bool Replace<t> (string argkey, T entity, DateTime argdateexpiration) where T:class {return client.
        Replace (Argkey, entity,argdateexpiration); ///<summary>///Get Cached data///</summary>///<param name= "Argkey" > Key value &L T;/param>///<returns></returns> public Object Get (string Argkey) {RE Turn client.
        Get (Argkey); ///<summary>///Get Cached data///</summary>///<typeparam name= "T" > Return Data Class
        Type </typeparam>///<param name= "Argkey" > Key value </param>///<returns></returns>
            Public T Get<t> (string argkey) {T Entity=default (t); entity = (T) client.
            Get (Argkey);
        return entity; ///<summary>///Removes a cached data///</summary>///<param name= "Argkey" > Key value </param>///<returns></returns> public bool Remove (string ARgkey) {return client.
        Delete (Argkey); ///<summary>///Removes a cached data///</summary>///<param name= "Argke Y > Key </param>///<param name= "argdateexpiration" > Data expiration </param>///<returns>& lt;/returns> public bool-Remove (String argkey, DateTime argdateexpiration) {return client.
        Delete (argkey,argdateexpiration); ///<summary>///Remove all cached data///</summary>///&LT;RETURNS&GT;&LT;/RETURNS&G
        T public bool Remove () {return client.
        Flushall (); ///<summary>///To remove all cached data///</summary>///<param name= "Servers" > Costume
            Service address </param>///<returns></returns> public bool Remove (ArrayList servers) { Return client.
        Flushall (servers); }
    }

Run the results as shown in figure:



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.