缓存应用--Memcached分布式缓存简介(二)

1 命令行查看状态

  很多时候我们需要去查看Memcached 的使用状态,比如Memcached 的运行时间,使用状态等等。在Windows系统中我们可以使用telnet 命令来查看Memcached 的相关运行情况。

  开始—>运行àcmd 运行得到如下:

 

输入telnet命令: telnet 服务地址 端口 

 

Memcached 的默认端口号是11211
      输入stats 命令: 在这里屏幕是空白,看不到输入的内容,回车之后可以看到Memcached的运行相关信息。 

 

Pid: Memcached 服务器中的进程编号

   Uptime:Memcached服务器启动之后所经历的时间,单位秒

   Time: 当前系统时间,单位秒

  Version: Memcached 的版本号

  pointer_size:服务器所在主机操作系统的指针大小,一般为32或64

  curr_items:表示当前缓存中存放的所有缓存对象的数量

  total_items:表示从memcached服务启动到当前时间,系统存储过的所有对象的数量,包括已经删除的对象

  bytes:表示系统存储缓存对象所使用的存储空间,单位为字节

  curr_connections:表示当前系统打开的连接数

  total_connections:表示从memcached服务启动到当前时间,系统打开过的连接的总数

  cmd_get: 查询缓存的次数,即使不成功也算一次

  cmd_set: 保存数据的次数,当然这里只保存成功的次数

  get_hits:表示获取数据成功的次数。

  get_misses:表示获取数据失败的次数。

  evictions:为了给新的数据项目释放空间,从缓存移除的缓存对象的数目。比如超过缓存大小时根据LRU算法移除的对象,以及过期的对象

  bytes_read:memcached服务器从网络读取的总的字节数

  bytes_written:memcached服务器发送到网络的总的字节数

  limit_maxbytes:memcached服务缓存允许使用的最大字节数

  threads:被请求的工作线程的总数量

缓存命中率 = get_hits / cmd_get * 100% ; 

2 Memcached 存储机制

       关于Memcached的存储机制,在网上搜了一下讲解基本上都是千篇一律的。

memcached默认情况下采用了名为Slab Allocator的机制分配、管理内存。在之前的版本中,Memcached存储会导致很多内存碎片,从而加重了操作系统对内存管理的负担。Slab Allocator的基本原理是按照预先规定的大小,将分配的内存分割成特定长度的块, 以完全解决内存碎片问题。

借用一张图说明一下: 

     

Slab Allocation 将分配的内存分割成各种尺寸的chunk (块),并把大小相同尺寸的chunk分为一组,就如上图一样:分割了 88b,112b,144b等尺寸。其实Slab Allocation还有重复利用内存的功能,也就是说分配的内存不会释放,而是重复利用。

当存储数据的时候,它会自动去查找最为匹配的chunk,然后将数据存储到其中。比如我存储数据的大小为110B,那么它会存储到112B的chunk中。

     上面的问题来了,我存储只需要110B,但是我存储到112B的chunk中。如下图(借用): 

 

    那么在110b的存储中会浪费2B的内存空间
至于如何完全解决这个内存空间浪费的问题,还没有很好的方案,不过Memcached的 增长因子(Growth Factor)能够适当解决此问题。目前Memcached的默认增长因子      是1.25,也就是说会以原有的最大值基础上乘以1.25 来分配空间。 

3 Memcached 对内存资源的有效利用

之前已经提到过了,Memcached 会重复利用已经分配的内存,也就是说不会去删除已有的数据而且释放内存空间,而是数据过期之后,用户将数据不可见。

         Memcached 还是用了一种Lazy Expiration (延迟过期[姑且这样翻译]) 技术,就是Memcached不会去监视服务器上的数据是否过期,而是等待get的时候检查时间戳是否过期,减少Memcached在监控数据上所用到的时间。

Memcached 不会去释放已经使用的内存空间,但是如果分配的内存空间已经满了,而Memcached 是如何去保证内存空间的重复使用呢!Memcached 是用了 Least Recently Used(LRU) 机制来协调内存空间的使用。LRU 意思就是最少最近使用,当此处内存空间数据最长时间没有使用,而且使用次数很少,在存储新的数据的同时就会覆盖此处空间。 

4 Memcached 客户端使用简单封装

