memcached—向memcached中保存Java实体需注意的问题

         今天以代码实例的形式总结一下向memcached中保存Java实体需注意的问题:

         memcached工具类代码:

package com.ghj.packageoftool;

import java.util.Date;

import com.danga.MemCached.MemCachedClient;
import com.danga.MemCached.SockIOPool;

/**
 * Memcached工具类
 * 
 * @author GaoHuanjie
 */
public class MemcachedUtils {

	private static MemCachedClient memCachedClient;
	static {
         /************************************配置Memcached**************************************/
         SockIOPool sockIOPool = SockIOPool.getInstance();

         sockIOPool.setServers(new String[]{"127.0.0.1:11211"});//设置memcachedserver地址
         sockIOPool.setWeights(new Integer[]{3}); 				//设置每一个MemCachedserver权重 
         sockIOPool.setFailover(true);  						//当一个memcachedserver失效的时候是否去连接还有一个memcachedserver.
         sockIOPool.setInitConn(10);    						//初始化时对每一个server建立的连接数目
         sockIOPool.setMinConn(10);     						//每一个server建立最小的连接数
         sockIOPool.setMaxConn(100);    						//每一个server建立最大的连接数
         sockIOPool.setMaintSleep(30);  						//自查线程周期进行工作,其每次休眠时间
         sockIOPool.setNagle(false);    						//Socket的參数,假设是true在写数据时不缓冲,马上发送出去。Tcp的规则是在发送一个包之前,包的发送方会等待远程接收方确认已收到上一次发送过来的包;这种方法就能够关闭套接字的缓存——包准备马上发出。

sockIOPool.setSocketTO(3000); //Socket堵塞读取数据的超时时间 sockIOPool.setAliveCheck(true); //设置是否检查memcachedserver是否失效 sockIOPool.setMaxIdle(1000*30*30); // 设置最大处理时间 sockIOPool.setSocketConnectTO(0); //连接建立时对超时的控制 sockIOPool.initialize(); // 初始化连接池 if (memCachedClient == null){ memCachedClient = new MemCachedClient(); } } private MemcachedUtils() { } /** * 向缓存加入键值对并为该键值对设定逾期时间(即多长时间后该键值对从Memcached内存缓存中删除,比方: new Date(1000*10),则表示十秒之后从Memcached内存缓存中删除)。 * * @author GaoHuanjie */ public static boolean add(String key, Object value, Date expire) { try { return memCachedClient.add(key, value, expire); } catch (Exception e) { e.printStackTrace(); return false; } } /** * 依据键获取Memcached内存缓存管理系统中对应的值 * * @author GaoHuanjie */ public static Object get(String key) { try { return memCachedClient.get(key); } catch (Exception e) { e.printStackTrace(); return null; } } }

         測试main方法所在类代码:
package com.ghj.packageofclient;

import java.util.Date;

import com.ghj.packageoftool.MemcachedUtils;
import com.ghj.packageofvo.User;

public class Client{
	
	public static void main(String[] args) {
		MemcachedUtils.add("user", new User("liunannan", "liunannan@jd"), new Date(1000*60));//向Memcached中加入一个序列化的对象
		User user = (User)(MemcachedUtils.get("user"));
		System.err.println("username:"+user.getUserName() + ",密码:" + user.getPassword());
	}
}
         Java实体代码:
package com.ghj.packageofvo;

/**
 * 用户业务bean
 * 
 * @author 高焕杰
 */
public class User{

	private String userName;
	private String password;

	public User(String userName, String password) {
		super();
		this.userName = userName;
		this.password = password;
	}
	
	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;
	}
}
        执行main方法,你将看到例如以下异常:

         稍有Java基础的人一看便知:之所以出现这个问题全然是因为Java实体User类没有实例化造成的,所以User类应该改成例如以下代码:

package com.ghj.packageofvo;

import java.io.Serializable;

/**
 * 用户业务bean
 * 
 * @author 高焕杰
 */
public class User implements Serializable{

	private static final long serialVersionUID = -3371451210123762490L;

	private String userName;
	private String password;

	public User(String userName, String password) {
		super();
		this.userName = userName;
		this.password = password;
	}
	
	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;
	}
}
        【0分下载project代码

原文地址:https://www.cnblogs.com/gcczhongduan/p/5208153.html