Memcache use in. Net

Source: Internet
Author: User

First, Memcache basic concept (socket server) Essence: Is a hash table stored in memory,the maximum value for key is 255 characters and the longest expiration time is 30 days features: Lazy deletion, no monitoring of data expiration mechanism, to achieve the most basic key-value storage,as soon as the memcache is restarted, the data disappears. Build a cluster: Cluster configuration is implemented through client-side drives, and memcache clusters do not communicate with each other How the client implements the cluster: In the Memcache configuration of multiple servers IP and port list, the client to the key hash processing to get hashcode, this hashcode to the number of machines to take the remainder, the data exist on the corresponding machine, read the same algorithm (simple remainder method, the shortcomings of expansion, Using Redis instead of better)

Basic concept: Memcache is the name of the project, Memcached is the name of the Memcache server that can execute the file

second, how to use:   1th Step:(administrator) folder where CD memcache is installed: memcached-d Install start: memcached-d start Uninstall: memcached-d stop memcached-d Uninst All2nd Step:Add Reference bottom is a Memche help class (Forget where copy, not the source forgive me)
1  Public Static classMemchehelper2     {3         Static ReadOnlyMemcachedclient mc=NULL;4         StaticMemchehelper ()5         {6             //best placed in configuration file7             string[] ServerList = {"127.0.0.1:11211","10.0.0.132:11211" };8 9             //Initialize PoolTenSockiopool pool =sockiopool.getinstance (); One pool. Setservers (serverlist); APool. Initconnections =3; -Pool. Minconnections =3; -Pool. MaxConnections =5; thePool. Socketconnecttimeout = +; -Pool. Sockettimeout = the; -Pool. Maintenancesleep = -; -Pool. Failover =true; +Pool. Nagle =false; - pool. Initialize (); +  A             //get the client instance atMC =Newmemcachedclient (); -Mc. EnableCompression =false; -         } -         /// <summary> -         ///This is the method of adding -         /// </summary> in         /// <param name= "key" ></param> -         /// <param name= "value" ></param> to         /// <returns></returns> +          Public Static BOOLSet (stringKeyObjectvalue) -         { the             returnMC. Set (key, value); *         } $          Public Static BOOLSet (stringKeyObjectValue,datetime time)//Specify expiration time, maximum 30 daysPanax Notoginseng         { -             returnMC. Set (key, value,time); the             +         } A         /// <summary> the         ///Get Data +         /// </summary> -         /// <param name= "key" ></param> $         /// <returns></returns> $          Public Static ObjectGet (stringkey) -         { -             returnMC. Get (key); the         } - Wuyi         /// <summary> the         ///Delete Data -         /// </summary> Wu         /// <param name= "key" ></param> -         /// <returns></returns> About          Public Static BOOLDelete (stringkey) $         { -             if(MC. Keyexists (key)) -             { -                returnMC. Delete (key); A             } +             return false; the         } -}

Easy to use in your code

 Static voidMain (string[] args) {            //Add, save one dayMemchehelper.set ("TestKey","TestValue", DateTime.Now.AddDays (1)); //Take valueMemchehelper.get ("TestKey"); //DeleteMemchehelper.delete ("TestKey"); }

Memcache use in. Net

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.