


Example introduction of high-performance caching system (Memcached)
Entity types in Memcached cannot be cached in Memcached without being serialized, so the entity classes need to be processed before they can be cached.
Memcached is a high-performance distributed In-memory object caching system for dynamic web applications to reduce database load. It improves the speed of dynamic, database-driven websites by caching data and objects in memory to reduce the number of database reads. Memcached is based on a hashmap that stores key/value pairs. Its daemon is written in C, but the client can be written in any language and communicates with the daemon through the memcached protocol.
We can use Memcached to cache string types and other types that have been serialized internally, but for our custom types, we cannot cache them in Memcached because Memcached can only cache serialized data. , therefore, here we serialize the custom entity type and store it in Memcached.
First download memcached under the windows platform, and then install it. After installation, start the memcached service. You can enter the dos command under cmd, or you can start the service in Computer Management->Service->memcached->Start.
Then you can start the service in Introduce relevant dlls into the project:
Commons.dll, ICSharpCode.SharpZipLib.dll, log4net.dll, Memcached.ClientLibrary.dll
Introduce Memcached.ClientLibrary.dll into the project reference
Then follow After writing the program, create an 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, subsequent running of the 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: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, the specific implementation in the Controller:
public class EntityMemcachedController : Controller { // // GET: /EntityMemcached/ /// <summary> /// 序列化实体类为字节数组,将其存储到Memcached中,以缓存数据,从而减轻访问压力.... /// </summary> /// <returns></returns> public ActionResult Index() { var vipInfo = new List<VIP>{ new VIP{ UserName="张三", Vip=1, QQ="3123456", Mail="3123456", VipEndDate=(DateTime?)DateTime.Now.AddDays(1) }, new VIP{ UserName="李四", Vip=1, QQ="4123456", Mail="4123456", VipEndDate=(DateTime?)DateTime.Now.AddDays(2) }, new VIP{ UserName="王五", Vip=1, QQ="5123456", Mail="5123456", VipEndDate=(DateTime?)DateTime.Now.AddDays(3) }, new VIP{ UserName="赵六", Vip=1, QQ="6123456", Mail="6123456", VipEndDate=(DateTime?)DateTime.Now.AddDays(4) }, new VIP{ UserName="刘七", 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);//设置cookie过期时间 MemcachedHelper.set(sessionId, vipInfo, DateTime.Now.AddMinutes(1));//设置缓存过期时间 return Content("Memcached分布式缓存设置成功!!!"); } 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); } } return Content("若显示则有'bug'"); }
See See the implementation:
Then exit and click "Implement memcached cache" again
I set the cache within one minute, so it will always be this interface within this minute. I have to say that memcached is still good! Next, we will study the caching strategy of OutputCached + Monogodb
The above is the detailed content of Example introduction of high-performance caching system (Memcached). For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











General Matrix Multiplication (GEMM) is a vital part of many applications and algorithms, and is also one of the important indicators for evaluating computer hardware performance. In-depth research and optimization of the implementation of GEMM can help us better understand high-performance computing and the relationship between software and hardware systems. In computer science, effective optimization of GEMM can increase computing speed and save resources, which is crucial to improving the overall performance of a computer system. An in-depth understanding of the working principle and optimization method of GEMM will help us better utilize the potential of modern computing hardware and provide more efficient solutions for various complex computing tasks. By optimizing the performance of GEMM

On July 29, at the roll-off ceremony of AITO Wenjie's 400,000th new car, Yu Chengdong, Huawei's Managing Director, Chairman of Terminal BG, and Chairman of Smart Car Solutions BU, attended and delivered a speech and announced that Wenjie series models will be launched this year In August, Huawei Qiankun ADS 3.0 version was launched, and it is planned to successively push upgrades from August to September. The Xiangjie S9, which will be released on August 6, will debut Huawei’s ADS3.0 intelligent driving system. With the assistance of lidar, Huawei Qiankun ADS3.0 version will greatly improve its intelligent driving capabilities, have end-to-end integrated capabilities, and adopt a new end-to-end architecture of GOD (general obstacle identification)/PDP (predictive decision-making and control) , providing the NCA function of smart driving from parking space to parking space, and upgrading CAS3.0

DNS (DomainNameSystem) is a system used on the Internet to convert domain names into corresponding IP addresses. In Linux systems, DNS caching is a mechanism that stores the mapping relationship between domain names and IP addresses locally, which can increase the speed of domain name resolution and reduce the burden on the DNS server. DNS caching allows the system to quickly retrieve the IP address when subsequently accessing the same domain name without having to issue a query request to the DNS server each time, thereby improving network performance and efficiency. This article will discuss with you how to view and refresh the DNS cache on Linux, as well as related details and sample code. Importance of DNS Caching In Linux systems, DNS caching plays a key role. its existence

On April 11, Huawei officially announced the HarmonyOS 4.2 100-machine upgrade plan for the first time. This time, more than 180 devices will participate in the upgrade, covering mobile phones, tablets, watches, headphones, smart screens and other devices. In the past month, with the steady progress of the HarmonyOS4.2 100-machine upgrade plan, many popular models including Huawei Pocket2, Huawei MateX5 series, nova12 series, Huawei Pura series, etc. have also started to upgrade and adapt, which means that there will be More Huawei model users can enjoy the common and often new experience brought by HarmonyOS. Judging from user feedback, the experience of Huawei Mate60 series models has improved in all aspects after upgrading HarmonyOS4.2. Especially Huawei M

The best version of the Apple 16 system is iOS16.1.4. The best version of the iOS16 system may vary from person to person. The additions and improvements in daily use experience have also been praised by many users. Which version of the Apple 16 system is the best? Answer: iOS16.1.4 The best version of the iOS 16 system may vary from person to person. According to public information, iOS16, launched in 2022, is considered a very stable and performant version, and users are quite satisfied with its overall experience. In addition, the addition of new features and improvements in daily use experience in iOS16 have also been well received by many users. Especially in terms of updated battery life, signal performance and heating control, user feedback has been relatively positive. However, considering iPhone14

Linux and Windows are two common operating systems, representing the open source Linux system and the commercial Windows system respectively. In both operating systems, there is a command line interface for users to interact with the operating system. In Linux systems, users use the Shell command line, while in Windows systems, users use the cmd command line. The Shell command line in Linux system is a very powerful tool that can complete almost all system management tasks.

Detailed explanation of the method of modifying the system date in the Oracle database. In the Oracle database, the method of modifying the system date mainly involves modifying the NLS_DATE_FORMAT parameter and using the SYSDATE function. This article will introduce these two methods and their specific code examples in detail to help readers better understand and master the operation of modifying the system date in the Oracle database. 1. Modify NLS_DATE_FORMAT parameter method NLS_DATE_FORMAT is Oracle data

There is a close interaction between the CPU (central processing unit), memory (random access memory), and cache, which together form a critical component of a computer system. The coordination between them ensures the normal operation and efficient performance of the computer. As the brain of the computer, the CPU is responsible for executing various instructions and data processing; the memory is used to temporarily store data and programs, providing fast read and write access speeds; and the cache plays a buffering role, speeding up data access speed and improving The computer's CPU is the core component of the computer and is responsible for executing various instructions, arithmetic operations, and logical operations. It is called the "brain" of the computer and plays an important role in processing data and performing tasks. Memory is an important storage device in a computer.
