1、相关类
package com.memcached.util;
import java.io.Serializable;
public class User implements Serializable{
private Integer id;
private String username;
private String password;
public User() {
super();
// TODO Auto-generated constructor stub
}
public User(Integer id, String username, String password) {
super();
this.id = id;
this.username = username;
this.password = password;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
package com.memcached.util;
import java.io.IOException;
import java.util.Properties;
import org.apache.log4j.Logger;
public class PropertyUtils {
private static Logger log = Logger.getLogger(MemcachedClientUtil.class);
public static Properties properties = new Properties();
/**
* 根据配置文件名和配置键名查询配置值
* @param key
* @param propertiesFileName
* @return
* @author wuliu
* @date 2015-6-5
*/
public static String getValue(String key,String propertiesFileName){
try {
properties.load(PropertyUtils.class.getClassLoader().getResourceAsStream(propertiesFileName));
} catch (IOException e) {
log.info(propertiesFileName + "文件没有找到......");
}
return properties.getProperty(key).trim();
}
}
package com.memcached.util;
import java.io.IOException;
import org.apache.log4j.Logger;
import org.omg.CosNaming.NamingContextExtPackage.AddressHelper;
import net.rubyeye.xmemcached.MemcachedClient;
import net.rubyeye.xmemcached.MemcachedClientBuilder;
import net.rubyeye.xmemcached.XMemcachedClient;
import net.rubyeye.xmemcached.XMemcachedClientBuilder;
import net.rubyeye.xmemcached.utils.AddrUtil;
/**
* memcached客户端工具类
* @author wuliu
* @date 2015-07-28
*/
public class MemcachedClientUtil {
private static Logger log = Logger.getLogger(MemcachedClientUtil.class);
/**
* memcached客户端
*/
private static MemcachedClient memcachedClient;
private static MemcachedClientBuilder builder;
/**
* 配置文件名
*/
private static String propertiesFileName;
/**
* memcached的服务
*/
private static String server;
private MemcachedClientUtil(){}
/**
* 初始化连接
*/
private static void init(){
propertiesFileName = "xmemcached.properties";
server = PropertyUtils.getValue("memcached.server", propertiesFileName) + ":" + PropertyUtils.getValue("memcached.port", propertiesFileName);
builder = new XMemcachedClientBuilder(AddrUtil.getAddresses(server), new int[]{5});
//设置连接池大小,即客户端个数
builder.setConnectionPoolSize(Integer.valueOf(PropertyUtils.getValue("memcached.connectionPoolSize", propertiesFileName)));
//宕机报警
// builder.setFailureMode(true);
//使用二进制文件
// builder.setCommandFactory(new BinaryCommandFactory());
}
/**
* 获取memcached客户端
* @return
* @author wuliu
* @date 2015-07-28
*/
public static MemcachedClient getMemcachedClient() {
try {
if(memcachedClient == null) {
init();
memcachedClient = builder.build();
// 装载数据时,关闭心跳检测
memcachedClient.setEnableHeartBeat(false);
}
} catch (IOException e) {
log.error("获取memcached客户端异常", e);
}
return memcachedClient;
}
/**
* 关闭memcachedClient客户端的连接
* @param mm MemcachedClient客户端
* @author wuliu
* @date 2015-07-28
*/
public static void closeMemcachedClient(MemcachedClient mm) {
if(mm != null) {
try {
mm.shutdown();
} catch (IOException e) {
log.error("memcachedClient客户端关闭异常", e);
}
}
}
}
2、xmemcached.properties配置文件
memcached.isOpen=true
memcached.expires=5
memcached.server=127.0.0.1
memcached.port=11211
memcached.connectionPoolSize=30
3、测试类
package com.memcached.util;
import java.util.HashMap;
import java.util.Map;
import net.rubyeye.xmemcached.Counter;
import net.rubyeye.xmemcached.MemcachedClient;
public class MemcachedDemo {
public static void main(String[] args) throws Exception {
// 获取memcached连接
MemcachedClient memcachedClient = MemcachedClientUtil.getMemcachedClient();
// memcachedClient.flushAll();
//设置指定键对应的值(过期时间单位秒),如果没有则添加,有则修改
// boolean set_username = memcachedClient.set("username", 0 , "admin0");
// System.out.println(set_username);
//设置指定键的新值,如果有则修改返回true(相同值也返回false);如果没有则不做操作返回false;
// boolean replace_username = memcachedClient.replace("username", 0, "admin2");
// System.out.println(replace_username);
//设置键的值,如果没有则插入返回true;如果有则不操作返回false;
// boolean add_username2 = memcachedClient.add("username2",0,"admin21");
// System.out.println(add_username2);
//删除指定键
// memcachedClient.delete("username");
//获取指定键对应的值
// String username = (String)memcachedClient.get("username");
// System.out.println(username);
// List<String> list = new ArrayList<String>();
// list.add("100");
// list.add("101");
// list.add("102");
// memcachedClient.set("m-list", 0, list);
// List<String> resultList = memcachedClient.get("m-list");
// for(String str : resultList){
// System.out.println(str);
// }
// Set<String> set = new HashSet<String>();
// set.add("1000");
// set.add("1001");
// set.add("1002");
// memcachedClient.set("m-set", 0, set);
//
// Set<String> resultSet = memcachedClient.get("m-set");
// Iterator<String> it = resultSet.iterator();
// while(it.hasNext()){
// System.out.println(it.next());
// }
// Map<String,Object> map = new HashMap<String, Object>();
// map.put("username", "admin");
// map.put("password", "123");
// memcachedClient.set("m-map", 0, map);
//
// Map<String,Object> resultMap = memcachedClient.get("m-map");
// for(Map.Entry<String, Object> entry : resultMap.entrySet()){
// System.out.println(entry.getKey()+"="+entry.getValue());
// }
// memcachedClient.set("m-user", 0, new User(1,"admin","123456"));
// User user = memcachedClient.get("m-user");
// if(user != null){
// System.out.println(user.getId()+","+user.getUsername()+","+user.getPassword());
// }
// 第一个参数指定递增的key名称, 第二个参数指定递增的幅度大小, 第三个参数指定当key不存在的情况下的初始值即设置一个新建为1。
//返回变化后的值
// long l = memcachedClient.incr("incrkey", 5, 1);
// System.out.println(l);
//两个参数的重载方法省略了第三个参数,默认指定为0。
// memcachedClient.incr("key1", 5);
// memcachedClient.decr("incrkey", 1);
// counter(memcachedClient);
MemcachedClientUtil.closeMemcachedClient(memcachedClient);
}
/**
* 计数器的操作
*
* @version 2016年10月31日上午11:00:21
* @author wuliu
* @param memcachedClient
* @throws Exception
*/
public static void counter(MemcachedClient memcachedClient) throws Exception{
Counter counter = memcachedClient.getCounter("counter", 0);
// long incr = counter.incrementAndGet();
// System.out.println(incr);
// long decr = counter.decrementAndGet();
// System.out.println(decr);
// long add = counter.addAndGet(-10);
}
}