jedis 保存自定义List和Object对象
来源:互联网 发布:windows 7 下载 编辑:程序博客网 时间:2024/06/04 19:26
1.新建抽象类,jdk1.8同样可以创建接口.
public abstract class SerializeTranscoder {
protected static Logger logger = Logger.getLogger(SerializeTranscoder.class);
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) {
logger.info("Unable to close " + closeable, e);
}
}
}
}
2.创建list的实现类
public class ListTranscoder<M extends Serializable> extends SerializeTranscoder {
@SuppressWarnings("unchecked")
public List<M> deserialize(byte[] in) {
List<M> 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) {
LoggerUtils.error(logger, String.format("Caught IOException decoding %d bytes of data",
in == null ? 0 : in.length) + e);
} catch (ClassNotFoundException e) {
LoggerUtils.error(logger, String.format("Caught CNFE decoding %d bytes of data",
in == null ? 0 : in.length) + e);
} 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<M> values = (List<M>) 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);
os.close();
bos.close();
results = bos.toByteArray();
} catch (IOException e) {
throw new IllegalArgumentException("Non-serializable object", e);
} finally {
close(os);
close(bos);
}
return results;
}
}
3.创建Object对象的类
public class ObjectsTranscoder<M extends Serializable> 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) {
LoggerUtils.error(logger, String.format("Caught IOException decoding %d bytes of data",
in == null ? 0 : in.length) + e);
} catch (ClassNotFoundException e) {
LoggerUtils.error(logger, String.format("Caught CNFE decoding %d bytes of data",
in == null ? 0 : in.length) + e);
} finally {
close(is);
close(bis);
}
return result;
}
}
jedis就可以直接操作list和Object对象.
public void setObjectList(String key,List<M> value,int cacheSeconds){
Jedis jedis=getResource();
if(jedis.exists(key)){
jedis.del(key);
}
ListTranscoder coder=new ListTranscoder();
jedis.set(key.getBytes(),coder.serialize(value));
if(cacheSeconds){//缓存时间
jedis.expire(key,cacheSeconds);
}
}
- jedis 保存自定义List和Object对象
- redis Jedis存取list对象和map
- redis jedis存储对象简单操作,map list 自定义对象
- ShareParefence保存Object对象
- localStorage 保存对象Object
- Redis缓存Object,List对象 - Java 对象和List的序列化和反序列化
- NSUserDefaults 保存自定义对象
- NSUserDefaults保存自定义对象
- ios保存自定义对象
- Java通用对象池GenericObjectPool<T>原理总结和自定义的Jedis管理池例子
- SharedPreferences保存List和对象序列化数据
- List<T>和List<Object[]>
- Redis缓存Object,List对象
- 利用SharedPreference 保存List<Object>集合
- 自定义对象List数组
- 【redis,1】java操作redis: 将string、list、map、自定义的对象保存到redis中
- java操作redis: 将string、list、map、自定义的对象保存到redis中
- java操作redis: 将string、list、map、自定义的对象保存到redis中
- 再谈Android动态链接库
- ubuntu安装mysql
- 用Activity实现对话框效果
- isFinite方法详解
- java设计模式19_备忘录模式
- jedis 保存自定义List和Object对象
- bzoj1748 [Usaco2005 open]Around the world 环球飞行
- 566. Reshape the Matrix/624. Maximum Distance in Arrays/594. Longest Harmonious Subsequence
- 共享经济模式对户外广告行业的启发
- 使用Xshell&Xftp实现远程登录及文件传输
- Klein3000&绞车一次故障排除
- Git—学习与使用
- 如何计算时间复杂度
- ACdream 1064 完美数(数位dp)