public void setex(final byte[] key, final int seconds, final byte[] value) {
wRedisManager.request(new RedisCallback<String>() {
@Override
public String doInRequest(Jedis jedis) {
return jedis.setex(key, seconds, value);
}
});
}
private byte[] getByteArrayFromObject(Object o) throws Throwable {
ObjectOutputStream out = null;
try {
ByteArrayOutputStream bo = new ByteArrayOutputStream();
out = new ObjectOutputStream(bo);
out.writeObject(o);
out.flush();
return bo.toByteArray();
} finally {
if (out != null)
out.close();
}
}
private Object getObjectFromByteArray(byte[] bytes) throws Throwable {
ObjectInputStream in = null;
try {
ByteArrayInputStream bi = new ByteArrayInputStream(bytes);
in = new ObjectInputStream(bi);
return in.readObject();
} finally {
if (in != null)
in.close();
}
}
主要观察一下str.getByte()和对象序列化生成的byte[]数组有啥不同,在redis中用redis *来看看值有啥不同
private Object getRetFromCache(MethodInvocation invocation) throws Throwable {
Object retValue = null;
String cacheKey = getCacheKeyByAnno(invocation);
// byte[] cacheValue =
// redisService.get(getByteArrayFromObject(cacheKey));
byte[] cacheValue = redisService.get(cacheKey.getBytes());
if (cacheValue == null) {
retValue = invocation.proceed();
// redisService.setex(getByteArrayFromObject(cacheKey),
// invocation.getMethod().getAnnotation(ControllerCacheAnno.class).timeout(),
// getByteArrayFromObject(retValue));
redisService.setex(cacheKey.getBytes(), invocation.getMethod().getAnnotation(ControllerCacheAnno.class)
.timeout(), getByteArrayFromObject(retValue));
} else {
retValue = getObjectFromByteArray(cacheValue);
}
return retValue;
}