The. NET Framework provides a cache object for us to process the system cache, which is very convenient to use,
I believe that you should be familiar with cache objects. However, the cached object stores the local memory, which limits the size and quantity of the cached object.
In this article, I want to introduce an open-source project: memcached (a project that provides distributed cache policies)
Memcached is widely used in some large Java and PHP projects (such as Facebook ). Use memcached to create a manageable and relatively independent cache layer to reduce frequent access between applications and databases. This is the charm of memcached.
This article does not discuss the internal implementation mechanism of memcached. It mainly describes the specific usage of memcached (more specifically, it should be in memcached. . Net ).
Memcached is mainly used in two parts: memcached server setup and memcached client call.
The following describes the specific operations.
MemcachedServer-side setup: Download The memcached Win32 Server installation package from the Internet. This article uses the memcached-1.2.1-win32 edition.
Decompress the file to a directory named memcached. Open the command line window, run the doscommand to jump to the memcached directory, and execute the following command:
C: "memcached> memcached.exe-D install
C: "memcached> memcached.exe-d start
The-D parameter is used to enable the daemon. Port 11211 of the local machine is enabled by default.
You can use-h to view other parameters.
In this way, our server is ready and easy to use.
The command to uninstall is:
C: "memcached> memcached.exe-d stop
C: "memcached> memcached.exe-D uninstall
MemcachedClient call:We also download the memcached. Net (C #) Client API package from the Internet,
This document uses memcacheddotnet_clientlib-1.1.5.zip.
Note:One client can call multipleMemcachedServer. (That is, a project can have multiple memcached instances.Server) Of course, these memcachedThe server should be on different servers, otherwise it makes no sense.
The following example is used to explain how to useMemcachedClientapi.
Create a website project.
We will introduce commons. dll; icsharpcode. sharpziplib. dll; memcached. clientlibrary. dll in the project.
The code in default. aspx. CS is as follows:
Code
Using system;
Using system. collections;
Using system. configuration;
Using system. Data;
Using system. LINQ;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. htmlcontrols;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. xml. LINQ;
Using memcached. clientlibrary;
Namespace memcachled
{
Public partial class _ default: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
String [] serverlist = {"127.0.0.1: 11211"}; // server list, which can be multiple
Sockiopool pool = sockiopool. getinstance ();
/** // Modify the following parameters according to the actual situation
Pool. setservers (serverlist );
Pool. initconnections = 3;
Pool. minconnections = 3;
Pool. maxconnections = 5;
Pool. socketconnecttimeout = 1000;
Pool. Fig = 3000;
Pool. maintenancesleep = 30;
Pool. Failover = true;
Pool. Nagle = false;
Pool. initialize (); // initialize the pool for memcache servers
Memcachedclient MC = new memcachedclient (); // initialize a client
MC. enablecompression = false;
MC. Add ("testadd", "added data"); // Add a cached data
Response. write ("added cache data:" + "key: testadd" + "value:" + MC. get ("testadd "). tostring (); // retrieves the cache data you just added
Response. Write ("<br/> ");
MC. Set ("testadd", "modified data ");
Response. Write ("modify a cached data:" + "key: testadd" + "value:" + MC. Get ("testadd"). tostring ());
Response. Write ("<br/> ");
MC. Delete ("testadd"); // delete an existing Cache
Sockiopool. getinstance (). Shutdown (); // close
}
}
}
I will not post the results.
In this article, memcacheddotnet_clientlib-1.1.5.zipand memcached-1.2.1-win32.zip will be sorted out and published tomorrow. Very late ~.
I'm in a hurry to write something wrong. Sorry!