基于Memcached内存对象缓存技术应用研究
使用Memcached进行对象缓存和内存优化

使用Memcached进行对象缓存和内存优化随着互联网的迅猛发展,数据量快速增长,对于网站的访问速度和稳定性提出了更高的要求。
而在这个背景下,Memcached作为一款轻量级的缓存系统,得到了越来越多的应用。
一、什么是MemcachedMemcached是一款高性能的分布式内存缓存系统,主要用于减轻数据库负载压力,提高网站访问速度和稳定性。
它采用了键值对的存储方式,在内存中对数据进行存储和访问。
这样可以有效地降低数据库的访问频率,提高网站的响应速度。
Memcached最初由Brad Fitzpatrick开发,是一个开源项目。
它运行在一个分布式的环境中,客户端通过TCP/IP协议连接到Memcached服务器,将数据存储在内存中,可以快速地读取数据。
二、为什么要使用Memcached在一个传统的web应用中,当一个用户请求一个页面时,通常需要连接到数据库中查询数据,然后渲染网页。
但是随着访问量的增加,数据库会遇到性能瓶颈,导致访问速度变慢。
而Memcached作为一个分布式缓存系统,可以有效地解决数据库访问频率过高的问题,减轻数据库的负担。
同时,由于Memcached是一款基于内存的缓存系统,访问速度非常快,能够提供快速的读取服务。
因此,在高并发的情况下,使用Memcached可以提高网站的响应速度,提供更好的用户体验。
三、如何使用Memcached使用Memcached非常简单,只需要在代码中添加一些缓存相关的代码即可。
下面以PHP为例,介绍如何在代码中使用Memcached。
1. 安装和启动Memcached首先需要安装Memcached,并启动Memcached服务。
sudo apt-get install memcached2. 使用PHP扩展在PHP代码中,可以使用PHP扩展来连接到Memcached服务器并进行相关的操作。
我们可以使用Memcached类来进行操作,示例如下:<?php// 创建一个Memcached对象$mem = new Memcached();// 添加Memcached服务器$mem->addServer("localhost", 11211);// 存储数据$mem->set("key", "value", 3600);// 获取数据$val = $mem->get("key");// 输出数据echo $val;>3. 使用缓存进行性能优化在实际使用中,需要根据具体的应用场景来选择合适的缓存策略。
基于.NET Cache+Memcached Web缓存技术的研究与应用

共享所有缓存数据 的特点 , 提高 了网络 的利用率 ,
另一 方 面 , 内部 缓存 独 有 的 高速 访 问 性 对 于频 繁 地 访 问服务 器能 够提 高很 高 的读 取 速度 , 降低 了 网络 的吞 吐量 , 并且 也避 免 了海 量数 据 在 数 据库 和 服 务 器之 问传 输带来 的网络瓶 颈 。
首次访问 : R B S 从 D M 中取得数据保有到 cce ah
-- -
第二次后 : c e 从 8 中取得数据显示页面
求内部 Ap nt s.e 自身缓存 , 如果数据仍然不存在 , 则
21 0 1年 7月 2 9日收到 第 一作者简介 : 南 轶 ( 96 18 一), , 女 陕西西安 人 , 硕士研 究生 , 研
南
轶 , : 于. E ah +M mcc e b 等 基 N TC c e e ahdWe 缓存技术 的研究 与应用
的稳定性所 提 出 的解决 方案 , 其体 系结 构如 图 1
所示 。
扩 展性 的 We 用 。Me cce b应 m ahd的分 布式如 图 2
所示。
1 We b缓存技术的研究
11 A p n t ah . s . e c e的研究 C
日
日
日
在. e Fa eo Nt r wr m k中, 用程序缓 存通过 Ss 应 y.
t We . tR ni e C c e t u t ' ah p m.
Me ahd解 决 了 在 分 布 式 应 用 中 A p nt mcce s. e cce 能提 供 数 据 共 享 的 缺 陷 , 部 署 在 整个 缓 ah 不 它 存 的外层 , 用户 对 服务 器 的数 据请 求 到达 外 部 缓 存
Memcached使用及原理

