内容简介:初见memcached
分类:工具
发布于 2016年12月03日

概述
Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载。
安装memcached
ubuntu :
sudo apt-get install memcached
安装后可以执行以下命令来查看是否已经在运行
ps -aux | grep memcache
安装成功并已经运行会得到以下结果
memcache 1043 0.0 0.0 325536 2808 ? Ssl 18:11 0:00 /usr/bin/memcached -m 64 -p 11211 -u memcache -l 127.0.0.1
连接memcached
telnet HOST PORT
连接本地默认端口的memcached
telnet 127.0.0.1 11211
基本使用(增删改查)
1. 增
add key token expire_time size DATA
- key : 用来代表这一数据的唯一标识
- token : 一个unsigned 32位整数,不需要唯一,当获取数据时会和数据一起返回
- expire_time : 数据存活的时间(单位:秒)
- size : 数据所占的字节数
- DATA : 实际的数据
后面命令类似的都有这些元素组成。
add paul 0 900 4 paul STORED
上面是一个示范例子,add命令需要注意
- 如果已经存在这个key,就无法保存,提示NOT_STORED
- 字节数必须对应数据大小,否则提示CLIENT_ERROR bad data chunk。
另一个可以添加数据的命令
set key token expire_time size DATA
例子
set david 0 900 5 david STORED
用法和add基本一样,但唯一不同的就是
- 如果key存在的话,则覆盖之前的数据,即替换
2. 删
delete key
3 . 改
set key token expire_time size DATA
replace key token expire_time size DATA
这两个命令的使用方法一样,效果一样
4. 查
get key
示例
get paul VALUE paul 0 4 paul END
其他命令
append
在key数据项后面添加数据
append key token expire_time size DATA
prepend
在key 数据项前面添加数据
prepend key token expire_time size DATA
gets
gets key
得到数据项 key,同时返回一个整数 , 用以和cas命令结合使用
示例
gets paul VALUE paul 0 4 11 paul END
- 与get相比,gets的结果在数据项大小size后有一个整数,和cas结合使用
cas
cas key token expire_time size int DATA
int : gets 命令获取数据项时返回的整数
更改数据项key。条件是在上一次gets后此数据项不能有更改
示例
cas paul 0 900 7 11 iampaul STORED get paul VALUE paul 0 7 iampaul END
当数据项在gets后已被修改,cas就会失败
gets paul VALUE paul 0 7 12 iampaul END set paul 0 900 4 paul STORED cas paul 0 900 6 12 paulme EXISTS
java连接memcached
需要下载jar包 : spymemcached-2.10.0.jar
maven 添加依赖:
<dependency> <groupId>net.spy</groupId> <artifactId>spymemcached</artifactId> <version>2.10.0 </version> </dependency>
关键类:
MemcachedClient
建立连接
//ip地址与端口 InetSocketAddress inetSocketAddress = new InetSocketAddress("localhost",11211); //通过inetSocketAddress创建MemcachedClient MemcachedClient memcachedClient = new MemcachedClient(inetSocketAddress); /**所有操作在开始和关闭中间执行*/ //关闭memcachedClient memcachedClient.shutdown();
主要方法
//增删改方法,都返回OperaionFuture<Boolean> OperationFuture<Boolean> of = memcachedClient.add(key, exp, obj); OperationFuture<Boolean> of = memcachedClient.delete(key); OperationFuture<Boolean> of = memcachedClient.set(key, exp, o); OperationFuture<Boolean> of = memcachedClient.replace(key, exp, o); //of.get()得到执行的结果 boolean result = of.get(); //get()方法得到数据项 Object obj = memcachedClient.get(key); //gets()方法得到CASValue, CASValue<Object> casValue = memcachedClient.gets(key); //casValue的getValue()方法得到数据项 Object obj = casValue.getValue(); //得到用于保存的token long token = casValue.getCas(); //用key,token,value/obj修改数据 memcachedClient.cas(key, token, value);
这些方法都是和上面所述的 memcached 指令相对应的。
需要注意的点
- 操作需要在创建连接和关闭连接之间进行。JavaWeb项目可以在ServlectContext的生死监听器创建和关闭。
- 要保存的对象一定要 序列化 , 不序列化不能保存,但是没有任何提示。
Eight hours work, eight hours sleep, and eight hours recreation
- Brigham Young
胡杨 2016年12月09日 17时49分
过来看看留个痕迹~QJuSwmE2Tdo 2017年01月12日 02时15分
Fidnnig this post solves a problem for me. Thanks!凯哥自媒体 2016年12月16日 09时39分
看到很多不会的paulWen 回复 凯哥自媒体 2016年12月16日 17时54分
有帮助就好。iFtrmAGRyiA 2017年01月12日 09时21分
Posts like this brehgtin up my day. Thanks for taking the time. http://rtbmgjqai.com [url=http://ffksnp.com]ffksnp[/url] [link=http://hxlglqhzf.com]hxlglqhzf[/link]nY4fKhfS4 2017年01月11日 06时15分
not getting drunk..but releasing DMT from plant ethogens occuring natrually within all ligfeHallucinatin.(actually trancending) u cannot understand unless u experianced .foreal ..its all true. and LOve saves all..GODUJX4u6ez 2017年01月14日 00时59分
You are so awesome for helping me solve this myrsety.hgyNsqVS 2017年01月15日 04时32分
Always refrhseing to hear a rational answer. http://uidnrd.com [url=http://rnjfippfm.com]rnjfippfm[/url] [link=http://rtjprtehum.com]rtjprtehum[/link]以上所述就是小编给大家介绍的《初见memcached》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:- Python爬虫初见
- 初见函数式编程
- [译] MySQL 组复制初见
- React系列十五 - Redux(一)初见
- Git Large File Storage 初见指南
- 微众银行马智涛:公众联盟链发展初见成效,分布式商业模式可期
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Data Mining
Jiawei Han、Micheline Kamber、Jian Pei / Morgan Kaufmann / 2011-7-6 / USD 74.95
The increasing volume of data in modern business and science calls for more complex and sophisticated tools. Although advances in data mining technology have made extensive data collection much easier......一起来看看 《Data Mining》 这本书的介绍吧!