memcached Configuration and Caching Knowledge overview

Source: Internet
Author: User
Tags mysql query

First look at the Baidu Encyclopedia in the face of cache introduction:

Caching (cache memory) is a memory chip on the hard disk controller with extremely fast access speed, which is the buffer between the internal storage of the hard disk and the external interface. Because the internal data transmission speed of the hard disk and the external interface transmission speed is different, the cache plays a role in Buffering. The size and speed of the cache are important factors that directly affect the transmission speed of the hard disk , and can greatly improve the overall performance of the hard disk . When the hard disk accesses fragmented data need to constantly exchange data between the hard disk and memory, there is a large cache, you can temporarily cache those fragmented data, reduce the load on the system, but also improve the speed of data transmission.

This is a description of the computer's hard disk cache. Of course, the computer contains many kinds of caches, not the focus of this article. We focus on several key points of this passage: speed, performance, and reduced system load, which is the advantage of caching.

Asp. NET Cache Overview:

Typically, applications can improve performance by storing data that is frequently accessed, as well as those that require a lot of processing time to be created in memory. For example, if your application uses complex logic to process large amounts of data, and then returns the data as a report that is frequently accessed by users, avoid recreating the report each time the user requests the data to improve efficiency. Similarly, if your application contains a page that handles complex data but does not require frequent updates, the server re-creates the page every time it is requested, making it inefficient.

In these cases, to help you improve the performance of your application, ASP. NET uses two basic caching mechanisms to provide caching functionality. The first mechanism is the application cache, which allows you to cache the generated data, such as a DataSet object or a custom report business object. The second mechanism is the page output cache, which saves the page processing output and reuses the saved output when the user requests the page again, rather than processing the page again. (excerpt from MSDN)

Scope of Cache application:

1. Frequently accessed data
Cause: Improve performance. The application uses complex logic to process large amounts of data and then returns the data as a report frequently accessed by the user, avoiding the ability to re-create the report every time the user requests the data.

2. Data that requires a lot of processing time to create
Cause: Improved performance. If your application contains a page that handles complex data but does not require frequent updates, the server re-creates the page every time it is requested, making it inefficient.

Asp. NET Cache application:

Asp. NET to manage the application cache through cache and also provides a cache of pages.

1, page output cache: caches the entire page, which is read directly from memory when the user requests it without the life cycle of the page processing. Primarily for pages that are not frequently modified, or for pages that require a lot of processing for compilation.

2, page partial output cache: caches part of the page, which is the cache user control (. ascx).

3, Application caching: provides a programmatic way to implement caching, by storing arbitrary data in memory in the form of key-value pairs, the application cache is easily lost, and the application cache is used to determine whether the item exists in the cache when an item is accessed and, if so, if it exists. If the item does not exist, you can recreate the item and put it back in the cache. This mode ensures that the most up-to-date data is always available in the cache.

4, Cache dependency: Net 2.0 introduces custom cache dependencies, especially based on the SqlCacheDependency feature of Ms-sql server, which allows us to avoid "data expiration", which can inform the cache based on changes in the data in the database, and remove those outdated data. You can refer to one of my blog posts configuring cache dependency under ASP. SQLServer2005 Cache Dependency

The cache has its advantages and disadvantages, should be used when the use of the situation, in the early stages of the project should be reserved cache interface, in the late phase of the project should do performance optimization plus caching.

Enter this topic memcached

For a new application our developers are primarily concerned about what it is, what it can do, what advantages and disadvantages it has. How to use ...

What is memcached?

Memcached is a high-performance, distributed memory object caching system for reducing database load and increasing access speed in dynamic applications.

What can memcached do?

By maintaining a unified, huge hash table in memory, memcached can be used to store data in a variety of formats, including images, videos, files, and the results of database retrieval.

Advantages and disadvantages of memcached

