本文主要是介绍Memcached,它的本质是在内存中进行缓存。
原文地址:MEMCACHED FOR C# - A WALKTHROUGH
一、在谈Memcahced之前,先来谈谈Object caching in .NET 4的缺点
Caching is the process of storing frequently used data on the server to fulfil subsequent requests. Object Caching in .NET 4 is a great tool to use for caching and literally takes minutes to get up and running.
缓存的目的,是将一些数据存储于服务器的内存中,而不必每次都向数据库中查询数据,从而提高服务器的响应速度。Object Caching in .Net就是对缓存的实现,但是Object Caching也有缺点:当应用程序池进行回收的时候会丢失数据,同时也不支持“负载均衡”。
However, one of the downsides of using the System.Runtime.Caching in .NET 4 is that every time your application pool recycles in IIS, you lose the objects in cache. The same happens if you make a change to your web.config or redeploy your application.
This isn't a problem if you are running smaller applications and cached data isn't vital to their performance. However, for high-traffic applications that need to have the cache up all the time, this becomes a problem.
Another thing to consider is the possibility of load-balanced servers - using the Object Caching in .NET 4 isn't distributed. That means it will only store its cache per application pool. If you have two servers it means that you will have two different cache stores.
二、针对Object Caching的缺点,再来谈谈Memcached
Memcached is a free & open source, high-performance, distributed memory object caching system intended for use in speeding up dynamic web applications by alleviating database load. You can think of it as a short-term memory for your applications. The brilliant thing aboutMemcached is that you run it on one server and point all the applications to the Memcached server. This way all servers use the same cache!
Another bonus is that even if you redeploy your app, or recycle the app pool in any way - the cached objects are still there. This is great news for larger applications that need to be online constantly and can't afford any downtime with regards to it's cache.
Memcached是一个开源的、免费的memory object caching system,它有两个特点:high-performance和distributed。它的high-performance特点,是通过将数据存储于内存当中,从而从而alleviating database load,由于“从内存读取数据” 比 “从数据库读取数据”要快,所以能够提高速度。
三、Memcached的两部分:服务器端和客户端API
The advantages of using Memcached are endless and getting it setup on your machine is really simple. There are two parts to getting Memcached setup - you need the Memcached server and then you need to get the client API. There are few client API available for most of the common languages out there (Ruby, C#, PHP, C, Java).
要想安装Memcached,需要两部分:Memcached Server和Memcached Client(API)
四、Memcached的原理(对于这一部分,不太了解,不过先粘贴过来)
(1)Socket 服务器端 【这里有篇文章:简单理解Socket http://www.cnblogs.com/dolphinX/p/3460545.html】
(2)数据:键值对存储 【key item】
(3)内存处理的算法:
本质就是一个大的哈希表。key最大长度是255个字符。
内存模型:Memcache预先将可支配的内存空间进行分区(Slab),每个分区里再分成多个块(Chunk)最大1MB,但同一个分区里:块的长度(bytes)是固定的。
插入数据:查找适合自己长度的块,然后插入,会有内存浪费。
LRU,闲置>过期 >最少访问 (LRU是Least Recently Used 近期最少使用算法)
惰性删除:它并没有提供监控数据过期的机制,而是惰性的,当查询到某个key数据时,如果过期那么直接抛弃。
(4)集群搭建原理:
Memcache服务器端并没有提供集群功能,但是通过客户端的驱动程序实现了集群配置。
客户端实现集群的原理:首先客户端配置多台集群机器的ip和端口的列表。然后客户端驱动程序在写入之前,首先对key做哈希处理得到哈希值后对总的机器的个数进行取余然后就选择余数对应的机器。
这部分内容来自http://www.cnblogs.com/peter-peng/p/3187826.html
对于“集群搭建原理”参考下面的说法
Memcached is a distributed memory object cache, so how does it actually distribute?
Effectively the Memcached server is simply a key value store so it is up to the client to pick the correct server to put the object on. The way it picks is to hash the key for the item you want to store and compute from that which server that item should be placed on. If you had two servers the split of items between the two should be approximately 50-50.
一些参考链接:
Memcached Clients- Which ones best https://sysdot.wordpress.com/2011/03/08/memcached-clients-which-ones-best/
下一篇文章,就要讲如何安装Memcached Server如何安装