Using memcached to improve the performance of. NET Applications

Source: Internet
Author: User

Using memcached to improve the performance of. NET Applications 2011-03-27 20:31:26

Tags: distributed cache. NET Memcached Performance Performance original works, allow reprint, please be sure to use hyperlinks in the form of the original source of the article, author information and this statement. Otherwise, the legal liability will be investigated. http://zhoufoxcn.blog.51cto.com/792419/528212

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 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 is not accessed directly by an application on another machine, so this article presents 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 IntroductionMemcached 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 benefitThis protocol can be used to memcached data access and view the status of Memcached, many programming languages based on the protocol to operate memcached, such as PHP, Java, C, C + + and C # and so on. 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 above command is run memcached, specifies that its listening port is 11121 (this is its default port, can be specified as other than 1024 of the port, Because a port less than 1024 already has the default designation, the maximum memory usage is 64m, and if Windows Firewall is enabled, remember to open this port on the firewall. You can use the following command line to run the debugger: Memcached.exe-p 11121-m 64-VV you will see the following result: Slab class   1:chunk size   & nbsp 11915 Slab class   2:chunk size    perslab perslab  9362 Slab class   3:c Hunk size    144 perslab  7281 slab class   4:chunk size    184 Perslab&nbs P 5698 Slab class   5:chunk size    232 perslab  4519 Slab class   6:chunk size&nb sp;   296 perslab  3542 slab class   7:chunk size    376 perslab  2788 Slab class   8:chunk size    472 PERSLAB&NBsp 2221 Slab class   9:chunk size    592 perslab  1771 Slab class  10:chunk SIZE&NBSP;&N bsp;  744 perslab  1409 slab class  11:chunk size    936 perslab  1120 Slab class&nbsp ; 12:chunk size   1176 perslab   891 slab class  13:chunk size   1472 Perslab &nbs P 712 Slab class  14:chunk size   1840 perslab   569 slab class  15:chunk size   2 304 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   2 Slab class  19:chunk size   5632 perslab   186 slab class  20:chunk size   70 perslab   148 Slab class  21:chunk size   8800 perslab   119 Slab class  22:c Hunk size  11000 perslab    Slab class  23:chunk size  13752 perslab   -Slab Class&nbs P 24:chunk size  17192 perslab    slab class  25:chunk size  21496 perslab  &nb Sp class  26:chunk size  26872 perslab    slab slab class  27:chunk size  33592 pe rslab    Slab class  28:chunk size  41992 perslab    in Slab class  29:c Hunk size  52496 perslab    slab class  30:chunk size  65624 perslab    1 5 Slab class  31:chunk size  82032 perslab    slab class  32:chunk size 102544 perslab& nbsp;   Slab class  33:chunk size 128184 perslab     8 slab class  34:chunk s ize 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   &nbs P 3 Slab class  38:chunk size 391224 perslab     2 slab class  39:chunk size 489032 perslab& nbsp;    2 <96 Server listening <112 server listening <116 send buffer is 8192, now 268435456 &lt The Listening server (UDP) can also view and manipulate memcached via telnet on the client side, provided that both the server and the client support the Telnet protocol, which is not supported by default in Windows7 and Windows2008. Need 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 a command-line window that appears after connecting to the memcached:telnet localhost 11121 connection, and entering "stats" in this command-line window to see the current memcached state. The following is the status data for the memcached that just started: Stat PID 852 stat uptime 1399 stat time 1300979378 stat version 1.2.5 stat pointer_size stat C Urr_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 Stat bytes_written 415 Stat limit_maxbytes 67108864 Stat threads 1 END With this data we can understand the status of Memcached. These data represent the following meanings: PID:32U, 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 current number of content that the server currently stores is the number of items stored by the server total_items:32u, the total amount 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.