Memcache very fast, memcached used Libevent (using Epoll under Linux if possible) to equalize any number of open links, use non-blocking network I/O, and implement reference counting on internal objects (therefore, for a variety of clients, objects can be in a variety of states, using their own page block allocators and hash tables, so that virtual memory is not fragmented and the time complexity of virtual memory allocation is guaranteed to be O (1).

The memcached cache is distributed and can be accessed simultaneously by multiple users on different hosts, thus solving the limitations of shared memory only for single-machine applications, and less disk overhead and blocking when using databases to do similar things. Of course memcached can also be used locally, but it's not worth the cost compared to the ASP.

Now more commonly used MySQL database, mysql default to open the query cache, when the data table update, the query cache will be updated, if frequent occurrence of this update will cause the query cache is very inefficient. If the MySQL database is small, you can use the MySQL query cache. Otherwise the memcached is more appropriate.

There is also a way of sharing memory in which multiple processes or threads share the same cache. Although the efficiency has improved a lot, but after all, can only be confined to one machine.

Because memcached is also memory management, so it is easy to lose, the theoretical memcached can reach the maximum number of concurrent connections is 200, this value should be adjustable, also depends on the configuration of the machine. Because memcached is distributed, its biggest bottleneck is network connectivity.

memcached How to use

1,windows under Installation memcached

Memcached Current version is 1.2.6

Start of service:

1, the Memcached-1.2.1-win32.zip will be resolved to the specified place, such as e:\memcached

2 command line input ' e:\memcached\memcached.exe-d install '
3 command line input ' E:\memcached\memcached.exe-d start ', the command starts memcached, the default listener port is 11211

2,ubuntu under Installation memcached

Start the terminal and enter it under the terminal

sudo apt-get install memcached

Now that the installation is successful, start the service,

memcached-d-U Root

Please refer to the instructions below for specific commands.

Common settings:
-P <num> Monitored ports
-L <ip_addr> connected IP address, default is native
-D Start memcached service
-D Restart Restart memcached service
-D Stop|shutdown Close the running memcached service
-D Install memcached service
-d Uninstall Uninstall memcached service
-U <username> Run as <username> (only valid when run as root)
-M <num> maximum memory usage, in megabytes. Default 64MB
-M running out of memory and returning an error instead of deleting an item
-C <num> Maximum number of simultaneous connections, default is 1024
-F <factor> block size growth factor, default is 1.25
-N <bytes> Minimum allocated space, key+value+flags default is 48
-H Display Help

. NET client Tools There are many kinds, I use http://sourceforge.net/projects/memcacheddotnet/

Run the following code to test it.

static void Main (string[] args)
{
String[] ServerList = {"192.168.0.129:11211"};
Initialize Pool
Sockiopool sock = Sockiopool.getinstance ();
Sock. Setservers (serverlist);

Sock. Initconnections = 3;
Sock. Minconnections = 3;
Sock. MaxConnections = 5;

Sock. socketconnecttimeout = 1000;
Sock. Sockettimeout = 3000;

Sock. Maintenancesleep = 30;
Sock. Failover = true;

Sock. Nagle = false;
Sock. Initialize ();

Get the Client instance

Memcachedclient memcached = new Memcachedclient ();
Memcached. EnableCompression = false;

Console.WriteLine ("-------memcached test-------");
Memcached. Set ("Key1", "Value1");
if (memcached. Keyexists ("Key1"))
{
Console.WriteLine ("Presence Key1..");
Console.WriteLine (memcached. Get ("Key1"). ToString ());
}
Else
{
Console.WriteLine ("Key1 Key Not Present");
}

Console.read ();
Sockiopool.getinstance (). Shutdown (); Close the pool, close sockets
}

The Ubuntu,ip I installed in the virtual machine was 192.168.0.192. After the code runs successfully, the memcached configuration succeeds.

Follow-up details of. NET Operations memcached ...

memcached Configuration and Caching Knowledge overview

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.