Introduction to the distributed cache system Memcached and its practices in. net)

Source: Internet
Author: User

Http://hi.baidu.com/wuxincai/blog/item/a2c9e9f2450fe714b07ec5d5.html (RPM)

Origin: in data-driven web development, the same data is often retrieved from the database repeatedly, which greatly increases the database load. Caching is a good solution to this problem. However, although some pages can be cached locally in ASP. NET, It is not flexible enough. In this case, Memcached may be what you want.

What is Memcached?
Memcached is a high-performance, distributed memory object cache system developed by Danga Interactive. It is used to reduce database load and improve access speed in dynamic applications.

What can Memcached cache?
By maintaining a unified and huge hash table in the memory, Memcached can be used to store data in various formats, including images, videos, files, and database retrieval results.

Is Memcached fast?

Very fast. Memcached uses libevent (if possible, epoll is used in linux) to balance any number of open links and use non-blocking network I/O, implement reference counting on internal objects (therefore, objects can be in a variety of States for various clients), and use your own page block distributor and hash table, therefore, the virtual memory will not produce fragments, and the time complexity of virtual memory allocation can be ensured to be O (1 )..

Danga Interactive developed Memcached to speed up Danga Interactive. LiveJournal.com provides up to 1 million page visits to 20 million users every day. These are completed by a cluster composed of web servers and database servers. Memcached almost completely abandons the way in which any data is read from the database. At the same time, it also shortens the page viewing speed and improves the resource allocation method, and the access speed to the database when Memcache fails.

Memcached features
Memcached caches are distributed and can be accessed by multiple users on different hosts at the same time. Therefore, the shared memory can only be used for single-host applications, the disk overhead and blocking will not occur when you use a database to do similar things.

Use of Memcached
I
Install the Memcached server (install it as a system service)
Download file: memcached 1.2.1 for Win32 binaries (Dec 23,200 6)
1. decompress the file to c: \ memcached.
2. Enter 'C: \ memcached \ memcached.exe-d install' in the command line'
3. Enter 'C: \ memcached \ memcached.exe-d start' in the command line to start Memcached. The default listening port is 11211.
You can use memcached.exe-h to view its help information.
Ii. NET memcached client library
Download file: https://sourceforge.net/projects/memcacheddotnet/

There are two versions:. net1.1 and. net2.0. There is also a good example.

Three applications

1. Place Commons. dll, ICSharpCode. SharpZipLib. dll, log4net. dll, and Memcached. ClientLibrary. dll in the bin directory.
2 reference Memcached. ClientLibrary. dll
3 code

 

Note: It is a good thing and it is very convenient to use. php and ruby projects use a lot of this, but there are only a few in. net projects ). I hope you will have more exchanges.

 

1 namespace Memcached. MemcachedBench
2 {
3 using System;
4 using System. Collections;
5
6 using Memcached. ClientLibrary;
7
8 public class MemcachedBench
9 {
10 [STAThread]
11 public static void Main (String [] args)
12 {
13 string [] serverlist = {"10.0.0.131: 11211", "10.0.0.132: 11211 "};
14
15 // initialize the pool
16 SockIOPool pool = SockIOPool. GetInstance ();
17 pool. SetServers (serverlist );
18
19 pool. InitConnections = 3;
20 pool. MinConnections = 3;
21 pool. MaxConnections = 5;
22
23 pool. SocketConnectTimeout = 1000;
24 pool. SocketTimeout = 3000;
25
26 pool. MaintenanceSleep = 30;
27 pool. Failover = true;
28
29 pool. Nagle = false;
30 pool. Initialize ();
31
32 // obtain the client instance
33 MemcachedClient mc = new MemcachedClient ();
34 mc. EnableCompression = false;
35
36 Console. WriteLine ("------------ test -----------");
37 mc. Set ("test", "my value"); // store the data to the cache server. Here, the string "my value" is cached, and the key is "test"
38
39 if (mc. KeyExists ("test") // test that the cache has a project whose key is test.
40 {
41 Console. WriteLine ("test is Exists ");
42 Console. WriteLine (mc. Get ("test"). ToString (); // Get the project whose key is test in the cache
43}
44 else
45 {
46 Console. WriteLine ("test not Exists ");
47}
48
49 Console. ReadLine ();
50
51 mc. Delete ("test"); // remove the project whose key is test in the cache
52
53 if (mc. KeyExists ("test "))
54 {
55 Console. WriteLine ("test is Exists ");
56 Console. WriteLine (mc. Get ("test"). ToString ());
57}
58 else
59 {
60 Console. WriteLine ("test not Exists ");
61}
62 Console. ReadLine ();
63
64 SockIOPool. GetInstance (). Shutdown (); // close the pool and close sockets
65}
66}
67}

4. Running result

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.