Introduction and practice of distributed cache system memcached

Source: Internet
Author: User
Tags 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
First,
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. Unzip the file 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
Ii.. 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.

Third, the application

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

namespaceMemcached.memcachedbench {usingSystem; usingSystem.Collections; usingmemcached.clientlibrary;  Public classmemcachedbench {[STAThread] Public Static voidMain (string[] args) {string[] ServerList = {"10.0.0.131:11211","10.0.0.132:11211" }; //Initialize PoolSockiopool pool =sockiopool.getinstance (); Pool.              Setservers (serverlist); Pool. Initconnections=3; Pool. Minconnections=3; Pool. MaxConnections=5; Pool. Socketconnecttimeout= +; Pool. Sockettimeout= the; Pool. Maintenancesleep= -; Pool. Failover=true; Pool. Nagle=false; Pool.              Initialize (); //get the client instancememcachedclient MC =Newmemcachedclient (); Mc. EnableCompression=false; Console.WriteLine ("------------Test-----------"); Mc. Set ("Test","My value");//stores the data to the cache server, where the string "My value" is cached, and key is "test"              if(MC. Keyexists ("Test"))//test cache has a project with key test{Console.WriteLine ("test is Exists"); Console.WriteLine (MC. Get ("Test"). ToString ());//get the item with key test in the cache             }             Else{Console.WriteLine ("Test not Exists");              } console.readline (); Mc. Delete ("Test");//Remove the item in the cache with key test              if(MC. Keyexists ("Test") {Console.WriteLine ("test is Exists"); Console.WriteLine (MC. Get ("Test").             ToString ()); }             Else{Console.WriteLine ("Test not Exists");                          } console.readline (); Sockiopool.getinstance ().  Shutdown (); //Close the pool, close sockets         }     } }

Iv. Results of operation

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.