本文很多都是理论上的分析,这些大多也是从网络上看来的,根据自己的理解和实际的应用做了一些总结。有时候我们使用Memcached的客户端时并不是那么的友好,这里对其做了一下简单的封装,抽象出来了一个接口:

1 usingSystem;
2 usingSystem.Collections.Generic;
3 usingSystem.Linq;
4 usingSystem.Text;
5 usingSystem.Collections;

7 namespaceMemcachedTest
8 {
9 publicinterfaceICache
10 {
11 boolContainKey(stringargKey);
12 
13 boolAdd(stringargKey,objectargValue);
14 
15 boolAdd(stringargKey, objectargValue, DateTime argDateExpiration);
16 
17 boolAdd<T>(stringargKey, T entity) whereT : class;
18 
19 boolAdd<T>(stringargKey, T entity, DateTime argDateExpiration) whereT : class;
20 
21 boolSet(stringargKey, objectargValue);
22 
23 boolSet(stringargKey, objectargValue, DateTime argDateExpiration);
24 
25 boolSet<T>(stringargKey, T entity) whereT : class;
26 
27 boolSet<T>(stringargKey, T entity, DateTime argDateExpiration) whereT : class;
28 
29 boolReplace(stringargKey,objectargValue);
30 
31 boolReplace(stringargKey,objectargValue,DateTime argDateExpiration);
32 
33 boolReplace<T>(stringargKey, T entity) whereT : class;
34 
35 boolReplace<T>(stringargKey, T entity, DateTime argDateExpiration) whereT : class;
36 
37 objectGet(stringargKey);
38 
39 T Get<T>(stringargKey);
40 
41 boolRemove(stringargKey);
42 
43 boolRemove(stringargKey, DateTime argDateExpiration);
44 
45 boolRemove();
46 
47 boolRemove(ArrayList servers);
48 
49 }

50 } 

下面这段代码对上面的接口进行了实现,里面的代码大多数人应该能够看懂,是比较简单的代码封装,如果能够了解本人上一篇的简单应用,对于这个封装的理解应该没有难度。实现代码如下:

1 usingSystem;
2 usingSystem.Collections.Generic;
3 usingSystem.Linq;
4 usingSystem.Text;
5 usingMemcached.ClientLibrary;
6 usingSystem.Collections;

