@ghosert
2014-11-02T22:47:17.000000Z
字数 1396
阅读 6886
技术入门
以 Ubuntu 为例:
sudo apt-get install memcached
Memcached 项目主页,这里可以了解 memcached 的一些基本信息。
Java 客户端 spymemcached
Python 客户端
待定
memcached -m 64 -p 11211 -u jiawzhang
memcached -m 64 -p 11212 -u jiawzhang
$ telnet localhost 11211
# set key-value
# 0: flag for client, 60: expire time, 5: bytes length, 'value' is 5 bytes-length
set key 0 60 5
value
STORED
# get key-value
get key
VALUE key 0 5
value
END
# Ctrl+] and quit 退出 telnet
示例 1:
MemcachedClient c=new MemcachedClient(
new InetSocketAddress("hostname", portNum));
// Store a value (async) for one hour
c.set("someKey", 3600, someObject);
// Retrieve a value (synchronously).
Object myObject=c.get("someKey");
示例 2:
// Get a memcached client connected to several servers
MemcachedClient c=new MemcachedClient(
AddrUtil.getAddresses("server1:11211 server2:11211"));
// Try to get a value, for up to 5 seconds, and cancel if it doesn't return
Object myObj=null;
Future<Object> f=c.asyncGet("someKey");
try {
myObj=f.get(5, TimeUnit.SECONDS);
} catch(TimeoutException e) {
// Since we don't need this, go ahead and cancel the operation. This
// is not strictly necessary, but it'll save some work on the server.
f.cancel(false);
// Do other timeout related stuff
}
Memcached入门指南
http://www.cnblogs.com/javapro/archive/2012/05/24/2516016.html
Memcached 入门-介绍-使用-优化
一致性 hash 算法( consistent hashing )
大多数 Memcached 客户端例如 spymemcached 支持一致性 hash,算法原理: