redis java对象list_redis整合spring示例二—java操作redis(存对象及List)
在java操作redis中,咱们已经有了基本的java操作redis相关代码。下面继续
redis存放对象和List可通过对它们先序列化,然后存到redis中。
对象序列化及反序列化工具类:
package com._656463.redis;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
/**
* Jedis does not support cache the Object directly, the Objects needed to be
* serialized and de-serialized
*/
public class ObjectTranscoder extends
SerializeTranscoder {
@SuppressWarnings("unchecked")
@Override
public byte[] serialize(Object value) {
if (value == null) {
throw new NullPointerException("Can't serialize null");
}
byte[] result = null;
ByteArrayOutputStream bos = null;
ObjectOutputStream os = null;
try {
bos = new ByteArrayOutputStream();
os = new ObjectOutputStream(bos);
M m = (M) value;
os.writeObject(m);
os.close();
bos.close();
result = bos.toByteArray();
} catch (IOException e) {
throw new IllegalArgumentException("Non-serializable object", e);
} finally {
close(os);
close(bos);
}
return result;
}
@SuppressWarnings("unchecked")
@Override
public M deserialize(byte[] in) {
M result = null;
ByteArrayInputStream bis = null;
ObjectInputStream is = null;
try {
if (in != null) {
bis = new ByteArrayInputStream(in);
is = new ObjectInputStream(bis);
result = (M) is.readObject();
is.close();
bis.close();
}
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
close(is);
close(bis);
}
return result;
}
}
List序列化和反序列化工具类:
package com._656463.redis;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
/**
* Jedis does not support cache the Object directly, the Objects needed to be
* serialized and de-serialized
*
* @作者 : huangyineng
* @日期 : 2015-6-11 下午5:55:21
*/
public class ListTranscoder extends SerializeTranscoder {
@SuppressWarnings("unchecked")
public List deserialize(byte[] in) {
List list = new ArrayList();
ByteArrayInputStream bis = null;
ObjectInputStream is = null;
try {
if (in != null) {
bis = new ByteArrayInputStream(in);
is = new ObjectInputStream(bis);
while (true) {
M m = (M) is.readObject();
if (m == null) {
break;
}
list.add(m);
}
is.close();
bis.close();
}
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
close(is);
close(bis);
}
return list;
}
@SuppressWarnings("unchecked")
@Override
public byte[] serialize(Object value) {
if (value == null) {
throw new NullPointerException("Can't serialize null");
}
List values = (List) value;
byte[] results = null;
ByteArrayOutputStream bos = null;
ObjectOutputStream os = null;
try {
bos = new ByteArrayOutputStream();
os = new ObjectOutputStream(bos);
for (M m : values) {
os.writeObject(m);
}
os.writeObject(null);//不加会报java.io.EOFException异常
os.close();
bos.close();
results = bos.toByteArray();
} catch (IOException e) {
throw new IllegalArgumentException("Non-serializable object", e);
} finally {
close(os);
close(bos);
}
return results;
}
}
抽象基类
package com._656463.redis;
import java.io.Closeable;
public abstract class SerializeTranscoder {
public abstract byte[] serialize(Object value);
public abstract Object deserialize(byte[] in);
public void close(Closeable closeable) {
if (closeable != null) {
try {
closeable.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
看在代码中怎么调用。这可能本站使用的代码
@Resource(name = "redisClientTemplate")
private RedisClientTemplate redisClient;
public List findAllChannels(){
List results = null;
ListTranscoder listTranscoder = new ListTranscoder();
byte[] channels = redisClient.get(Constants.CHANNELS_REDIES_KEY);
if(channels!=null){
results = listTranscoder.deserialize(channels);
}
if(results==null || results.size()<=0){
Map params = new HashMap();
params.put("start", 0);
params.put("size",Integer.MAX_VALUE);
results = this.findScrollDataList(params);
//加入redis
channels = listTranscoder.serialize(results);
redisClient.set(Constants.CHANNELS_REDIES_KEY, channels);
redisClient.setExpire(Constants.CHANNELS_REDIES_KEY, Constants.expireTime);
}
return results;
}
完毕
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐



所有评论(0)