Memcached for use in. NET Applications

Source: Internet
Author: User


In the process of running the application there will always be some data that often needs to be accessed and infrequently changed, if each time it is required to read from the database or external file system, performance is certainly affected, so it is common practice to cache this part of the data, As long as the data is not changed, the performance of the data directly from the central memory will be greatly improved each time it is acquired. A cache class is available in. NET to implement these capabilities. An instance of this class can be obtained from the HttpContext object's Cache property or the Page object's Cache property in ASP. In most cases we can use the cache class to improve the performance of the ASP, but there are some deficiencies in using the cache class, such as we cannot specify the size of the memory used by the cache class, and the data cached in the cache cannot be accessed directly by an application on another machine. Therefore, in this article, we propose another data caching scheme, that is, the use of distributed cache. The distributed cache is characterized by the fact that the cached data does not have to be on the same machine as the application, which greatly enhances the reusability of the cached data. The article describes how to do this in. NET application using Memcache as the distributed cache.
memcached Introduction
Memcached is a software developed by Brad Fitzpatric, a Danga Interactive company in LiveJournal. In the common application we will save the data to the database, every time we need to query the data from the database, if many users of the application will have a large number of concurrent access to the database, which will increase the response time of the application, using memcached can effectively solve the problem. Memcached 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. Like the famous Facebook site, memcached is used. Zhou Gong will provide 32-bit and 64-bit memcached programs on the Windows platform later.
To improve performance, the data in the memcached is stored in the memcached built-in storage space. Because when the memcached reboot will cause the data to be lost, the general scheme is to keep the data in the database, check the data in the memcached if there is no cache at the time of the request, and if there is any, fetch the data directly from the cache; The data is returned to the application from the database and the requested data is cached to memcached so that the next request for the same data can be read directly from the memcached without having to look up the database, and update the database and memcached as soon as the data is updated.
memcached is a command-line window program that can be launched in a command-line window or encapsulated in system services. You need to provide some necessary parameters when starting memcached, specifying the port to which the Memcached runtime listens and the maximum amount of memory used. If the cached data size exceeds the specified memory, then memcached will automatically "delete" the unused cache (marked as invalidated) according to the LRU (Least recently used) algorithm, and the new cache data can use the memory that is used to mark the data that is invalidated. This will not worry about memcached exceeding the specified memory. In addition, to improve performance, memcached does not delete cached data from physical memory after the cached data expires, and only checks to see if it has expired when the data is removed.
There are memcached versions of various platforms, such as Linux, FreeBSD, Solaris (memcached 1.2.5 or higher), MAC OS x and Windows platforms, and 32-bit and 64-bit versions on the Windows platform.
Memcached has a set of protocols that can be used to memcached data access and view memcached state, and many programming languages operate memcached based on this protocol, such as PHP, Java, C, C + + and C #.
The memcached version of the corresponding platform is available to run the memcached. This is the case only for the 32-bit memcached on the Windows platform.
Run memcached:
Memcached.exe-p 11121-m 64
The above command is run memcached, specifies that its listening port is 11121 (this is its default port, can be specified as other ports greater than 1024, because the port less than 1024 has been specified by default), the maximum use of memory is 64m, if Windows Firewall is enabled, Remember to open this port on the fire wall.
You can use the following command line to run the program when you debug it:
Memcached.exe-p 11121-m 64-VV
This will see the following result:
Slab class 1:chunk Size Perslab 11915
Slab class 2:chunk Size Perslab 9362
Slab class 3:chunk size 144 Perslab 7281
Slab class 4:chunk size 184 Perslab 5698
Slab class 5:chunk size 232 Perslab 4519
Slab class 6:chunk size 296 Perslab 3542
Slab class 7:chunk size 376 Perslab 2788
Slab class 8:chunk size 472 Perslab 2221
Slab class 9:chunk size 592 Perslab 1771
Slab class 10:chunk size 744 Perslab 1409
Slab class 11:chunk Size 936 Perslab 1120
Slab class 12:chunk size 1176 Perslab 891
Slab class 13:chunk size 1472 Perslab 712
Slab class 14:chunk size 1840 Perslab 569
Slab class 15:chunk size 2304 Perslab 455
Slab class 16:chunk size 2880 perslab 364
Slab class 17:chunk Size 3600 perslab 291
Slab class 18:chunk size 4504 Perslab 232
Slab class 19:chunk size 5632 Perslab 186
Slab class 20:chunk size 7040 Perslab 148
Slab class 21:chunk size 8800 Perslab 119
Slab class 22:chunk size 11000 Perslab 95
Slab class 23:chunk size 13752 Perslab 76
Slab class 24:chunk size 17192 Perslab 60
Slab class 25:chunk size 21496 Perslab 48
Slab class 26:chunk size 26872 Perslab 39
Slab class 27:chunk size 33592 Perslab 31
Slab class 28:chunk size 41992 Perslab 24
Slab class 29:chunk size 52496 perslab 19
Slab class 30:chunk size 65624 perslab 15
Slab class 31:chunk size 82032 perslab 12
Slab class 32:chunk size 102544 perslab 10
Slab class 33:chunk size 128184 Perslab 8
Slab class 34:chunk size 160232 Perslab 6
Slab class 35:chunk size 200296 Perslab 5
Slab class 36:chunk size 250376 Perslab 4
Slab class 37:chunk size 312976 Perslab 3
Slab class 38:chunk size 391224 Perslab 2
Slab class 39:chunk size 489032 Perslab 2
<96 Server Listening
<112 Server Listening
<116 send buffer is 8192, now 268435456
<116 Server Listening (UDP)
The client can also view and manipulate memcached via Telnet, provided that both the server side and the client support the Telnet protocol, which is not supported by default in Windows7 and Windows2008 and needs to be installed and enabled in the Control Panel.
First open Control Panel and then click "Turn Windows features on or off" as shown in:

Click "Turn Windows features on or off" to see the status of the current system enabled features, depending on the current machine selection to open the Telnet server side or client features as shown in:

After the above operation, you can remotely view the memcached status or Operation memcached on the customer service side. The following command is connected to the memcached:
telnet localhost 11121
A command-line window will appear after the connection, and you can see the status of the current memcached by entering "stats" in this command-line window, as follows: The status data of the memcached that is just started.
STAT Uptime 1399
STAT Time 1300979378
STAT version 1.2.5
STAT Pointer_size 32
STAT Curr_items 0
STAT Total_items 0
STAT bytes 0
STAT Curr_connections 3
STAT total_connections 5
STAT Connection_structures 4
STAT Cmd_get 0
STAT Cmd_set 0
STAT Get_hits 0
STAT get_misses 0
STAT Evictions 0
STAT Bytes_read 23
STAT Bytes_written 415
STAT limit_maxbytes 67108864
STAT Threads 1
With this data we can understand the state of memcached.
These data represent the following meanings:
PID:32U, the server process ID.
UPTIME:32U, server run time, in seconds.
TIME:32U, the current Unix time of the server.
Version:string, the version number of the server.
CURR_ITEMS:32U, the amount of content currently stored by the server is current number of items stored by the server
TOTAL_ITEMS:32U, the total number of content that has been stored since the server started.
Bytes:64u, the number of bytes consumed by the server currently storing content.
CURR_CONNECTIONS:32U, number of connections.
TOTAL_CONNECTIONS:32U, the total number of connections accepted since the server was running.
CONNECTION_STRUCTURES:32U, the number of connection structures that the server allocates.
CMD_GET:32U, the total number of fetch requests.
CMD_SET:32U, the total number of storage requests.
GET_HITS:32U, the total number of successful requests.
GET_MISSES:32U, the total number of failed requests.
Bytes_read:64u, the total number of bytes that the server reads from the network.
Bytes_written:64u, the total number of bytes that the server sends to the network.
LIMIT_MAXBYTES:32U, the total number of bytes that the server is allowed to use when it is stored.
In the above description, 32u and 64u represent 32-bit and 64-bit unsigned integers, and string represents data of type string.