8 namespaceMemcachedTest
9 {
10 publicclassMemcache:ICache
11 {
12 privateMemcachedClient client;
13 privatestaticMemcache memcache;
14 
15 ///<summary>
16 ///构造方法
17 ///</summary>
18 protectedMemcache()
19 {
20 SockIOPool pool =SockIOPool.GetInstance();
21 string[] servers ={ "127.0.0.1:11211"};
22 pool.SetServers(servers);
23 pool.MinConnections =3;
24 pool.MaxConnections =5;
25 pool.InitConnections =3;
26 pool.SocketConnectTimeout =5000;
27 pool.Initialize();
28 this.client =newMemcachedClient();
29 client.EnableCompression =false;
30 }
31 
32 publicstaticMemcache Instance()
33 {
34 if(memcache ==null)
35 {
36 memcache =newMemcache();
37 }
38 returnmemcache;
39 }
40 
41 
42 ///<summary>
43 ///判断是否包含某个键
44 ///</summary>
45 ///<param name="argKey">键值</param>
46 ///<returns></returns>
47 publicboolContainKey(stringargKey)
48 {
49 returnclient.KeyExists(argKey);
50 }
51 
52 ///<summary>
53 ///添加缓存数据
54 ///</summary>
55 ///<param name="argKey">键值</param>
56 ///<param name="argValue">存储值</param>
57 ///<returns></returns>
58 publicboolAdd(stringargKey, objectargValue)
59 {
60 returnclient.Add(argKey,argValue);
61 }
62 
63 ///<summary>
64 ///添加缓存数据
65 ///</summary>
66 ///<param name="argKey">键值</param>
67 ///<param name="argValue">存储值</param>
68 ///<param name="argDateExpiration">过期时间</param>
69 ///<returns></returns>
70 publicboolAdd(stringargKey, objectargValue, DateTime argDateExpiration)
71 {
72 returnclient.Add(argKey, argValue, argDateExpiration);
73 }
74 
75 ///<summary>
76 ///添加缓存数据
77 ///</summary>
78 ///<typeparam name="T">存储对象类型</typeparam>
79 ///<param name="argKey">键值</param>
80 ///<param name="entity">存储值</param>
81 ///<returns></returns>
82 publicboolAdd<T>(stringargKey, T entity) whereT : class
83 {
84 returnclient.Add(argKey, entity);
85 }
86 
87 ///<summary>
88 ///添加缓存数据
89 ///</summary>
90 ///<typeparam name="T">存储对象类型</typeparam>
91 ///<param name="argKey">键值</param>
92 ///<param name="entity">存储值</param>
93 ///<param name="argDateExpiration">过期时间</param>
94 ///<returns></returns>
95 publicboolAdd<T>(stringargKey, T entity, DateTime argDateExpiration) whereT : class
96 {
97 returnclient.Add(argKey, entity, argDateExpiration);
98 }
99 
100 ///<summary>
101 ///添加缓存数据,如果存在则替换原有数据
102 ///</summary>
103 ///<param name="argKey">键值</param>
104 ///<param name="argValue">存储值</param>
105 ///<returns></returns>
106 publicboolSet(stringargKey, objectargValue)
107 {
108 if(ContainKey(argKey))
109 {
110 returnfalse;
111 }
112 returnclient.Set(argKey, argValue);
113 }
114 
115 ///<summary>
116 ///添加缓存数据,如果存在则替换原有数据
117 ///</summary>
118 ///<param name="argKey">键值</param>
119 ///<param name="argValue">存储值</param>
120 ///<param name="argDateExpiration">过期时间</param>
121 ///<returns></returns>
122 publicboolSet(stringargKey, objectargValue, DateTime argDateExpiration)
123 {
124 if(ContainKey(argKey))
125 {
126 returnfalse;
127 }
128 returnclient.Set(argKey, argValue, argDateExpiration);
129 }
130 
131 ///<summary>
132 ///添加缓存数据,如果存在则替换原有数据
133 ///</summary>
134 ///<typeparam name="T">存储对象类型</typeparam>
135 ///<param name="argKey">键值</param>
136 ///<param name="entity">存储值</param>
137 ///<returns></returns>
138 publicboolSet<T>(stringargKey, T entity) whereT : class
139 {
140 if(ContainKey(argKey))
141 {
142 returnfalse;
143 }
144 returnclient.Set(argKey, entity);
145 }
146 
147 ///<summary>
148 ///添加缓存数据,如果存在则替换原有数据
149 ///</summary>
150 ///<typeparam name="T">存储对象类型</typeparam>
151 ///<param name="argKey">键值</param>
152 ///<param name="entity">存储值</param>
153 ///<param name="argDateExpiration">过期时间</param>
154 ///<returns></returns>
155 publicboolSet<T>(stringargKey, T entity, DateTime argDateExpiration) whereT : class
156 {
157 if(ContainKey(argKey))
158 {
159 returnfalse;
160 }
161 returnclient.Set(argKey, entity, argDateExpiration);
162 }
163 
164 
165 ///<summary>
166 ///替换原有缓存
167 ///</summary>
168 ///<param name="argKey">键值</param>
169 ///<param name="argValue">存储值</param>
170 ///<returns></returns>
171 publicboolReplace(stringargKey, objectargValue)
172 {
173 returnclient.Replace(argKey,argValue);
174 }
175 
176 ///<summary>
177 ///替换原有缓存
178 ///</summary>
179 ///<param name="argKey">键值</param>
180 ///<param name="argValue">存储值</param>
181 ///<param name="argDateExpiration">过期时间</param>
182 ///<returns></returns>
183 publicboolReplace(stringargKey, objectargValue, DateTime argDateExpiration)
184 {
185 returnclient.Replace(argKey,argValue,argDateExpiration);
186 }
187 
188 publicboolReplace<T>(stringargKey, T entity) whereT : class
189 {
190 returnclient.Replace(argKey,entity);
191 }
192 
193 publicboolReplace<T>(stringargKey, T entity, DateTime argDateExpiration) whereT : class
194 {
195 returnclient.Replace(argKey, entity,argDateExpiration);
196 }
197 
198 publicobjectGet(stringargKey)
199 {
200 returnclient.Get(argKey);
201 }
202 
203 publicT Get<T>(stringargKey)
204 {
205 T entity=default(T);
206 entity =(T)client.Get(argKey);
207 returnentity;
208 }
209 
210 publicboolRemove(stringargKey)
211 {
212 returnclient.Delete(argKey);
213 }
214 
215 publicboolRemove(stringargKey, DateTime argDateExpiration)
216 {
217 returnclient.Delete(argKey,argDateExpiration);
218 }
219 
220 publicboolRemove()
221 {
222 returnclient.FlushAll();
223 }
224 
225 publicboolRemove(ArrayList servers)
226 {
227 returnclient.FlushAll(servers);
228 }
229 }

230 } 

