Comprehensive analysis of Windows Memcache technology applications

Source: Internet
Author: User

Original http://www.cnblogs.com/liuqin520/p/4615644.html

I. Introduction of Memcache

Memcache is a danga.com project, the first to serve LiveJournal, many people around the world use this cache project to build their own heavy load of the site, to share the pressure of the database. It can handle any number of connections, using non-blocking network IO. Because it works by opening up a space in memory, and then building a HashTable, Memcached self-manages these HashTable. Memcache is a high-performance distributed memory cache server. The general purpose is to reduce the number of database accesses by caching database query results to improve the speed and scalability of dynamic Web applications. Memcache official website: http://www.danga.com/memcached.

Second, memcache installation

Installation package will have x64 and x86 two folders, according to the operating system select an open will find Memcached.exe. This file cannot be directly double-clicked to run the installation and needs to be installed by CMD.

cmd command: memcache–d start|stop|shutdown|restart|uninstall|install Start | stop | shutdown | restart | uninstall | install.

Installation steps: 1. Locate the file Memcache.exe path;

2.memcache–d install installation completed;

3. Memcache.exe can be seen in Windows process after starting the service;

4. After the server installation is complete, we can test it by Telnet to the server (if you are prompted that the Telnet command does not exist, you need to go to the control Panel to turn on the Windows Telnet Service feature) For example: 10.0.27.120 is installed MEMC The IP address of the ache service, 11211 is the default port. Enter stats to view the parameter information.

All installations to this memcache are complete.

Application of Memcache in program

Create a new Memcache class library in the solution, referencing the following class libraries, Commons.dll,memcached.clientlibrary.dll,icsharpcode.sharpziplib.dll. New MemcacheHelper.cs the use class for this memcache. After the preparation work is done, it begins to enter the Memcache.

1. Configure the Memcache server address in the Web. config

<appSettings>

<!--memcache server configuration, IP address later change, port number fixed to 11211--><add key= "memcached.serverlist" value= "10.3.2.24:11211"/>

<appSettings>

2. Write implementation code inside the MemcacheHelper.cs class

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingmemcached.clientlibrary;usingSystem.Data;usingSystem.Configuration;usingNlp.common;namespaceNLP. memcache{ Public classMemcachehelper {#regionGlobal static objects//Global Socket Connection pool object    Private StaticSockiopool Sockiopool;  Public StaticSockiopool Currentpool {Get      {        returnSockiopool; }    }    //Global memcached Client Object    Private StaticMemcachedclient MC; #endregion     Public Static BOOLMemcachehelperinit () {Try      {        //Initialize the list of memcached servers        string[] ServerList = configurationmanager.appsettings["memcached.serverlist"]. Split (','); //initializing the socket connection poolSockiopool = Sockiopool.getinstance ("Mempool");        Sockiopool.setservers (serverlist);        Sockiopool.initialize (); //Initializing the memcached clientMC =Newmemcachedclient (); Mc. Poolname="Mempool"; Mc. EnableCompression=false; return true; }      Catch(Exception ex) {return false; }    }    /// <summary>    ///determine if the Pkey keyword is in the PMC/// </summary>    /// <param name= "PMC" ></param>    /// <param name= "PKey" ></param>    /// <returns></returns>     Public Static BOOLIscache (stringPKey) {      if(Memcachehelperinit ()) {if(MC. Keyexists (PKey)) {return true; }        Else        {          return false; }      }      Else      {        return false; }    }    /// <summary>    ///Erase memcache Data/// </summary>    /// <param name= "key" > </param>    /// <returns></returns>     Public Static BOOLRemoveCache (stringPKey) {      if(Memcachehelperinit ()) {if(!MC. Keyexists (PKey)) {return false; }        Else        {          returnMC.        Delete (PKey); }      }      Else      {        return false; }    }    /// <summary>    ///set-New or modified/// </summary>    /// <param name= "key" >Key</param>    /// <param name= "value" >value</param>    /// <returns>is successful</returns>     Public Static BOOLAddcache (stringKeyObjectvalue) {      if(Memcachehelperinit ()) {if(!MC. Keyexists (key)) {returnMC.        ADD (key, value); }        Else        {          returnMC.        Set (key, value); }      }      Else      {        return false; }    }    /// <summary>    ///set-New or modified/// </summary>    /// <param name= "key" >Key</param>    /// <param name= "value" >value</param>    /// <param name= "expiry" >Expiry Time</param>    /// <returns>is successful</returns>     Public Static BOOLAddcache (stringKeyObjectvalue, DateTime expiry) {      if(Memcachehelperinit ()) {if(!MC. Keyexists (key)) {returnMC.        ADD (key, value, expiry); }        Else        {          returnMC.        Set (key, value, expiry); }      }      Else      {        return false; }    }    /// <summary>    ///get Memcache data based on a single key value/// </summary>    /// <param name= "key" ></param>    /// <returns></returns>     Public Static ObjectGetCache (stringkey) {      if(Memcachehelperinit ()) {if(!MC. Keyexists (key)) {return NULL; }        Else        {          returnMC.        Get (key); }      }      Else      {        return false; }    }    /// <summary>    ///get Memcache data based on multiple key values/// </summary>    /// <param name= "key" > </param>    /// <returns></returns>     Public Staticdictionary<string,Object> GetCache (string[] keys) {Dictionary<string,Object> dic =Newdictionary<string,Object>(); if(Memcachehelperinit ()) {foreach(stringKeyinchkeys) {          Objectobj =MC.          Get (key); if(!dic. ContainsKey (key) && obj! =NULL) dic.        ADD (key, obj); }        returndic; }      Else      {        return NULL; }    }  }}

3. Call MemcacheHelper.cs's Memcache method inside the calling code

such as bool result= Memcachehelper.addcache ("User", "123456");//implement new key bit user,value value of 123456 memcache

Gets the value of Memcache in JSON format string Ob_json = Memcachehelper.getcache ("User"). ToString ();

This realizes the Memcache new, the query complete application.

Comprehensive analysis of Windows Memcache technology applications

Related Article

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.