研发管理部 陈海涛 2011-06-10
2014-6-25
1
什么是Memcached
Memcached是(运营LiveJournal的技术团队)开发的一套分布式内存对象
缓存系统。常用于减少数据库负载,提升性能。 社区网站主页:/ 开发团队主页:/ 项目主页:/
结果:老数据留在尾部(tail)。
2014-6-25 © 2004 - 2010 UC Mobile
16
Set时LRU
set时要为当前key分配内存 先从本slab队尾检查是否有过期数 据,共检查50条。
是否查到过期数据, 查到则淘汰1条后中止。 NO 新分配内存是否OK (是否还有可用内存)? NO 是否设置了-M参数表示不能踢 出有效数据 YES NO YES 内存分配失 败,返回出错 YES
2014-6-25 © 2004 - 2010 UC Mobile
7
分布式-客户端实现
2014-6-25 © 2004 - 2010 UC Mobile
8
分布式-一致性hash
2014-6-25 © 2004 - 2010 UC Mobile
9
内存管理-术语
2014-6-25 © 2004 - 2010 UC Mobile
2014-6-25 © 2004 - 2010 UC Mobile
6
常用优化参数
-f <factor> chunk size的增长因子(合理范围 1.05~2,默认:1.25) -t <num> memcached启动的工作线程数,默 认为4,建议不要超过系统CPU的个数。 -I <num>[k|K|m|M] 改变slab page的容量 大小,以调整ITEM容量的最大值,默认为1MB。 不能少于1024bytes(即1K),不能大 于128MB。memcached不推荐大于1MB。
Memcached开源高性能分布式内存对象缓存系统教程说明书