上面的代码没有注释,因为提交不了这么长的代码,去掉了注释。代码大家可以看懂的,这里不做过多的讲解。 

  学习例子源码下载 

转载于:https://www.cnblogs.com/aaa6818162/archive/2011/04/20/2022305.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/380570.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

C#| 使用String.Format()方法将小数点前的数字四舍五入

To round the digits before the decimal point, we can use String.Format() method, here is the example. 为了将小数点前的数字四舍五入&#xff0c;我们可以使用String.Format()方法&#xff0c;这里是示例。 using System;namespace ConsoleApplication1{class Program{…

php describe,php – 在Zend框架中的许多DESCRIBE查询

我刚刚在Zend中设置FirePHP,我注意到大量的DESCRIBE查询.一些页面在同一个表上都有50个或更多相同的查询.例如0.00198 connect NULL0.00449 DESCRIBE nodes NULL0.00041 SELECT nodes.* FROM nodes WHERE (((nodes.id 111))) NULL0.0037 DESCRIBE nodes NULL0.00155 SELECT no…

数据库还原后连接不上

有时候数据库还原后.在SQL Server的企业管理器里查看到用户的权限都是正常的, 但是数据库连接就是有问题. 我的解决办法就是把数据库的用户删了重新建~ 转载于:https://www.cnblogs.com/heys/archive/2006/04/26/385980.html

Android模拟器无法上网问题

方法一 首先&#xff0c;Windows下&#xff0c;配置Adroid环境变量&#xff08;Win7为例&#xff09; 1、桌面右键——》我的电脑——》高级系统设置 2、高级——》环境变量——》系统变量——》Path 3、添加android sdk目录到系统变量Path中&#xff0c;如下图&#xff1a; 注…

哈希表中能有相同元素吗_最小删除以使用哈希表使所有元素相同

哈希表中能有相同元素吗Prerequisite: Hashing data structure 先决条件&#xff1a; 哈希数据结构 Problem statement: 问题陈述&#xff1a; Find minimum number of deletions to make all elements same. 找到最小的删除数以使所有元素相同。 Example: 例&#xff1a; …

汇编指令处理的数据长度

在8086CPU中&#xff0c;可以处理两种尺寸的数据&#xff0c;byte&#xff08;8位&#xff09;和word&#xff08;16位&#xff09;&#xff0c;所以要在指令中说明是字操作还是字节操作 通过寄存器名指明要处理数据的尺寸 字操作&#xff1a; mov ax,1 mov bx,ds:[0] inc ax…

oracle 数组的用法,oracle存储过程中数组的使用

create or replace package ArrayTestPKG1 istype tt_type is table of varchar(32) INDEX BY BINARY_INTEGER; --- 定义数组type table2 is table of tableA.columnA%type index by binary_integer;function toArray(Liststr in varchar, V1 out tt_type) return number;Proc…

XP的一些小结

XP的一些小结 最近博客上关于XP的讨论也有不少文章&#xff0c;也很火热&#xff0c;我觉得&#xff1a;与其象有的人说的&#xff0c;XP是否适应国情&#xff0c;倒不如书XP是否适应具体的项目&#xff0c;具体的开发小组&#xff0c;具体的环境。我想起了在大学毕业设计时&am…

python整数转换字符串_使用Python中的str()函数将整数值转换为字符串

python整数转换字符串Given an integer value and we have to convert the value to the string using str() function. 给定一个整数值&#xff0c;我们必须使用str()函数将该值转换为字符串。 Python code to convert an integer value to the string value Python代码将整数…

2011年:签到已死?

