Introduction to cache app--memcached distributed cache

Source: Internet
Author: User
Tags memcached

I. what is memcached

Memcached is a high-performance distributed memory object caching system for dynamic Web applications to mitigate database load. It provides a dynamic, database-driven site speed by caching data and objects in memory to reduce the number of times the database is read.

Believe that many people have used the cache, in. NET also has a built-in caching mechanism, there are many third-party tools such as Apache,nginx can do static resources cache, and we can also develop their own caching mechanism, cache database query data to reduce the frequent operation of the database. But most of the time we always feel that these caches are always unsatisfactory, and memcached can solve many of your annoying problems. At least I solved a lot of problems in my study, so I decided to record and share them.

Memcached is based on a hashmap that stores key/value pairs. Its daemon is written in C, but the client can be written in any language (this article uses C # as an example) and communicates with the daemon through the memcached protocol. Maybe these things are too advanced, we do not do research at this stage.

Two. Distributed cache

In fact, memcached as a distributed cache data service, but there is no communication between each service at all, it may be a bit different from my understanding of the distribution, it may be my caishuxueqian, it may be the angle of thinking of each person. The Memcached client is a distributed algorithm that saves data to a different Memcached server and caches the data. Distributed cache, can be known memcached can be a large amount of data cache. This will make up for what many of us have encountered before. Caching data to the application server, and only a small amount of data can be cached, or the impact on the application server is very large.
memcached Application mechanism diagram:

This figure is a bit primitive, but the problem can be described clearly, the basic principle of caching mechanism is to first query data saved to memcached, the address in this request directly from the memcached cache data, so that can reduce the pressure on the server request.

Three. Memcached Features

(1) The protocol is simple: Do not use complex XML format, but use text format

(2) event handling mechanism based on libevent (not understood)

(3) Internal memory storage: Data is present in memory, so restarting the machine can result in data loss

(4) Memcached distributed between non-communication: Memcached server does not communicate, the data are stored through the client's distributed algorithm to each server

Four. Installation of memcached

First, this is a test on a Windows system, and memcached performance will be higher on non-Windows platforms such as Linux.

Download memcached server: http://memcached.org/(search for the applicable version of the website)

1 unzip the file to D:\Program files\memcached
2 command Line input D:\Program files\memcached\memcached.exe-d install
3 command Line Input D:\Program files\memcached\memcached\memcached.exe-d start, the command starts Memcached, the default listener port is 11211

Of course we can see this service in the Windows service:

five . NET platform using memcached

Because the personal first contact of the client is memcacheddotnet, so accustomed to the application of this core library as a client. The download work is very simple, because the client has helped us to implement the distributed program algorithm, we care as long as how to exist to obtain this data. Here's a quick introduction:

1 static void Main (string[] args)
2 {
3 Sockiopool pool = sockiopool.getinstance ();
4 string[] Servers = {"127.0.0.1:11211"};
5 pool. Setservers (servers);
6 pool. Minconnections = 3;
7 pool. MaxConnections = 5;
8 pool. Initconnections = 3;
9 pool. Socketconnecttimeout = 5000;
Ten pool. Initialize ();
11
Memcachedclient client = new Memcachedclient ();
Client. EnableCompression = false;
Console.WriteLine ("-----------------------Memcached set setting Value--------------------------");
Client. Set ("Key1", "value1");
Console.WriteLine (client. Get ("Key1"));
Console.WriteLine ("-----------------------Memcached Add Setting Value--------------------------");
Client. ADD ("Key2", "value2");
Console.WriteLine (client. Get ("Key2"));
Client. Set ("Key2", "value1 value2");
Console.WriteLine (client. Get ("Key2"));
Console.WriteLine ("-----------------------Memcached Replace Set Value--------------------------");
The client. Replace ("Key2", "value3");
Console.WriteLine (client. Get ("Key2"));
25
Console.WriteLine ("-----------------------Memcached key value exists--------------------------");
if (client. Keyexists ("Key2"))
28 {
Console.WriteLine ("Key Key2 exists");
30}
if (client. Keyexists ("Hechen") ==false)
32 {
Console.WriteLine ("Key Hechen not Present");
34}
35
Console.WriteLine ("-----------------------Memcached delete Data--------------------------");
The PNS client. ADD ("Key4", "value4");
Console.WriteLine ("Key4==>" + client. Get ("Key4"));
The client. Delete ("Key4");
if (!client. Keyexists ("Key4"))
41 {
Console.WriteLine ("Key4 has been deleted");
43}
44
Console.WriteLine ("-----------------------Memcached data Expires--------------------------");
The client. ADD ("Key5", "Value5", DateTime.Now.AddMilliseconds (5000));
Console.WriteLine (client. Get ("Key5"));
System.Threading.Thread.Sleep (6000);
Console.WriteLine ("Expired:" + client. Get ("Key5"));
50}

The above example is simple, including some basic usage, which is usually used when using memcached to cache data.

Memcached Initializing data
Sockiopool pool = sockiopool.getinstance ();
String[] Servers = {"127.0.0.1:11211"};
Pool. Setservers (servers);
Pool. Minconnections = 3;
Pool. MaxConnections = 5;
Pool. Initconnections = 3;
Pool. Socketconnecttimeout = 5000;
Pool. Initialize ();


Sockiopool is used to initialize the distributed cache pool of objects, which set a variety of properties, I believe that the meaning of these attributes are all understood. It is important to note that initialize () is the only way to initialize the data after it is called. To use the connection cache pool.

Memcached three ways to set cache values
Memcachedclient client = new Memcachedclient ();
Client. EnableCompression = false;
Console.WriteLine ("-----------------------Memcached Set Value--------------------------");
Client. Set ("Key1", "value1");
Console.WriteLine (client. Get ("Key1"));
Console.WriteLine ("-----------------------Memcached Add Setting Value--------------------------");
Client. ADD ("Key2", "value2");
Console.WriteLine (client. Get ("Key2"));
Client. Set ("Key2", "value1 value2");
Console.WriteLine (client. Get ("Key2"));
Console.WriteLine ("-----------------------Memcached Replace Set Value--------------------------");
Client. Replace ("Key2", "value3");
Console.WriteLine (client. Get ("Key2"));

A heart can test these three methods: Set () If the cache has the same key value, this replaces the original, add () just adds the data, if there is the same key is no longer added. Replace () replaces the same key value that you already have.

1 Console.WriteLine ("-----------------------Memcached data Expires--------------------------");
2 client. ADD ("Key5", "Value5", DateTime.Now.AddMilliseconds (5000));
3 Console.WriteLine (client. Get ("Key5"));
4 System.Threading.Thread.Sleep (6000);
5 Console.WriteLine ("Expired:" + client.) Get ("Key5"));

Most of the time we don't want the data to be cached permanently, usually with an expiration time. The above added cache data set the cache time, can achieve the effect of expired cache.

Cases Click to download

This article ends here, the content is relatively simple, and many people have written something. However, for personal learning or accumulation, for the cache part of the subsequent articles continue to update.

Transferred from: http://www.cnblogs.com/qingyuan/archive/2011/01/17/1937855.html

Introduction to cache app--memcached distributed cache

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.