About the T utorialMemcached is an open source, high-performance, distributed memory object caching system.This tutorial provides a basic understanding of all the relevant concepts of Memcached needed to create and deploy a highly scalable and performance-oriented system.AudienceThis tutorial is designed for software professionals who wish to learn and apply the concepts of Memcached in simple and easy steps.PrerequisitesBefore proceeding with this tutorial, you need to know the basics of data structures.Copyright & DisclaimerCopyright 2018 by Tutorials Point (I) Pvt. Ltd.All the content and graphics published in this e-book are the property of Tutorials Point (I) Pvt. Ltd. The user of this e-book is prohibited to reuse, retain, copy, distribute or republish any contents or a part of contents of this e-book in any manner without written consent of the publisher.We strive to update the contents of our website and tutorials as timely and as precisely as possible, however, the contents may contain inaccuracies or errors. Tutorials Point (I) Pvt. Ltd. provides no guarantee regarding the accuracy, timeliness or completeness of our website or its contents including this tutorial. If you discover any errors on our website or inthistutorial,******************************************T able of ContentsAbout the Tutorial (i)Audience (i)Prerequisites (i)Copyright & Disclaimer (i)Table of Contents (ii)PART 1 BASICS (1)1.Memcached ─ Overview (2)2.Memcached ─ Environment (3)Installing Memcached on Ubuntu (3)Memcached Java Environment Setup (3)3.Memcached ─ Connection (4)Connection from Java Application (4)PART 2 STORAGE COMMANDS (6)4.Memcached ─ Set Data (7)Set Data Using Java Application (8)5.Memc ached ─ Add Data (9)Add Data Using Java Application (10)6.Memcached ─ Replace Data (11)Replace Data Using Java Application (12)7.Memcached ─ Append Data (14)Append Data Using Java Application (15)8.Memcached ─ Prepend Data (17)Prepend Data Using Java Application (18)9.Memcached ─ CAS Command (20)CAS Using Java Application (21)PART 3 RETRIEVAL COMMANDS (23)10.Memcached ─ Get Data (24)Get Data Using Java Application (24)11.Memcached ─ Get CAS Data (26)Get CAS Data Using Java Application (26)12.Memcached ─ Delete Data (28)Delete Data Using Java Application (28)13.Memcached ─ Increment Decrement Data (30)Incr/Decr Using Java Application (31)PART 4 STATISTICAL COMMANDS (33)14.Memcached ─ Stats (34)Stats Using Java Application (35)15.Memcached ─ Stats Items (37)16.Memcached ─ Stats Slabs (38)17.Memcached ─ Stats Sizes (39)18.Memcached ─ Clear Data (40)Clear Data Using Java Application (40)MemcachedPart 1Basics4Memcached5Memcached is an open source, high-performance, distributed memory caching system intended to speed up dynamic web applications by reducing the database load. It is a key-value dictionary of strings, objects, etc., stored in the memory, resulting from database calls, API calls, or page rendering.Memcached was developed by Brad Fitzpatrick for LiveJournal in 2003. However, it is now being used by Netlog, Facebook, Flickr, Wikipedia, Twitter, and YouTube among others The key features of Memcached are as follows:∙ It is open source.∙ Memcached server is a big hash table. ∙ It significantly reduces the database load.∙ It is perfectly efficient for websites with high database load.∙ It is distributed under Berkeley Software Distribution (BSD) license. ∙It is a client-server application over TCP or UDP.Memcached is not:∙ a persistent data store ∙ a database∙ application-specific ∙ a large object cache∙ fault-tolerant or highly available1. Memcached ─ OverviewInstalling Memcached on UbuntuTo install Memcached on Ubuntu, go to terminal and type the following commands:$sudo apt-get update$sudo apt-get install memcachedConfirming Memcached InstallationTo confirm if Memcached is installed or not, you need to run the command given below. This command shows that Memcached is running on the default port11211.$ps aux | grep memcachedTo run Memcached server on a different port, execute the command given below. This command starts the server on the TCP port 11111 and listens on the UDP port 11111 as a daemon process.$memcached -p 11111 -U 11111 -dYou can run multiple instances of Memcached server through a single installation. Memcached Java Environment SetupTo use Memcached in your Java program, you need to download spymemcached-2.10.3.jar and setup this jar into the classpath.6To connect to a Memcached server, you need to use the telnet command on HOST and PORT names.SyntaxThe basic syntax of Memcached telnet command is as shown below:$telnet HOST PORTHere,HOST and PORT are machine IP and port number respectively, on which the Memcached server is executing.ExampleThe following example shows how to connect to a Memcached server and execute a simple set and get command. Assume that the Memcached server is running on host 127.0.0.1 and port 11211.$telnet 127.0.0.1 11211Trying 127.0.0.1...Connected to 127.0.0.1.Escape character is '^]'.// now store some data and get it from memcached serverset tutorialspoint 0 900 9memcachedSTOREDget tutorialspointVALUE tutorialspoint 0 9memcachedENDConnection from Java ApplicationTo connect the Memcached server from your java program, you need to add the Memcached jar into your classpath as shown in the previous chapter. Assume that the Memcached server is running on host 127.0.0.1 and port 11211.7MemcachedExampleimport net.spy.memcached.MemcachedClient;public class MemcachedJava {public static void main(String[] args) {// Connecting to Memcached server on localhostMemcachedClient mcc = new MemcachedClient(newInetSocketAddress("127.0.0.1", 11211));System.out.println("Connection to server sucessfully");//not set data into memcached serverSystem.out.println("set status:"+mcc.set("tutorialspoint", 900,"memcached").done);//Get value from cacheSystem.out.println("Get from Cache:"+mcc.get("tutorialspoint"));}}OutputOn compiling and executing the program, you get to see the following output:Connection to server successfullyset status:trueGet from Cache:memcached8MemcachedPart 2Storage Commands9Memcached 10Memcached set command is used to set a new value to a new or existing key. SyntaxThe basic syntax of Memcached set command is as shown below: set key flags exptime bytes [noreply]valueThe keywords in the syntax are as described below:∙ key: It is the name of the key by which data is stored and retrieved from Memcached. ∙flags: It is the 32-bit unsigned integer that the server stores with the data provided by the user, and returns along with the data when the item is retrieved. ∙exptime: It is the expiration time in seconds. 0 means no delay. If exptime is more than 30 days, Memcached uses it as UNIX timestamp for expiration. ∙bytes: It is the number of bytes in the data block that needs to be stored. This is the length of the data that needs to be stored in Memcached. ∙ noreply (optional): It is a parameter that informs the server not to send any reply. ∙ value: It is the data that needs to be stored. The data needs to be passed on the new line after executing the command with the above options.OutputThe output of the command is as shown below:STORED∙STORED indicates success. ∙ERROR indicates incorrect syntax or error while saving data.4. Memcached ─ Set DataMemcachedEnd of ebook previewIf you liked what you saw…Buy it from our store @ https://11。
Memcached 缓存技术介绍

