实战Memcached缓存系统(2)Memcached Java API基础之MemcachedClient

尊重知识,转载请注明本文来自:编程艺术家Poechant的CSDN博客 http://blog.csdn.net/poechant

 

1. 构造函数

 

  1. public MemcachedClient(InetSocketAddress[] ia) throws IOException;  
  1. public MemcachedClient(List<InetSocketAddress> addrs) throws IOException;  
  1. public MemcachedClient(ConnectionFactory cf, List<InetSocketAddress> addrs) throws IOException;  

其中最简单的构造函数就是第一个,可以直接传递一个InetSocketAddress,也可以是InetSocketAddress的数组。其实InetSocketAddress也是被转换成数组的。

比如:

 

  1. MemcachedClient cache = new MemcachedClient(new InetSocketAddress("127.0.0.1"11211));  



 

 

2. 常用方法

一般缓存数据的常用操作有:set(add+replace)、get、replace、add

 

 

  1. public Future<Boolean> set(String key, int exp, Object o)  


第一个参数:键(key)

 

第二个参数:过期时间(单位是秒)

第三个参数:要设置缓存中的对象(value),如果没有则插入,如果有则修改。

 

 

  1. public Object get(String key)  

 

第一个参数:键(key)

 

 

  1. public Future<Boolean> replace(String key, int exp, Object o)  


第一个参数:键(key)

 

第二个参数:过期时间(单位是秒)

第三个参数:该键的新值(new value),如果有则修改。

 

 

  1. public Future<Boolean> add(String key, int exp, Object o)  


第一个参数:键(key)

 

第二个参数:过期时间(单位是秒)

第三个参数:该键的值(value),如果没有则插入。

 

尊重知识,转载请注明本文来自:编程艺术家Poechant的CSDN博客 http://blog.csdn.net/poechant

猜你喜欢

转载自danwind.iteye.com/blog/2030666