Use Memcached to Improve the Performance of. NET Applications

Source: Internet
Author: User

When an application is running, there will always be data that needs to be accessed frequently and does not change frequently. If you need to read the data from the database or external file system every time you obtain the data, performance will certainly be affected, so the common practice is to cache this part of the data, as long as the data does not change every time the data is obtained directly from the memory area, the performance will be greatly improved. In. NET, a Cache class is provided to implement these functions. In ASP. NET, you can obtain instances of this class through the Cache attribute of the HttpContext object or the Cache attribute of the Page object. In most cases, we can use the Cache class to improve ASP. but the Cache class also has some shortcomings. For example, we cannot specify the memory size occupied by the Cache class, in addition, the data cached in the Cache cannot be directly accessed by applications on another machine. Therefore, this article proposes another data Cache solution, that is, using distributed Cache. The distributed cache feature that the cached data does not need to be on the same machine as the application, thus greatly enhancing the reusability of the cached data. This article describes how to use Memcache as a distributed cache in. NET applications.
Introduction to Memcached
Memcached is a software developed by Brad Fitzpatric, Danga Interactive under LiveJournal. In common applications, we store the data in the database, and query the data from the database whenever needed, if many application users access the database in a large number of concurrent ways, the response time of the application will be increased. Memcached can effectively solve this 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, so as to speed up dynamic Web applications and improve scalability. Like the famous Facebook website, Memcached is used. Zhou Gong will later provide the 32-bit and 64-bit Memcached programs on the Windows platform.
To improve performance, data in Memcached is stored in the built-in storage space of Memcached. When Memcached is restarted, all the data is lost. Therefore, the general solution is to save the data in the database. Check whether the data is cached in Memcached each time you request the data, if yes, the data is directly retrieved from the cache. If no, the data is retrieved from the database and returned to the application and the requested data is cached in Memcached, in this way, the next request for the same data can be directly read from Memcached without having to query the database. Once the data is updated, both the database and Memcached are updated.
Memcached is a command line window program that can be started in the command line window or encapsulated in system services. When starting Memcached, you must provide some required parameters to specify the port listening for Memcached runtime and the maximum memory used. If the cached data exceeds the specified memory size, Memcached automatically deletes unused cache (marked as invalid) according to the LRU (Least Recently Used) algorithm ), the new cache data can use the memory occupied by the data marked as invalid, so that you do not have to worry about Memcached exceeding the specified memory. In addition, to improve performance, Memcached does not delete cached data from the physical memory after the cached data expires. It only checks whether the data has expired when it is retrieved and modified.
Currently, Memcached is available on multiple platforms, such as Linux, FreeBSD, Solaris (memcached 1.2.5 or later), Mac OS X, and Windows. On Windows, there are also 32-bit and 64-bit versions.
Memcached has a set of protocols that allow you to access Memcached data and view the status of Memcached. Many programming languages use this Protocol to operate Memcached, for example, PHP, Java, C, C ++, and C.
Get the Memcached version of the corresponding platform to run Memcached. Here, we only use 32-bit Memcached on Windows as an example.
Run Memcached:
Memcached.exe-p 11121-m 64
The above command is to run Memcached and specify that its listening port is 11121 (this is its default port, which can be another port greater than 1024, because the default value is already set for ports smaller than 1024), and the maximum memory usage is 64 mb. If Windows Firewall is enabled, open this port on the firewall.
You can run the following command line during program debugging:
Memcached.exe-p 11121-m 64-vv
The following result is displayed:
Slab class 1: chunk size 88 perslab 11915
Slab class 2: chunk size 112 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 was 8192, now 268435456
<116 server listening (udp)
You can also use telnet to view and operate Memcached on the client, provided that both the server and client support the Telnet protocol, which is not supported by Windows7 and Windows2008 by default, it must be installed and enabled in the control panel.
First open the control panel, and then click "enable or disable Windows", as shown in:


After you click "enable or disable Windows", you will see the status of the features enabled by the current system. Select to enable the Telnet server or client functions based on the current machine, as shown in:


After the above operations, you can remotely view the Memcached status or operate Memcached on the client. The following command is used to connect to Memcached:
Telnet local host 11121
After the connection, a command line window appears. In this command line window, enter "stats" to view the current Memcached status, as shown below:
STAT pid 852
Statuptime 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 performance_get 0
STAT performance_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
END
With this data, we can understand the status of Memcached.
The data represents the following meanings:
Pid: 32u, server process ID.
Uptime: 32u, server running time, in seconds.
Time: 32u, the current UNIX time of the server.
Version: string, the server version number.
Curr_items: 32u, number of contents currently stored by the server Current number of items stored by the server
Total_items: 32u, total number of contents stored since the server was started.
Bytes: 64u, the number of bytes occupied by the server's current storage content.
Curr_connections: 32u, number of connections.
Total_connections: 32u, total number of connections received since the server was running.
Connection_structures: 32u, number of connection structures allocated by the server.
Cmd_get: 32u, total number of retrieved requests.
Pai_set: 32u, total number of stored 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 sent by the server to the network.
Limit_maxbytes: 32u, total number of bytes that can be used by the server during storage.
In the above description, 32u and 64u indicate 32-bit and 64-bit unsigned integers, and string indicates string type data.
Apply Memcached in. NET
There are many. NET version Memcached client programs. Here, the Enyim Memcached used by Zhou Gong can go

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.