优化
调试块大小 使用 Growth Factor进行调试: 启动时指定 Growth Factor(通过 -f选项),可以适当控制 slab差异,默认值为1.25。
以 verbose模式启动 Memcached: 默认 Growth Factor输出(f=1.25): # memcached -m 1024 -c 40960 -u root -p 11219 -P /var/run/m11219.pid -vv slab class 1: chunk size 96 perslab 10922 slab class 2: chunk size 120 perslab 8738 slab class 3: chunk size 152 perslab 6898 slab class 4: chunk size 192 perslab 5461 …… 设置 Growth Factor为 2倍(f=2): # memcached -m 1024 -c 40960 -u root -p 11219 -P /var/run/m11219.pid -f2 -vv slab class 1: chunk size 96 perslab 10922 slab class 2: chunk size 192 perslab 5461 slab class 3: chunk size 384 perslab 2730 slab class 4: chunk size 768 perslab 1365 …… 输出可见,从192字节的组开始,组大小依次增大为原来的 2倍。 这样设置后,slab之间的差别比较大,有些情况下会浪费内存。 通过重新计算数据的预期平均长度,调整 growth factor,以获得最恰当的内存使用。
命中率
memcached

memcached初见memcached博客分类:memcachedmemcached缓存技术memcached 客户端memcached java⼀、概念Memcached是/doc/99761701eff9aef8941e064c.html (运营LiveJournal的技术团队)开发的⼀套分布式内存对象缓存系统,⽤于在动态系统中减少数据库负载,提升性能。
⼆、适⽤场合1. 分布式应⽤。
由于memcached本⾝基于分布式的系统,所以尤其适合⼤型的分布式系统。
2. 数据库前段缓存。
数据库常常是⽹站系统的瓶颈。
数据库的⼤并发量访问,常常造成⽹站内存溢出。
当然我们也可以使⽤Hibernate的缓存机制。
但memcached是基于分布式的,并可独⽴于⽹站应⽤本⾝,所以更适合⼤型⽹站进⾏应⽤的拆分。
3. 服务器间数据共享。
举例来讲,我们将⽹站的登录系统、查询系统拆分为两个应⽤,放在不同的服务器上,并进⾏集群,那这个时候⽤户登录后,登录信息如何从登录系统服务器同步到查询系统服务器呢?这时候,我们便可以使⽤memcached,登录系统将登录信息缓存起来,查询系统便可以获得登录信息,就像获取本地信息⼀样。
三、不适⽤场合那些不需要“分布”的,不需要共享的,或者⼲脆规模⼩到只有⼀台服务器的应⽤,memcached不会带来任何好处,相反还会拖慢系统效率,因为⽹络连接同样需要资源四、安装这⾥介绍windows环境的安装。
1. 下载memcache的windows稳定版,解压放某个盘下⾯,⽐如在c:\memcached2. 在cmd下输⼊ 'c:\memcached\memcached.exe -d install' 安装3. 再输⼊: 'c:\memcached\memcached.exe -d start' 启动。
以后memcached将作为windows的⼀个服务每次开机时⾃动启动。
Java环境下Memcached应用详解

