[关闭]
@ghosert 2014-11-02T14:47:17.000000Z 字数 1396 阅读 1773

Memcached 初探

技术入门


安装

1. 安装 Memcached

以 Ubuntu 为例:

  1. sudo apt-get install memcached

Memcached 项目主页,这里可以了解 memcached 的一些基本信息。

2. 安装客户端

Java 客户端 spymemcached

下载地址

Python 客户端

待定

使用

1. 启动多个 Memcached 实例

  1. memcached -m 64 -p 11211 -u jiawzhang
  2. memcached -m 64 -p 11212 -u jiawzhang

2. telnet 客户端访问

  1. $ telnet localhost 11211
  2. # set key-value
  3. # 0: flag for client, 60: expire time, 5: bytes length, 'value' is 5 bytes-length
  4. set key 0 60 5
  5. value
  6. STORED
  7. # get key-value
  8. get key
  9. VALUE key 0 5
  10. value
  11. END
  12. # Ctrl+] and quit 退出 telnet

3. Java spymemcached 客户端示例

示例 1:

  1. MemcachedClient c=new MemcachedClient(
  2. new InetSocketAddress("hostname", portNum));
  3. // Store a value (async) for one hour
  4. c.set("someKey", 3600, someObject);
  5. // Retrieve a value (synchronously).
  6. Object myObject=c.get("someKey");

示例 2:

  1. // Get a memcached client connected to several servers
  2. MemcachedClient c=new MemcachedClient(
  3. AddrUtil.getAddresses("server1:11211 server2:11211"));
  4. // Try to get a value, for up to 5 seconds, and cancel if it doesn't return
  5. Object myObj=null;
  6. Future<Object> f=c.asyncGet("someKey");
  7. try {
  8. myObj=f.get(5, TimeUnit.SECONDS);
  9. } catch(TimeoutException e) {
  10. // Since we don't need this, go ahead and cancel the operation. This
  11. // is not strictly necessary, but it'll save some work on the server.
  12. f.cancel(false);
  13. // Do other timeout related stuff
  14. }

更多详细示例

相关资料

  1. Memcached入门指南

    http://www.cnblogs.com/javapro/archive/2012/05/24/2516016.html

  2. Memcached 入门-介绍-使用-优化

    http://leiwuluan.iteye.com/blog/1173402

  3. 一致性 hash 算法( consistent hashing )

    大多数 Memcached 客户端例如 spymemcached 支持一致性 hash,算法原理:

    http://blog.csdn.net/sparkliang/article/details/5279393

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注