Examples of high performance cache systems (Memcached)

Source: Internet
Author: User
Tags set cookie
in memcached, entity types are not serializable in memcached, so the entity classes need to be processed before they can be cached.

Memcached is a high-performance distributed memory object caching system for dynamic Web applications to mitigate database load. It improves the speed of dynamic, database-driven Web sites by caching data and objects in memory to reduce the number of times a database is read. Memcached is based on a hashmap that stores key/value pairs. Its daemon (daemon) is written in C, but the client can write in any language and communicate with the daemon through the memcached protocol.

We can use memcached to cache types such as String types that have internally implemented serialization, but for our custom types we cannot cache them in memcached because memcached can only cache data after serialization, so Here we will serialize the custom entity type and store it in memcached.

First download the memcached under Windows platform and install it. After the installation is started memcached service, you can use the DOS command under CMD input, you can also be in the Computer Management, service->memcached-> start. To start the service.

This is followed by the introduction of the associated DLL in the project:
Commons.dll,icsharpcode.sharpziplib.dll,log4net.dll,memcached.clientlibrary.dll
Introduction of Memcached.ClientLibrary.dll in project references

Then you write the program, where you create an MVC program:
Create a class in the Models folder:

[Serializable]public class Vip{public string UserName {get; set;} public int? Vip {get; set;} Public DateTime? vipenddate {get; set;} public string Mail {get; set;} public string QQ {get; set;}}

If the label is not serializable, then the subsequent run program will error.

A Memcachedhelper class is then created to aid programming.

public class Memcachedhelper{public static memcachedclient mclient;static memcachedhelper () {string[] serverlist = new String[] {"127.0.0.1:11211"}; Sockiopool pool = sockiopool.getinstance ("First");p ool. Setservers (serverlist);p ool. Initialize (); mclient = new Memcachedclient (); mclient. poolname = "First"; mclient. EnableCompression = false;} public static bool Set (string key, object value, DateTime expiry) {return mclient. Set (key, value, expiry);} public static object Get (string key) {return mclient. Get (key);}}

The final is the controller inside the specific implementation:

public class Entitymemcachedcontroller:controller {////GET:/entitymemcached///<summary >///The serialized entity class is a byte array that is stored in memcached to cache data, thereby easing access pressure ....///</summary>//<returns></ Returns> public ActionResult Index () {var vipinfo = new list<vip>{new V ip{username= "Zhang San", Vip=1, qq= "3123456", mail= "3123456", vipenddate= (DateTime?) DateTime.Now.AddDays (1)}, new vip{username= "John Doe", Vip=1, qq= "4123456", mail= "4123456", vipenddate= (Datetim E?) DateTime.Now.AddDays (2)}, new vip{username= "Harry", Vip=1, qq= "5123456", mail= "5123456", vipenddate= (Datetim E?) DateTime.Now.AddDays (3)}, new vip{username= "Zhao Liu", Vip=1, qq= "6123456", mail= "6123456", vipenddate= (Datetim E?) DateTime.Now.AddDays (4)}, new vip{username= "Liu Qi", Vip=1, qq= "7123456", mail= "7123456", vipenddate= (Datetim E?)                 DateTime.Now.AddDays (5)}};       if (request.cookies["_entitymemcached"] = = null) {string sessionId = Guid.NewGuid ().                ToString (); response.cookies["_entitymemcached"].                Value = sessionId; response.cookies["_entitymemcached"]. Expires = DateTime.Now.AddMinutes (1);//Set Cookie Expiration Time Memcachedhelper.set (SessionId, Vipinfo, DateTime.Now.Ad Dminutes (1));//Set cache expiration Time return Content ("memcached distributed cache Settings succeeded!!!            "); } else {string key = request.cookies["_entitymemcached"].                Value.tostring ();                Object obj = Memcachedhelper.get (key);                list<vip> info = obj as list<vip>;                if (info! = null) {return View (info);        }} and return Content ("bug" if shown); }

See the implementation:

Then exit and re-click "Implement Memcached Cache"

I set the cache within a minute, so within this minute will always be this interface, have to say memcached still good! Next, we study the outputcached + MONOGODB cache strategy.

Related Article

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.