Java 环境下 Memcached 应用详解来自网络,共享与大家。
爱好技术的朋友,一起努力!! 这里将介绍 Java 环境下 Memcached 应用,Memcached 主要是集群环境下的缓存 解决方案,希望本文对大家有所帮助。
本文将对在 Java 环境下 Memcached 应用进行详细介绍。
Memcached 主要是 集群环境下的缓存解决方案,可以运行在 Java 或者.NET 平台上,这里我们主要 讲的是 Windows 下的 Memcached 应用。
这些天在设计 SNA 的架构,接触了一些远程缓存、集群、session 复制等的 东西,以前做企业应用的时候感觉作用不大,现在设计面对 internet 的系统架 构时就非常有用了,而且在调试后看到压力测试的情况还是比较好的。
在缓存的选择上有过很多的思考,虽然说 memcached 结合 java 在序列化上 性能不怎么样,不过也没有更好的集群环境下的缓存解决方案了, 就选择了 memcached。
本来计划等公司买的服务器到位装个 linux 再来研究 memcached, 但这两天在找到了一个 windows 下的 Memcached 版本,就动手开始调整现有的 框架了。
Windows 下的 Server 端很简单, 不用安装, 双击运行后默认服务端口是 11211, 没有试着去更改端口,因为反正以后会用 Unix 版本,到时再记录安装步骤。
下 载客户端的 JavaAPI 包,接口非常简单,参考 API 手册上就有现成的例子。
目标,对旧框架缓存部分进行改造: 1、缓存工具类 2、hibernate 的 provider 3、用缓存实现 session 机制 今天先研究研究缓存工具类的改造, 在旧框架中部分函数用了 ehcache 对执 行结果进行了缓存处理,现在目标是提供一个缓存工具类,在配置文件 中配置 使用哪种缓存(memcached 或 ehcached),使其它程序对具体的缓存不依赖,同时 使用 AOP 方式来对方法执行结果进行缓存。
使用Docker容器部署和管理Memcached缓存