导读&#xff1a;作为移动互联网最受关注的热点之一&#xff0c;各式LBS应用一度大量涌现&#xff0c;但其发展局限也越来越被更多的业界同行清楚认知&#xff0c;LBS只是一个功能特性还是可以支撑起一个产品&#xff1f;签到如何添加黏性和用户核心需求结合&#xff1f;LBS厂商…

Pip:基本命令和使用的指南,实现有效的包管理

目录 学习目标&#xff1a; 学习内容&#xff1a; 学习时间&#xff1a; 学习产出&#xff1a; 介绍 Pip 工具&#xff1a;Pip 是 Python 包管理工具&#xff0c;可以帮助用户方便地安装、管理和升级 Python 包&#xff1a; 安装 Pip 工具&#xff1a;学习如何在不同操作系统上…

div指令

功能&#xff1a; 除法 格式&#xff1a; div 寄存器 div 内存单元注解 除数&#xff1a;有8位和16位两种&#xff0c;在寄存器或内存单元中被除数&#xff1a;如果除数为8位&#xff0c;被除数则为16位&#xff0c;默认放在AX中&#xff0c;如果除数为16位&#xff0c;被除数…

RCP的布局

如果按照网上的例子创建出来的rcp只是一个空框架&#xff0c;如果你想显示出在插件里正常工作的界面&#xff0c;你需要在你自己的透视图类的createInitialLayout方法里布局界面。 在窗口底部显示属性页&#xff1a; layout.addView("org.eclipse.ui.views.PropertySheet&…

oracle dg 搭建方式,Linux平台 Oracle 11g DG测试环境快速搭建参考

环境现状&#xff1a;两台虚拟主机A和B&#xff1a;1. A机器已安装ASM存储的Oracle 11g 实例2. B机器已安装系统&#xff0c;配置以及目录结构均和A机器保持一致/u01 3块ASM盘DG部署规划&#xff1a;primarystandby主机JY-DBJY-DBSdb_namejyzhaojyzhaodb_unique_namejyzhaojyz…

C#| 使用String.Format()方法将小数点后的数字四舍五入

To round the digits after the decimal point, we can use String.Format() method, here is the example. 为了将小数点后的数字四舍五入&#xff0c;我们可以使用String.Format()方法&#xff0c;这里是示例。 using System;namespace ConsoleApplication1{class Program{s…

SmartFoxServer学习总结(转载)

一、要安装pro类型版本&#xff0c;此类型版本支持的功能较多&#xff0c;我安装的是SmartFoxServerPRO_1.6.2二、需要java虚拟机支持&#xff0c;最好安装jre-6u7-windows-i586-p-s.exe,把Java\jre1.6.0_07文件夹下的所有文件复制&#xff0c;覆盖到SmartFoxServerPRO_1.6.2\j…

dup和dd指令

dup 功能&#xff1a; 和数据定义的伪指令配合使用&#xff0c;用来进行数据的重复 格式&#xff1a; 数据类型 重复的次数 dup &#xff08;重复的数据&#xff09; db 3 dup(0)相当于db 0,0,0;定义了3个字节&#xff0c;他们的字节都是0 db 3 dup(0,1,2)相当于 db 0,1,2&…

汇编指令(转)

一、数据传输指令  它们在存贮器和寄存器、寄存器和输入输出端口之间传送数据.  1. 通用数据传送指令.    MOV  传送字或字节.    MOVSX 先符号扩展,再传送.    MOVZX 先零扩展,再传送.    PUSH  把字压入堆栈.    POP  把字弹出堆栈.    …

oracle10g 克隆安装,克隆Oracle Home(10g2)

克隆一个已经存在的Oracle Home&#xff0c;免掉新安装oracle10g软件的痛苦&#xff0c;如果原Oracle Home已经安装了patch就省得打patch了。一、在目标主机172.19.111.37上做安装前准备工作1、增加组和用户(和克隆主机的目录结构一样)# groupadd oinstall# groupadd dba# user…

取地址符和解引用符的区别_(&)和解引用(*)运算符的地址以及C中的指针...

取地址符和解引用符的区别Here, we are discussing about the two most useful operators with the pointers, why and how they are used? 在这里&#xff0c;我们用指针讨论两个最有用的运算符 &#xff0c;为什么以及如何使用它们&#xff1f; 1)运营商地址(&#xff06;)…