一,什么是memcached?
- Memcached是一个高性能的分布式内存对象缓存系统,用于动态web应用以减轻数据库负载。。它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态、数据库驱动网站的速度。Memcached基于一个存储键/值对的hashmap。(摘自百科)
- 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 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. Memcached is simple yet powerful. Its simple design promotes quick deployment, ease of development, and solves many problems facing large data caches. Its API is available for most popular languages.(摘自:http://memcached.org/)
二,memcached的主要优点和功能是什么?
- memcached的主要设计目的是减少数据库读取的次数,对初次读取进行缓存处理,提供高速查询服务。
- 由于使用了一致性哈希算法,分布式结构的memcached可以很好地达到负载均衡。
- memcached介绍参考:http://blog.sina.com.cn/s/blog_493a845501013ei0.html
三,如何使用memcached?
Memcached守护进程(daemon )是用C写的,但是客户端可以用任何语言来编写,并通过memcached协议与守护进程通信。
1,memcached在ubuntu上的安装
安装libevent
- libevent下载,地址:http://libevent.org/
- 解压:tar -zxvf libevent-******
- 移动:mv libevent-***** /usr/local/libevent
- ./configure --prefix=/usr
- make
- make install
- 查看是否成功:ls -al /usr/lib | grep libevent
- (安装过程可能出现权限问题,sudo即可)
安装memcached:
- memcached下载,地址:http://memcached.org/
- tar xzvf memcached-1.4.15.tar.gz
- cd memcached-1.4.15
- ./configure --with-libevent=/usr
- make
- make install
- 查看是否成功:ls -al /usr/local/bin/memcached
2,memcached命令解释及使用
启动memcached:
- /usr/local/bin/memcached -d -m 1024 -u root -p 11211 -P /tmp/memcached.pid
参数:
-d 启动守护进程(后台运行)
-m 分配给memcache使用的内存,单位是MB
-u 运行memcached的用户
-l 监听的服务器IP
-p 监听的服务器端口,默认是11211
-P(大写) 保存Memcache的pid文件,后面跟路径
检测是否启动可以用tlenet命令: telnet 127.0.0.1 11211
stats可以查看memcached的详细信息
memcached命令使用:
参考文章:http://www.cnblogs.com/wayne173/p/5652034.html,以下部分截图
3,memcached Java API使用
memcached提供了三种API:
- 官方提供的基于传统阻塞io由Greg Whalin维护的客户端
- Dustin Sallings实现的基于java nio的Spymemcached
- XMemcached
这里我们使用XMemcached来介绍。
api的使用方法比较简单,直接上代码。
1 import java.io.IOException; 2 import java.util.concurrent.TimeoutException; 3 import net.rubyeye.xmemcached.utils.AddrUtil; 4 import net.rubyeye.xmemcached.MemcachedClient; 5 import net.rubyeye.xmemcached.MemcachedClientBuilder; 6 import net.rubyeye.xmemcached.XMemcachedClientBuilder; 7 import net.rubyeye.xmemcached.exception.MemcachedException; 8 9 public class MemcachedTest { 10 11 public static void main(String[] args) { 12 //新建一个创建器,通过AddrUtil获取memcached的ip:port 13 MemcachedClientBuilder builder = new XMemcachedClientBuilder(AddrUtil.getAddresses("192.168.3.152:11211")); 14 //memcached客户端 15 MemcachedClient memcachedClient; 16 try { 17 //建立一个客户连接 18 memcachedClient = builder.build(); 19 //set一个键值对,参数1:key 参数2:expireTimeS,参数3:value 20 memcachedClient.set("hello", 0, "Hello,xmemcached"); 21 //get根据键取值 22 String value = memcachedClient.get("hello"); 23 System.out.println("hello=" + value); 24 //delete根据键删去值 25 memcachedClient.delete("hello"); 26 value = memcachedClient.get("hello"); 27 System.out.println("hello=" + value); 28 // close memcached client 29 memcachedClient.shutdown(); 30 } catch (MemcachedException e) { 31 System.err.println("MemcachedClient operation fail"); 32 e.printStackTrace(); 33 } catch (TimeoutException e) { 34 System.err.println("MemcachedClient operation timeout"); 35 e.printStackTrace(); 36 } catch (InterruptedException e) { 37 // ignore 38 } catch (IOException e) { 39 System.err.println("Shutdown MemcachedClient fail"); 40 e.printStackTrace(); 41 } 42 } 43 }