需要JAR包:
commons-pool-1.5.6.jar
java_memcached-release_2.6.3.jar
slf4j-api-1.6.1.jar
slf4j-simple-1.6.1.jar
定义一个序列化的bean文件
/**
*
* @Description:
* @author wuyan
* @date 2016年1月28日 下午2:36:44
*/
public class QWBean implements java.io.Serializable{
private String name = null ;
public QWBean() {
// TODO Auto-generated constructor stub
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
public String toString(){
return "name:" + name;
}
}
通过对象方式访问
import java.io.IOException;
import com.danga.MemCached.MemCachedClient;
import com.danga.MemCached.SockIOPool;
/**
*
* @Description:
* @author wuyan
* @date 2016年1月28日 下午12:26:23
*/
public class MemcacheCache {
public static void main(String[] args) throws IOException {
// 服务器列表和其权重
String[] servers = { "127.0.0.1:1978" };
Integer[] weights = { 3 };
// 获取socke连接池的实例对象
SockIOPool pool = SockIOPool.getInstance();
// 设置服务器信息
pool.setServers(servers);
pool.setWeights(weights);
// 设置初始连接数、最小和最大连接数以及最大处理时间
pool.setInitConn(5);
pool.setMinConn(5);
pool.setMaxConn(250);
pool.setMaxIdle(1000 * 60 * 60 * 6);
// 设置主线程的睡眠时间
pool.setMaintSleep(30);
// 设置TCP的参数,连接超时等
pool.setNagle(false);
pool.setSocketTO(3000);
pool.setSocketConnectTO(0);
// 初始化连接池
pool.initialize();
MemCachedClient mcc = new MemCachedClient();
QWBean bean = new QWBean();
bean.setName("wuyan");
mcc.set("wuyan", bean);
QWBean value = (QWBean) mcc.get( "wuyan" );
<span style="white-space:pre"> </span>System.out.println(value.toString());
}
}
通过数组访问
<span style="font-size:12px;">MemCachedClient mcc = new MemCachedClient();
String[] info = {"wuyan","123"};
mcc.set("info", info);
String[] _info = (String[])mcc.get("info");
for(int i=0; i< _info.length;i++){
System.out.println(_info[i]);
}</span>
在此之前也尝试了读取值类弄为java.lang.String的方式,但是没有成功!!总觉得应该是ttserver配置的问题,错误原因尚未找到,先记录吧!!
MemCachedClient mcc = new MemCachedClient();
mcc.set("name", "wuyan");
System.out.println(mcc.get("name"));
<span style="color:#ff0000;">SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/Users/wuyan/java/workspace/bjapp/BjCSS/WebContent/WEB-INF/lib/slf4j-log4j12-1.7.5.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/Users/wuyan/java/apache-activemq-5.13.0/activemq-all-5.13.0.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.slf4j.impl.Log4jLoggerFactory]</span>
2016-01-28 14:49:39,127 ERROR (com.danga.MemCached.MemCachedClient:?) - ++++ exception thrown while trying to get object from cache for key: name
2016-01-28 14:49:39,132 ERROR (com.danga.MemCached.MemCachedClient:?) - invalid stream header: 77757961
java.io.StreamCorruptedException: invalid stream header: 77757961
at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:804)
at java.io.ObjectInputStream.<init>(ObjectInputStream.java:299)
at com.schooner.MemCached.ObjectTransCoder.decode(Unknown Source)
at com.schooner.MemCached.AscIIClient.get(Unknown Source)
at com.schooner.MemCached.AscIIClient.get(Unknown Source)
at com.schooner.MemCached.AscIIClient.get(Unknown Source)
at com.danga.MemCached.MemCachedClient.get(Unknown Source)
at com.aspire.bjcss.cmod.MemcacheCache.main(MemcacheCache.java:59)
null