memcached问题
java_memcached 本身的api add的数据,get也可以读取数据没问题。用memadmin也可以查询数据。
用memadmin录入数据,memadmin展示也没有问题。但用java_memcached客户端无法获取数据?为什么?
80 [main] ERROR com.danga.MemCached.MemCachedClient - ++++ exception thrown while trying to
get object from cache for key: t
80 [main] ERROR com.danga.MemCached.MemCachedClient - invalid stream header: 74747474
java.io.StreamCorruptedException: invalid stream header: 74747474
at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:782)
at java.io.ObjectInputStream.<init>(ObjectInputStream.java:279)
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.sloth.memcached.MemcachedClient.get(MemcachedClient.java:35)
at com.sloth.memcached.Test.<init>(Test.java:19)
at com.sloth.memcached.Test.main(Test.java:26)
[解决办法]
大家用缓存很少?
[解决办法]
数据放到不同的slabs中不能用get统一读取?
[解决办法]
无源码无真相,不过本人还是觉得 你MemCachedClient 的初始化配置有关
[解决办法]
核心初始化
private MemcachedClientFactory() {
String[] servers = MemcachedClientProperty.MemcachedServers.split(",");
Integer[] weights = null;
String weightsCfg = MemcachedClientProperty.MemcachedWeights;
if(weightsCfg != null){
String[] wcfg = weightsCfg.split(",");
weights = new Integer[wcfg.length];
for (int i = 0; i < weights.length; i++) {
weights[i] = Integer.valueOf(wcfg[i]);
}
}else{
weights = new Integer[servers.length];
for (int i = 0; i < weights.length; i++) {
weights[i] = 1;
}
}
// 获取socke连接池的实例对象
SockIOPool pool = SockIOPool.getInstance();
// 服务器列表和其权重
pool.setServers(servers);
pool.setWeights(weights);
pool.setHashingAlg(SockIOPool.CONSISTENT_HASH);
// 设置初始连接数、最小和最大连接数以及最大处理时间
pool.setInitConn(MemcachedClientProperty.MemcachedInitConn);
pool.setMinConn(MemcachedClientProperty.MemcachedMinConn);
pool.setMaxConn(MemcachedClientProperty.MemcachedMaxConn);
pool.setMaxIdle(1000 * 60 * 60 * 6);
// 设置主线程的睡眠时间
pool.setMaintSleep(30);
// 设置TCP的参数,连接超时等
pool.setNagle(false);
pool.setSocketTO(3000);
pool.setSocketConnectTO(0);
// 初始化连接池
pool.initialize();
}
我觉得初始化没问题
测试代码。
System.out.println(mem.add("MemcachedClientService", MemcachedClientService.class.toString())); 重复添加返回false
System.out.println(mem.add("TEST", "asdfghjkl"));重复添加返回false
System.out.println(mem.get("MemcachedClientService"));正常返回结果,在memadmin中可以查看分步在slab3中
System.out.println(mem.get("TEST"));正常返回结果,在memadmin中可以查看分步在slab1中
System.out.println(mem.get("t"));失败,在memadmin中可以查看分步在slab1中,手工在memadmin添加。
[解决办法]
核心初始化
private MemcachedClientFactory() {
String[] servers = MemcachedClientProperty.MemcachedServers.split(",");
Integer[] weights = null;
String weightsCfg = MemcachedClientProperty.MemcachedWeights;
if(weightsCfg != null){
String[] wcfg = weightsCfg.split(",");
weights = new Integer[wcfg.length];
for (int i = 0; i < weights.length; i++) {
weights[i] = Integer.valueOf(wcfg[i]);
}
}else{
weights = new Integer[servers.length];
for (int i = 0; i < weights.length; i++) {
weights[i] = 1;
}
}
// 获取socke连接池的实例对象
SockIOPool pool = SockIOPool.getInstance();
// 服务器列表和其权重
pool.setServers(servers);
pool.setWeights(weights);
pool.setHashingAlg(SockIOPool.CONSISTENT_HASH);
// 设置初始连接数、最小和最大连接数以及最大处理时间
pool.setInitConn(MemcachedClientProperty.MemcachedInitConn);
pool.setMinConn(MemcachedClientProperty.MemcachedMinConn);
pool.setMaxConn(MemcachedClientProperty.MemcachedMaxConn);
pool.setMaxIdle(1000 * 60 * 60 * 6);
// 设置主线程的睡眠时间
pool.setMaintSleep(30);
// 设置TCP的参数,连接超时等
pool.setNagle(false);
pool.setSocketTO(3000);
pool.setSocketConnectTO(0);
// 初始化连接池
pool.initialize();
}
我觉得初始化没问题
测试代码。
System.out.println(mem.add("MemcachedClientService", MemcachedClientService.class.toString())); 重复添加返回false
System.out.println(mem.add("TEST", "asdfghjkl"));重复添加返回false
System.out.println(mem.get("MemcachedClientService"));正常返回结果,在memadmin中可以查看分步在slab3中
System.out.println(mem.get("TEST"));正常返回结果,在memadmin中可以查看分步在slab1中
System.out.println(mem.get("t"));失败,在memadmin中可以查看分步在slab1中,手工在memadmin添加。
[解决办法]
今天依然没人???
[解决办法]
public class MemCachedEngine {
private MemCachedEngine(){}
private static MemCachedEngine instance = new MemCachedEngine();
public static MemCachedEngine getInstance() {
return instance;
}
public MemCachedClient mcc=null;
public MemCachedClient getMcc() {
if(mcc==null){
mcc = new MemCachedClient("mem1");
String[] servers = "mem1cache:11211";
SockIOPool pool = SockIOPool.getInstance("mem1");
/**服务器列??*/
pool.setServers(servers);
/**服务器权??*/
pool.setWeights(new Integer[]{5});
/**初始连接?? ??小最大连接数 */
pool.setInitConn(10);
pool.setMinConn(5);
pool.setMaxConn(100);
pool.setMaxIdle(3600000);
/**主线程的睡眠时间*/
pool.setMaintSleep(3000);
pool.setNagle(false);
/**连接超时*/
pool.setSocketTO(3000);
/**连接超时*/
pool.setSocketConnectTO(0);
/***/
pool.setFailover(true);
/***/
pool.setAliveCheck(true);
/**初始化连接池*/
pool.initialize();
}
/**压缩设置*/
mcc.setCompressEnable(true);
/**超过指定大小(K)的数据会被压缩**/
mcc.setCompressThreshold(1024);
/**java默认的类型没有实现序列化,可以通过这个设置????*/
mcc.setPrimitiveAsString( true );
/**是否过滤key的编码*/
mcc.setSanitizeKeys( false );
return mcc;
}
public void setMcc(MemCachedClient mcc) {
this.mcc = mcc;
}
}希望对你有帮助
[解决办法]
看来是初始化有点问题。还未找到原因。。。
python的客户端没有问题。。。。
[解决办法]
总结这一现象java_memcached不能get其他组件add的数据?
不知道其他人也遇到类似的问题不?
[解决办法]
事实上python客户端也存在这个问题。。
mc = memcache.Client(['192.168.0.100:11211'],debug=0)
mc.set("foo","bar")
value = mc.get("TEST") get foot 没有问题 TEST 是java组件 add的
print value
也就是说memcache get数据必须统一组件?