Introduction and practice of distributed cache system memcached

Source: Internet
Author: User
Tags memcached readline

Origin: In data-driven web development, it is often repeated to remove the same data from the database, which greatly increases the database load. Caching is a good way to solve this problem. However, although the local cache of the page can already be implemented in ASP, it is still not flexible enough. At this point, memcached may be what you want.

What is memcached?
Memcached is a high-performance, distributed memory object caching system developed by Danga Interactive for reducing database load and increasing access speed in dynamic applications.

What can memcached cache?
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.

memcached, are you quick?

Very fast. Memcached uses libevent (using Epoll under Linux if possible) to equalize any number of open links, use non-blocking network I/O, and implement reference counting for internal objects (so objects can be in various states for multiple clients). Use your own page block allocator and hash table, so virtual memory is not fragmented and the time complexity of virtual memory allocation is guaranteed to be O (1).

Danga Interactive developed memcached for the speed of Danga Interactive. Currently, LiveJournal.com has provided up to 20 million page visits per day to 1 million of users. These, however, are done by a cluster of Web servers and database servers. Memcached almost completely abandons the way any data is read from the database, and it also shortens the speed at which the user can view the page, better resource allocation, and access to the database when the memcache fails.

Features of Memcached
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.

Use of memcached
A
Memcached Server-side installation (it is installed as a system service here)
Download file: memcached 1.2.1 for Win32 binaries (Dec 23, 2006)
1 Extracting files to c:\memcached
2 command line input ' c:\memcached\memcached.exe-d install '
3 command line input ' c:\memcached\memcached.exe-d start ', the command starts memcached, the default listener port is 11211
You can view its help through memcached.exe-h
Two. NET memcached client Library
Download file: https://sourceforge.net/projects/memcacheddotnet/

There is a good example of two versions of. net1.1 and. net2.0.

Three applications

1 Put the Commons.dll,icsharpcode.sharpziplib.dll,log4net.dll,memcached.clientlibrary.dll in the bin directory
2 References Memcached.ClientLibrary.dll
3 Code

1 namespace Memcached.memcachedbench
2 {
3 using System;
4 using System.Collections;
5
6 using Memcached.clientlibrary;
7
8 public class Memcachedbench
9 {
Ten [STAThread]
public static void Main (string[] args)
12 {
String[] ServerList = {"10.0.0.131:11211", "10.0.0.132:11211"};
14
15//Initialize Pool
Sockiopool pool = sockiopool.getinstance ();
Pool. Setservers (serverlist);
18
Pool. Initconnections = 3;
Pool. Minconnections = 3;
Pool. MaxConnections = 5;
22
The pool. socketconnecttimeout = 1000;
Pool. Sockettimeout = 3000;
25
The pool. Maintenancesleep = 30;
The pool. Failover = true;
28
The pool. Nagle = false;
The pool. Initialize ();
31
32//Get Client instance
memcachedclient mc = new Memcachedclient ();
MC. EnableCompression = false;
35
Console.WriteLine ("------------Test-----------");
Panax Notoginseng MC.  Set ("Test", "my Value"); Stores the data to the cache server, where the string "My value" is cached, and key is "test"
38
if (MC. Keyexists ("test"))//test cache exists with key test item
40 {
Console.WriteLine ("Test is Exists");
Console.WriteLine (MC. Get ("Test").  ToString ()); Get the item with key test in the cache
43}
The Else
45 {
Console.WriteLine ("Test Not Exists");
47}
48
Console.ReadLine ();
50
The MC.  Delete ("test"); Remove the item in the cache with key test
52
if (MC. Keyexists ("Test"))
54 {
Console.WriteLine ("Test is Exists");
Console.WriteLine (MC. Get ("Test"). ToString ());
57}
+ Else
59 {
Console.WriteLine ("Test Not Exists");
61}
Console.ReadLine ();
63
Sockiopool.getinstance ().  Shutdown (); Close the pool, close sockets
65}
66}
67}

4 Running Results

Introduction and practice of distributed cache system memcached

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.