Applying memcached in. Net
There are many. NET version of the Memcached client program, where the Enyim Memcached used by Zhou Gong, can download the latest version to
To use memcached in your project, you need to add an app to Enyim.Caching.dll. In addition, we may also need to configure the config file memcached information (also can be specified in the program code, but that is not flexible), the following is a config file configuration example:

<?xml version="1.0"encoding="Utf-8"?> <configuration> <configSections> <sectiongroup name=""> <section name="memcached"Type="Enyim.Caching.Configuration.MemcachedClientSection, enyim.caching"/> </sectionGroup> </configSections> < protocol="Binary"> <memcached> <servers> <add address="localhost"port="11121"/> <!--<add address="localhost"port="11131"/> <add address="localhost"port="11141"/> <add address="localhost"port="11151"/>--> </servers> <socketpool minpoolsize="Ten"Maxpoolsize=" -"connectiontimeout="00:00:10"deadtimeout="00:02:00"/> </memcached> </> </configuration>

If we configure multiple instances of memcached, you can add multiple memcached instance configurations under the <servers> node as described in the comments section above.
It is necessary to note that if we need to add a custom data type to memcached, we need to add the data type to the [Serializable] tag.
Here is an example of a enyim memcached:

    usingSystem; usingSystem.Collections.Generic; usingSystem.Linq; usingSystem.Text; usingenyim.caching; usingEnyim.Caching.Memcached; /** Zhou Gong (ZHOUFOXCN) * Date: 2011-03-24 * Source:HTTP://BLOG.CSDN.NET/ZHOUFOXCNor * Copyright NOTE: This article may be used for non-commercial purposes in the case of the source of the original source, the Zhou public does not make any warranty or commitment. * */     namespaceMemcachedmonitor {[Serializable] Public classPerson { Public intUserId {Get;Set; }  Public stringUserName {Get;Set; } }       Public classMemcacheddemo {Private StaticMemcachedclient client =NewMemcachedclient ("");  Public voidSetdemo () { person person=Newperson {UserId =1, UserName ="Li Gang" }; //storage with no expiration time, memcached will determine expiration policy based on LRU            BOOLsuccess=client. Store (Storemode.add, person.              UserName, person); //cache with Expiration time//BOOL success = client. Store (Storemode.add, person.  UserName, Person, DateTime.Now.AddMinutes (10)); Console.WriteLine ("the result of storing [{0}]: {1}", person.          UserName, success); }                Public voidGetdemo () { person person= client. Get<person> ("Li Gang"); if(Person! =NULL) {Console.WriteLine ("retrieving results of [{0}]--userid:{1},username:{2}","Li Gang", person. UserId, person.              UserName); }              Else{Console.WriteLine ("failed to retrieve [{0}]!","Li Gang"); }          }                Public voidMultigetdemo () {List<string> personnamelist =Newlist<string>();  for(inti =0; I <Ten; i++) {Personnamelist.add ("Li Gang xx"+i); }              //bulk Fetch, retrieving all specified data from all personnamelist in a single network communicationidictionary<string,Object> resultlist =client.              Get (personnamelist);              person person; foreach(keyvaluepair<string,Object> Iteminchresultlist) { person= Item. Value asPerson ; if(Person! =NULL) {Console.WriteLine ("retrieving results of [{0}]--userid:{1},username:{2}","Li Gang", person. UserId, person.                  UserName); }                  Else{Console.WriteLine ("failed to retrieve [{0}]!","Li Gang"); }              }          }      }      } 

Note: If you need to retrieve more than one cached data from memcached at a time, you can refer to the Multigetdemo () method, so that a dozen needs one network communication to retrieve all the data and reduce network connection time. In addition, the memcached client can use the text or binary protocol, and the binary protocol performs slightly better than using the text protocol after the week-to-day test comparison. In the config file above, the Zhou public is configured to use the binary protocol.
Summary, the use of memcached such a distributed cache can greatly improve the performance of the application, after the weeks of public testing, the correct use of memcached can be a single server to increase the number of concurrent access from 20 to about 1000, that is, 50 times times the increase, which is a fairly objective promotion!

Memcached for use in. NET 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: and provide relevant evidence. A staff member will contact you within 5 working days.

Tags Index: