Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Memcached, a high-performance cache system

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article is about how to use Memcached, a high-performance cache system. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Entity types cannot be cached in Memcached without serialization in Memcached, so entity classes need to be processed in order to cache them.

Memcached is a high-performance distributed in-memory object caching system, which is used for dynamic Web applications to reduce database load. It reduces the number of times to read the database by caching data and objects in memory, thus improving the speed of dynamic, database-driven websites. 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 cache string types and other types that have been serialized internally, but for our custom types, we can't cache them in Memcached, because Memcached can only cache the data after serialization, so here we serialize the custom entity types and then store them in Memcached.

First download memcached under the windows platform, and then install it. After installation, you can start the memcached service. You can enter it with the dos command under cmd, or you can start it in computer Management-> Services-> memcached- >. To start the service.

Then the relevant dll is introduced into the project:

Commons.dll,ICSharpCode.SharpZipLib.dll,log4net.dll,Memcached.ClientLibrary.dll

Introduce Memcached.ClientLibrary.dll into the reference of the project

Then it's time to write a program, and create a MVC program here:

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 it is not marked as serializable, the subsequent running program will report an error.

Then create a MemcachedHelper class to assist programming.

Public class MemcachedHelper {public static MemcachedClient mclient;static MemcachedHelper () {string [] serverlist = new string [] {"127.0.0.1 serverlist 11211"}; SockIOPool pool = SockIOPool.GetInstance ("First"); pool.SetServers (serverlist); pool.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) }}

Finally, there is the specific implementation of Controller:

Public class EntityMemcachedController: Controller {/ GET: / EntityMemcached/ serialize entity classes into byte arrays and store them in Memcached to cache data, thus reducing access pressure. / public ActionResult Index () {var vipInfo = new List {UserName= "Zhang San", Vip=1, QQ= "3123456", Mail= "3123456", VipEndDate= (DateTime?) DateTime.Now.AddDays (1)}, new VIP {UserName= "Li Si", Vip=1, QQ= "4123456", Mail= "4123456", VipEndDate= (DateTime?) DateTime.Now.AddDays (2)} New VIP {UserName= "Wang Wu", Vip=1, QQ= "5123456", Mail= "5123456", VipEndDate= (DateTime?) DateTime.Now.AddDays (3)}, new VIP {UserName= "Zhao Liu", Vip=1, QQ= "6123456", Mail= "6123456", VipEndDate= (DateTime?) DateTime.Now.AddDays (4)}, new VIP {UserName= "Liu Qi", Vip=1, QQ= "7123456", Mail= "7123456" VipEndDate= (DateTime?) 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.AddMinutes (1)); / / set cache expiration time return Content ("Memcached distributed cache set successfully!") ;} else {string key = Request.Cookies ["_ EntityMemcached"] .Value.ToString (); object obj = MemcachedHelper.Get (key); List info = obj as List; if (info! = null) {return View (info) }} return Content ("'bug'" if displayed);}

Look at the implementation:

Then back out and click "implement memcached caching" again

I set the cache for less than a minute, so it will always be this interface for a minute, and I have to say that memcached is still good!

Thank you for reading! This is the end of the article on "how to use the high-performance cache system Memcached". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report