使用Docker容器部署和管理Memcached缓存随着互联网的快速发展,大量的数据处理和存储需求逐渐涌现。
为了提高应用程序的性能和响应速度,缓存成为了不可或缺的一部分。
而Memcached作为一种高性能的分布式内存对象缓存系统,被广泛应用于各类网站和应用程序中。
本文将探讨使用Docker容器来部署和管理Memcached缓存的方法和优势。
Docker是一个开源的容器化平台,可以将应用程序及其依赖打包成轻量级、可移植的容器,并进行部署和管理。
相较于传统的虚拟机技术,Docker提供了更快速、更高效的部署和资源利用方式。
对于Memcached这种高性能的缓存系统,使用Docker进行部署和管理是一个理想的解决方案。
首先,在部署Memcached缓存之前,我们需要在本地安装Docker。
安装完成后,可以通过Docker Hub来获取Memcached的Docker镜像。
Docker Hub是一个存储和分享Docker镜像的在线平台,用户可以在其中找到各种基础镜像和应用程序镜像。
在命令行中执行以下命令,即可从Docker Hub上下载Memcached镜像:```docker pull memcached```下载完成后,可以通过以下命令来创建一个Memcached容器:```docker run -d -p 11211:11211 --name my_memcached memcached```上述命令的含义是以后台模式运行一个Memcached容器,将容器内部的11211端口映射到本地的11211端口,并将容器命名为my_memcached。
接下来,我们可以使用telnet命令来测试是否成功部署了Memcached缓存:```telnet localhost 11211```如果成功连接上了Memcached服务器,则说明容器已经正确部署并且可以正常访问了。
使用Docker部署和管理Memcached缓存的好处不仅仅在于简化了部署过程,还包括了以下几个优势:1. 资源隔离:每个Docker容器都有自己独立的进程空间和文件系统,不会相互影响。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
基于Memcached内存对象缓存技术应用研究刘亮;徐步东;谭艳艳【摘要】The memory object cache technology has been widely concerned in the industry and academia in recent years. The Memcached is a kind of distributed cache server,it is a typical representative of distributed memory object caching system,and it is getting efficient service performance advantages by minimal consumption of resources,so it is widely used in the Web application system. The memory management mechanism and distributed algorithm owned by Memcached are not perfect. As the important target to enhance the perform-ance of Web applications,using the distributed algorithm,in-depth researching on the memory object caching technology based on Mem-cached,reduce the cost of the cachehit rate decreased as result of the addition server nodes by Consistent Hashing algorithm,improve the cache hit rate after increased the server node,and the improved Memcached is applied to an e-commerce platform. Tests show that the number of requests,traffic,TCP connections and CPU utilization rate of the e-commerce application systems are improved significantly, using Memcached can effectively reduce database server load pressure and improve Web application system response speed,enhancing the user experience effect.%内存对象缓存技术近年来受到业界和学界的广泛关注,Memcached既是一种分布式缓存服务器,也是分布式内存对象缓存系统的典型代表.其以最小的资源消耗获得高效的服务性能优势,广泛应用于Web应用系统中.Memcached自身的内存管理机制、分布式算法并非完美,对此,文中以提升Web应用系统性能为目标,利用分布式算法,基于Memcached对内存对象缓存技术进行深入研究,通过一致性Consistent Hashing算法减轻因添加服务器节点而造成缓存命中率降低的代价,提高增加服务器节点后的缓存命中率,并将优化后的Memcached应用于某电子商务平台之中.测试表明,此电子商务应用系统的请求数、流量、TCP连接数以及CPU利用率等技术指标改善明显,利用Memcached能够有效减轻数据库服务器负载压力,提高Web应用系统响应速度,增强用户体验效果.【期刊名称】《计算机技术与发展》【年(卷),期】2015(025)011【总页数】5页(P204-208)【关键词】内存对象;缓存系统;分布式算法;电子商务【作者】刘亮;徐步东;谭艳艳【作者单位】山东师范大学信息技术管理处,山东济南 250014;山东师范大学信息技术管理处,山东济南 250014;山东师范大学信息科学与工程学院,山东济南250014【正文语种】中文【中图分类】TP311.52响应速度是基于Web技术的应用系统首要面对的关键问题,经过业界学者的不懈探索,从最初在线程内缓存对象,到共享内存缓存机制,系统仅仅局限于一台计算机上。
内存缓存技术基于操作系统原理中非常经典的理论,即20%的数据通常在80%的时间内被访问[1],因此应用系统的目标就可以集中在这20%的数据中,当数据被首次访问后,就将其置入内存中,再次访问这些数据时,只需从内存中读取并赋予更高访问级别即可。
目前,国内外对缓存技术的研究现状主要有两方面,一是对缓存技术算法的改进,二是对缓存技术架构的应用创新。
GLakshaman认为,基于客户端的代理服务器调度算法能选择最快的代理服务器节点,提高文档获取速度[2]。
Rosu以GLakshaman的算法为基础,将代理加速器设置于代理服务器之前,并缓存代理服务器中的热点对象,提高了代理服务器的响应速度以及整体性能[3]。
在QoS-Capable网络的应用中,M.Rezvan和K.Pawlikowski所提出的分布式缓存体系架构降低了QoS路由算法的计算量,并缓存已有的计算结果,提高了路由的整体速度[4]。
Sinisa Srbljic等在分布式网络的网络冲突、代理服务器过载等问题中,采用最佳缓存代理服务器数量的解决策略,促进了缓存技术在分布式网络中的应用[5]。
Memcached通过缓存数据对象于内存中,降低数据库访问次数,减轻数据库负载,提高了Web应用系统的动态访问速率,增强了Web应用系统的灵活性和可扩展性,是分布式内存对象缓存系统的典型代表。
Memcached也是一种分布式缓存服务器,在高速运行的过程中具备以下特点:(1)协议简单;(2)基于libevent程序库的事件处理方式;(3)内置内存存储方式;(4)非互相通信的分布式。
Memcached并非适用于所有应用,对于非分布、非共享、规模足够小的服务器应用,由于网络连接等资源的限制,Memcached会拖慢系统效率。
在应用中经过测试,其本地读写速度比直接读取内存数据慢10倍之多。
通常情况,Memcached多用于数据库前端,以减少SQL解析、磁盘读写等数据库操作,并且其在内存中管理数据,比直接读取数据库具有更高的速率和性能。
1.1 Memcached内存管理机制Memcached是一个基于存储键/值对的Hashmap,它通过三十二位元的循环冗余校验(CRC-32)计算键值后,将数据分散于不同的机器上,进程本身非常轻,仅占用很少的CPU资源。
Memcached采用对象缓存分配机制(Slab Allocator)分配、管理内存。
在此之前,操作系统对内存的分配,仅仅简单地对所有数据记录进行动态内存分配(malloc)和释放(free),从而导致内存碎片的增多,增加了内存管理器的任务,严重情况下,会造成操作系统本身比Memcached进程还慢。
鉴于此,Slab Allocator将分配的内存(Page,默认1 MB)分割成预先规定的特定长度的块来解决内存碎片问题。
Slab Allocator将分配的内存分割成块(Chunk),块的大小不一,然后把相同大小的块组成组(Slab Class,Chunk的集合),并重复利用已分配的内存[6]。
Memcached服务器端获取客户端发送的数据,按照数据大小,选择最适合的Slab。
Slab中的空闲Chunk列表存储于Memcached服务器中,服务器则根据该列表选择相应的Chunk缓存数据。
尽管Slab Allocator机制有效解决了内存碎片问题,但由于分配的内存是特定长度,造成服务器无法加以有效利用。
例如,128字节的Chunk缓存100字节的数据,会存在28字节的浪费[6]。
对此问题的解决方案没有办法做到完美,但在两种情况下,可以使用适合数据大小的Slab Classes列表以提高内存利用率,一是客户端发送数据的公用大小在预知的情况下,二是在缓存数据的大小相同的情况下。
尽管无法达到最优化,但通过设置Memcached的Growth Factor因子能调节Slab间的差异。
Growth Factor因子默认值f=1.25[7]的10组输出,见表1。
表1中输出结果计算误差的设置是为了保持字节数的对齐。
因此,将Memcached引入Web应用中时,数据的预期平均长度需重新计算,从而调整Growth Factor获得最优设置。
对于已分配的内存,Memcached并不释放,记录超时后,其存储空间即可被重复利用。
Memcached采用lazy expiration技术在获取数据时检查其时间戳,判断记录是否过期,因此系统不会在内部过期监视上消耗CPU时间。
Memcached对已超时记录的空间有优先使用权,但也难免在新增记录时会发生分配空间不足的情况,因此LRU(Least Recently Used)算法经常被用来删除最近最少使用的记录以释放内存空间,并将其分配给新的记录[8]。
1.2 Memcached的分布式Memcached是“伪分布式”缓存服务器,其实现非常简单,仅包括内存存储功能,而其分布式功能是由客户端实现的。
node1、node2和node3表示三台Memcached服务器,应用程序将要保存键名为“shijingshan”,“chaoyang”,“haidian”,“dongcheng”和“xicheng”的数据。
首先,Web应用向Memcached中添加“shijingshan”,客户端获取Web应用传来的“shijingshan”,然后根据“键”选择存储数据的Memcached服务器,并存储“shijingshan”及其值。
同理,“chaoyang”,“haidian”,“dongcheng”,“xicheng”都是先选择服务器再保存键值。
其次,Web应用获取数据时,把数据的键“shijingshan”发送给客户端,客户端依据“键”以及数据存储时的算法选择Memcached服务器,然后执行获取命令,只要数据没过期或被删除,就能获得原来保存的数据。
因此,Memcached分布式,就是根据“键”值的不同把数据存储到不同的服务器上,实现数据的分布式存取。
随着Memcached服务器的增加,键值Key分散于不同的服务器中,因此如果部分Memcached服务器发生宕机,对其他缓存服务器没有影响,系统能够正常运行。