设为首页 收藏本站
查看: 1127|回复: 0

[经验分享] 优秀的缓存工具Memcached

[复制链接]

尚未签到

发表于 2015-8-31 13:38:37 | 显示全部楼层 |阅读模式
Memcached介绍
基于对ERP项目的思考,我看看了关于缓存方面的东西。在所有的缓存方案中,Memcached是最让我有兴趣的。看看Memcached项目
的有关背景:
memcached is a high-performance, distributed memory object caching system, generic in nature, but intended
for use in speeding up dynamic web applications by alleviating database load.
Danga Interactive developed memcached to enhance the speed of LiveJournal.com,
a site which was already doing 20 million+ dynamic page views per day for 1 million users with a
bunch of webservers and a bunch of database servers. memcached dropped the database load to almost nothing,
yielding faster page load times for users, better resource utilization, and faster access to the databases
on a memcache miss.

不翻译了,只强调几个keywords:
high-performance,distributed memory
LiveJournal.com----------因为这个百万级的网站的需求而产生的。

1.high-performance
在官方wiki上有这么一句:
Very fast. It uses libevent to scale to any number of open connections (using epoll on Linux,
if available at runtime), uses non-blocking network I/O, refcounts internal objects (so objects
can be in multiple states to multiple clients), and uses its own slab allocator and hash table so
virtual memory never gets externally fragmented and allocations are guaranteed O(1).
使用libevent,它可以应对任意多个连接,使用非阻塞的网络IO.
由于它的工作机制是在内存中开辟一块空间,然后建立一个HashTable,Memcached自管理这些HashTable。
由于是对HashTable的遍历,所以其查询复杂度是O(1)。
至于其中提到的libevent,呵呵,还没有看:)

2.distributed memory:分布式的内存管理,这正式它的优势所在。Memcached Client API 屏蔽了对象在不同内存区域的差别,
以一致的接口方法,所以用起来你完全感觉不到在使用不同的内存对象,当然缓存的时候也感觉不到。
这也是系统水平扩展的一个要点。理论上内存可以无限扩展,只要平行增加server节点就可以。

3.和数据库缓存的比较:
  不考虑使用什么样的数据库(MS-SQL, Oracle, Postgres, MysQL-InnoDB, etc..),
  实现事务(ACID,Atomicity, Consistency, Isolation, and Durability )需要大量开销,
  特别当使用到硬盘的时候,意味着查询可能会阻塞。当使用不包含事务的数据库(例如Mysql-MyISAM),上面的开销不存在,但读线程又可能会被写线程阻塞。
  可以看出,用数据库做缓存,在很大程度上是不明智的。
4. 和共享内存的比较:
   最初的缓存做法是在线程内对对象进行缓存,但这样进程间就无法共享缓存,命中率非常低,导致缓存效率极低。
   后来出现了共享内存的缓存,多个进程或者线程共享同一块缓存,但毕竟还是只能局限在一台机器上,多台机器做相
   同的缓存同样是一种资源的浪费,而且命中率也比较低。   Memcached Server和Clients共同工作,实现跨服务器
   分布式的全局的缓存。并且可以与Web Server共同工作,Web Server对CPU要求高,对内存要求低,
   Memcached Server对CPU要求低,对内存要求高,所以可以搭配使用。

5. 缓存对象同步管理:
   Memcached是遵循LRU(Least Recently Used:最近最少被使用)原则。当然你也可以设置缓存对象的生命周期。

6.Memcached的使用(Win32,.Net)
  具体下载安装过程:http://jehiah.cz/projects/memcached-win32/
  在win32下,memcached就是一个windows service。你可以在windows服务中找到:memcached Server 。这个就是了。
  发现这玩意比较消耗内存。
   
  .Net中使用:
  引用Memcached.ClientLibrary.dll,主要使用其中的几个类:

   Memcached.ClientLibrary.SockIOPool pool = Memcached.ClientLibrary.SockIOPool.GetInstance(); ---服务器管理对象
   MemcachedClient mc = new MemcachedClient(); ---客户端

比较感兴趣这个GetInstance方法,具体看看:

1            // empty constructor
2         protected SockIOPool() { }
3
4         /// <summary>
5         /// Factory to create/retrieve new pools given a unique poolName.
6         /// </summary>
7         /// <param name="poolName">unique name of the pool</param>
8         /// <returns>instance of SockIOPool</returns>
9         [MethodImpl(MethodImplOptions.Synchronized)]
10         public static SockIOPool GetInstance(String poolName)
11         {
12             if(Pools.ContainsKey(poolName))
13                 return (SockIOPool)Pools[poolName];
14
15             SockIOPool pool = new SockIOPool();
16             Pools[poolName] = pool;
17
18             return pool;
19         }
20
21         /// <summary>
22         /// Single argument version of factory used for back compat.
23         /// Simply creates a pool named "default".
24         /// </summary>
25         /// <returns>instance of SockIOPool</returns>
26         [MethodImpl(MethodImplOptions.Synchronized)]
27         public static SockIOPool GetInstance()
28         {
29             return GetInstance(GetLocalizedString("default instance"));
30         }          string[] serverlist = { "192.168.100.142:11211" };
        pool.SetServers(serverlist);
        pool.InitConnections = 3;
        pool.MinConnections = 3;
        pool.MaxConnections = 5;
上面的代码,可以看到server的配置还是很强的。至于利用client api开发则是相当简单。自带的MemCachedBench_2.0项目就是相当好的一个demo。

有空翻译下:http://www.linuxjournal.com/article/7451

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-106849-1-1.html 上篇帖子: memcached学习笔记——存储命令源码分析下篇 下篇帖子: 【转】 Key/Value之王Memcached初探:三、Memcached解决Session的分布式存储场景的应用
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表