There are many applications for memcached in. NET . NET version of the Memcached client program, where the Enyim Memcached used by Zhou Gong, can download the latest version to https://github.com/enyim/EnyimMemcached/. 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:

  1. <? XML version= "1.0" encoding="Utf-8" ?>
  2. <configuration>
  3. <configsections>
  4. <sectiongroup name="enyim.com">
  5. <section name="memcached" type= "Enyim.Caching.Configuration.MemcachedClientSection, Enyim.caching " />
  6. </sectiongroup>
  7. </configsections>
  8. <enyim.com protocol="Binary">
  9. <memcached>
  10. <servers>
  11. <add address="localhost" port="11121" />
  12. <!--<add address="localhost" port="11131" />
  13. <add address="localhost" port="11141" />
  14. <add address="localhost" port="11151" />-->
  15. </Servers>
  16. <socketpool minpoolsize="Ten" maxpoolsize= " connectiontimeout=" 00:00:10 " deadtimeout="00:02:00 " />
  17. </memcached>
  18. </enyim.com>
  19. </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:

  1. Using System;
  2. Using System.Collections.Generic;
  3. Using System.Linq;
  4. Using System.Text;
  5. Using Enyim.caching;
  6. Using Enyim.Caching.Memcached;
  7. /*
  8. * Zhou Gong (ZHOUFOXCN)
  9. * Date: 2011-03-24
  10. * Source: HTTP://BLOG.CSDN.NET/ZHOUFOXCN or http://zhoufoxcn.blog.51cto.com
  11. * Copyright NOTE: This article may be used for non-commercial purposes in the case of the original source, and the Zhou public does not make any warranty or commitment.
  12. * */
  13. Namespace Memcachedmonitor
  14. {
  15. [Serializable]
  16. Public class person
  17. {
  18. public int UserId { get; set;}
  19. public string UserName { get; set;}
  20. }
  21. Public class Memcacheddemo
  22. {
  23. private static memcachedclient client = new Memcachedclient ("enyim.com/memcached");
  24. public void Setdemo ()
  25. {
  26. Person person = new Person {UserId = 1, UserName = "Li Gang"};
  27. //Without expiration time, memcached will determine expiration policy based on LRU
  28. bool Success=client. Store (Storemode.add, person. UserName, person);
  29. //cache with Expiration time
  30. //bool success = client. Store (Storemode.add, person. UserName, Person, DateTime.Now.AddMinutes (10));
  31. Console.WriteLine ("storage [{0}] Result: {1}", person. UserName, success);
  32. }
  33. public void Getdemo ()
  34. {
  35. Person person = client. Get<person> ("Li Gang");
  36. if (person! = null)
  37. {
  38. Console.WriteLine ("Retrieving results of [{0}]--userid:{1},username:{2}", "Li Gang", person. UserId, person. UserName);
  39. }
  40. Else
  41. {
  42. Console.WriteLine ("Retrieve [{0}] failed!", "Li Gang");
  43. }
  44. }
  45. public void Multigetdemo ()
  46. {
  47. list<string> personnamelist = new list<string> ();
  48. For (int i = 0; i <; i++)
  49. {
  50. Personnamelist.add ("Li gang xx" + i);
  51. }
  52. //Bulk Fetch, retrieving all of the specified data from all personnamelist in a single network communication
  53. idictionary<string, object> resultlist = client. Get (personnamelist);
  54. person person;
  55. foreach (keyvaluepair<string, object> item in resultlist)
  56. {
  57. Person = Item. Value as person ;
  58. if (person! = null)
  59. {
  60. Console.WriteLine ("Retrieving results of [{0}]--userid:{1},username:{2}", "Li Gang", person. UserId, person. UserName);
  61. }
  62. Else
  63. {
  64. Console.WriteLine ("Retrieve [{0}] failed!", "Li Gang");
  65. }
  66. }
  67. }
  68. }
  69. }

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! Confined to space, the deeper and more detailed usage of memcached is not described in this article, this article is considered as a catalyst, readers can refer to other relevant information.

Using memcached to improve the performance of. NET Applications

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.