Memcached - Base
Memcached
标签 : Java与NoSQL
在程序的实现中, 经常会忽略程序的运行时间. 即使采用类似的实现方法, 有时候运行速度也会相差很多. 大多数情况下, 这一速度上的差异是由数据访问速度的差异所导致的.
– 松本行弘<代码的未来>
Memcached is an in-memory key-value store for small chunks of arbitrary data (strings, objects) from results of database calls, API calls, or page rendering.
Free & open source, high-performance, distributed memory object caching system, generic in nature, but intended for use in speeding up dynamic web applications by alleviating database load.
编译
- 环境
编译Memcached需要使用gcc/make/cmake/autoconf/libtool等工具:
yum install gcc make cmake autoconf libtool
Memcached的事件循环机制基于libevent库:
wget https://github.com/libevent/libevent/releases/download/release-2.0.22-stable/libevent-2.0.22-stable.tar.gz
./configure --prefix=/usr/local/libevent
make && make install
- Memcached
wget http://memcached.org/files/memcached-1.4.25.tar.gz
./configure --prefix=/usr/local/memcached --with-libevent=/usr/local/libevent
make && make install
/usr/local/memcached/bin/memcached -vv -d #启动
- Manual
-p <num> TCP port number to listen on (default: 11211)
-U <num> UDP port number to listen on (default: 11211, 0 is off)
-s <file> UNIX socket path to listen on (disables network support)
-A enable ascii "shutdown" command
-a <mask> access mask for UNIX socket, in octal (default: 0700)
-l <addr> interface to listen on (default: INADDR_ANY, all addresses)
<addr> may be specified as host:port. If you don\'t specify
a port number, the value you specified with -p or -U is
used. You may specify multiple addresses separated by comma
or by using -l multiple times
-d run as a daemon
-u <username> assume identity of <username> (only when run as root)
-m <num> max memory to use for items in megabytes (default: 64 MB) ## 设置最大使用内存
-c <num> max simultaneous connections (default: 1024) ## 设置最大连接数
-v verbose (print errors/warnings while in event loop)
-vv very verbose (also print client commands/reponses)
-f <factor> chunk size growth factor (default: 1.25) ## 设置增长因子
-n <bytes> minimum space allocated for key+value+flags (default: 48)
-t <num> number of threads to use (default: 4)
-R Maximum number of requests per event, limits the number of
requests process for a given connection to prevent
starvation (default: 20)
-b <num> Set the backlog queue limit (default: 1024)
-I Override the size of each slab page. Adjusts max item size
(default: 1mb, min: 1k, max: 128m)
-F Disable flush_all command
- Client
Memcached可以通过C、C++、Java、PHP、Python、Ruby、Perl、Erlang、Lua等语言来访问.此外, Memcached的通信协议由简单文本构成, 使用如Telnet的方式也很容易访问:
telnet host port
命令
Memcached提供的命令简单易懂, 因此在此只做简单介绍, 详细可参考官方Wiki的Commands部分.
Storage Commands
<command name> <key> <flag> <expire> <bytes>
<data block>
Memcached提供的存储的命令如下:
| 命令 | 描述 |
|---|---|
set |
Store this data, possibly overwriting any existing data. New items are at the top of the LRU. |
add |
Store this data, only if it does not already exist. New items are at the top of the LRU. If an item already exists and an add fails, it promotes the item to the front of the LRU anyway. |
replace |
Store this data, but only if the data already exists. Almost never used, and exists for protocol completeness (set, add, replace, etc) |
append |
Add this data after the last byte in an existing item. This does not allow you to extend past the item limit. Useful for managing lists. |
prepend |
Same as append, but adding new data before existing data. |
cas |
Check And Set (or Compare And Swap). An operation that stores data, but only if no one else has updated the data since you read it last. Useful for resolving race conditions on updating cache data. |
Storage Commands后面参数的形式/含义相同:
| 参数 | 描述 |
|---|---|
| key | 键: 类似于Map内的key,不能重复 |
| flag | 自定义标志位(正整数) |
| expire | 失效时间 (/s) |
| bytes | 缓存内容长度 |
注意: 命令cas的格式稍有不同
cas <key> <flag> <expire> <bytes> <value>
<data block>
只有当value值与gets(见下)返回的identifier数字一致时才会生效,否则返回EXISTS.
Retrieval Commands
get/gets <key>
| 参数 | 描述 |
|---|---|
get |
Command for retrieving data. Takes one or more keys and returns all found items. |
gets |
An alternative get command for using with CAS. Returns a CAS identifier (a unique 64bit number) with the item. Return this value with the cas command. If the item’s CAS value has changed since you gets’ed it, it will not be stored. |
Delete
delete key [time]
Removes an item from the cache, if it exists. time可选,指定rm该key的time秒内,不允许操作该key.
Incr/Decr
incr/decr <key> <number>
Increment and Decrement. If an item stored is the string representation of a 64bit integer, you may run incr or decr commands to modify that number. You may only incr by positive values, or decr by positive values. They does not accept negative values.
- 应用场景: 秒杀
原先秒杀的下单过程的所有操作都通过数据库,比如读取库存/写入订单/更新库存/收缴欠款等, 响应缓慢且对数据库压力具大,现在可将与库存相关操作都放到Memcached内: 在Memcached中设置一个count(库存量),每个秒杀decr之,成功之后再进行后面的一系列下单操作,由于主要在内存操作,速度非常快.
Statistics
| 参数 | 描述 |
|---|---|
stats |
ye ‘ole basic stats command. 如计算统计缓存命中率: (get_hits / cmd_get) * 100% |
stats items |
Returns some information, broken down by slab, about items stored in memcached. |
stats slabs |
Returns more information, broken down by slab, about items stored in memcached. More centered to performance of a slab rather than counts of particular items. |
stats sizes |
A special command that shows you how items would be distributed if slabs were broken into 32byte buckets instead of your current number of slabs. Useful for determining how efficient your slab sizing is. |
Flush
flush_all
Invalidate all existing cache items. Optionally takes a parameter, which means to invalidate all items after N seconds have passed.
This command does not pause the server, as it returns immediately. It does not free up or flush memory at all, it just causes all items to expire.
限制
| 参数 | 限制 |
|---|---|
| key长度限制 | 文本协议支持250B, 二进制协议支持 65536B |
| value限制 | 1M |
| 总内存限制 | 32位操作系统最大支持2G |
Memcached Slab Allocator
Memcached使用Slab Allocator机制来管理内存, 缓解内存碎片化问题:
Memcached启动时首先向操作系统申请一大块内存,并将其分割成各种尺寸的Chunk,并将尺寸相同的Chunk分成组Slab Class.
其中,Chunk块就是用来存储key-value数据的最小单位; 而每个Slab Class的大小可以在Memcached启动的时通过指定-f参数设置(默认1.25,所以如果第一组Chunk的大小为88B,第二组Chunk为122B,依此类推).
![]()
当Memcached收到客户端发送过来的数据时会首先根据数据大小选择一个最合适的Slab Class,然后通过查询该Slab Class内空闲Chunk列表缓存数据.
![]()
当一条数据过期/delete时,该记录所占用的Chunk就可以回收,重新添加到空闲列表中.
Growth Factor调优
从以上过程可以看到Memcached内存管理制效率非常高,不会造成内存碎片,但它最大的缺点是会导致内存空间浪费:
因为每个Chunk的长度是“固定”的,所以变长数据无法充分利用这些空间:
![]()
如图:将100字节的数据缓存到128字节的Chunk中,剩余28个字节就浪费掉了.
Chunk空间的浪费问题无法彻底解决,只能缓解: 比如开发者预先对缓存数据长度进行估计, 并指定合理的Chunk大小. 但可惜的是,Memcached目前还不支持自定义Chunk的大小,但可以通过-f参数来调整Slab Class内Chunk的Growth Factor(增长因子):
/usr/local/memcached/bin/memcached -u nobody -vvv -f 1.25
注意:当
f=1.25时,从Memcached输出结果来看,某些相邻的Slab Class大小比值并非精确的1.25,这些误差是为了保持字节对齐而故意设置的.
Memcached数据过期
Memcached会优先使用已超时Item空间,即便如此,也会发生追加新记录时空间不
足的情况,此时就要使用Least Recently Used(LRU)机制来分配空间:
Memcached为每个Item维持一个计数器count, 当某个Item被请求时,count+1,当空间不足时,通过count断其中某个Item最近最少使用, 然后踢出.
Lazy Expiration
Memcached内部不会监视记录是否过期,而是当get时查看记录时间戳,以检查否过
期. 这种技术被称为Lazy Expiration, 好处是不会在Item过期监视上耗费CPU时间.
永久数据被踢
“Memcached数据丢失”(永久数据被踢): 明明将key设为永久有效,却莫名其妙的丢失了.
这种情况需要从以下几个方面分析:
1. 如果 Slab里的很多Chunk已经过期,但过期后并未被get,Memcached则不知道他们已经过期.
2. 永久数据很久没get,不活跃, 同时Memcached相关Slab内存紧张, 如果新增Item, 很可能被LRU机制踢出.
解决方案: 永久数据和非永久数据分开存放.
Memcached - Base的更多相关文章
- ASP.Net MVC4+Memcached+CodeFirst实现分布式缓存
ASP.Net MVC4+Memcached+CodeFirst实现分布式缓存 part 1:给我点时间,允许我感慨一下2016年 正好有时间,总结一下最近使用的一些技术,也算是为2016年画上一个完 ...
- Key/Value之王Memcached初探:三、Memcached解决Session的分布式存储场景的应用
一.高可用的Session服务器场景简介 1.1 应用服务器的无状态特性 应用层服务器(这里一般指Web服务器)处理网站应用的业务逻辑,应用的一个最显著的特点是:应用的无状态性. PS:提到无状态特性 ...
- 【转】 Key/Value之王Memcached初探:三、Memcached解决Session的分布式存储场景的应用
一.高可用的Session服务器场景简介 1.1 应用服务器的无状态特性 应用层服务器(这里一般指Web服务器)处理网站应用的业务逻辑,应用的一个最显著的特点是:应用的无状态性. PS:提到无状态特性 ...
- CentOS7下安装Mysql和Memcached 以及 使用C#操作Mysql和Memcached
我本身是学.net的,但是现在很多主流SQL和NOSQL都是部置在linux下,本着好学的精神,前段时间装了个虚拟机,在其装上CentOS64位的服务器系统,对于英文0基础,linux0基础的我来说, ...
- 方法过滤器,分布式缓存 Memcached实现Session解决方案
控制器-〉方法过滤器-〉controller-> 方法 所以通过建立controller基类的方法进行方法过滤,所有控制器先执行基类的OnActionExecuting 方法. using Sp ...
- Python操作memcached及redis
Memcached Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态.数据库驱动网站的速度 ...
- memcached(二)事件模型源码分析
memcachedd事件模型 在memcachedd中,作者为了专注于缓存的设计,使用了libevent来开发事件模型.memcachedd的时间模型同nginx的类似,拥有一个主进行(master) ...
- 【原创】基于Memcached 实现用户登录的Demo(附源码)
一个简单的Memcached在Net中运用的一个demo.主要技术 Dapper+MVC+Memcached+sqlserver, 开发工具为vs2015+Sql 效果图如下: 登录后 解决方案 主要 ...
- memcached与spring
key的生成规则 update 与 query 的参数不一样,如何让其生成一样的key 列表缓存如何定义key及失效 最近同事推荐了一个开源项目:Simple-Spring-Memcached(简称s ...
随机推荐
- 再深刻理解下web3.js中estimateGas如何计算智能合约消耗的gas量
我们可使用web3.js框架的estimateGas函数获得一个以太坊智能合约的Gas估计值 ,通过执行一个消息调用或交易,该消息调用或交易直接在节点的VM中执行,并未在区块链中确认,函数会返回估算使 ...
- [HAOI 2007]理想的正方形
Description 有一个a*b的整数组成的矩阵,现请你从中找出一个n*n的正方形区域,使得该区域所有数中的最大值和最小值的差最小. Input 第一行为3个整数,分别表示a,b,n的值第二行至第 ...
- [Noi2016]区间
题目描述 在数轴上有 n个闭区间 [l1,r1],[l2,r2],...,[ln,rn].现在要从中选出 m 个区间,使得这 m个区间共同包含至少一个位置.换句话说,就是使得存在一个 x,使得对于每一 ...
- VK Cup 2017 - Round 2
FallDream打的AB都FFT了,只剩一个我打的C,没进前一百,之后看看马拉松复活赛有没机会呗. A. Voltage Keepsake 题目大意:n个东西,每个东西一开始有bi能源,每秒消耗ai ...
- Blocks
Description solution 这题和之前做过的一题的一个套路非常类似:把不是更优的决策给去掉,使得序列变得具有单调性,分析这题: 发现如果两个右端点 \(i\),\(j\) 满足 \(su ...
- 【模版 Floyd最小环】
输入 输入中有多组数据.请用SeekEof判断是否到达文件结束. 对于每组数据: 第一行有两个正整数N,M,分别表示学校的景点个数和有多少对景点之间直接有边相连.(N< =100,M< = ...
- 百度杯CTF夺旗大赛9月场writeup
在i春秋上注册了账号,准备业余时间玩玩CTF.其中的九月场已经打完了,但是不妨碍我去做做题,现在将一些思路分享一下. 一. 第二场web SQL 根据题目来看是一个SQL注入的题目: 这里推荐两篇文章 ...
- 华科机考:N阶楼梯上楼
时间限制:1秒空间限制:32768K 题目描述 N阶楼梯上楼问题:一次可以走两阶或一阶,问有多少种上楼方式.(要求采用非递归) 输入描述: 输入包括一个整数N,(1<=N<90). 输出描 ...
- PHP 中 config.m4 的探索
PHP 中 config.m4 的探索 最近在看php扩展相关的东西,虽然来来回回编辑了好多次config.m4,并且也在技术社区看到了 config.m4是什么?什么作用? 类的问题,但是还是觉得有 ...
- [ 学习笔记 ] Hibernate框架学习之一
一.JavaEE开发三层结构和三大框架的对应关系: Struts2框架 -> 表现层 web层(MVC是表现层的设计模型) 业务层 service层 Hibernate框架 -> 持久层 ...