redis set序列化存入缓存
Posted 小海追梦
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了redis set序列化存入缓存相关的知识,希望对你有一定的参考价值。
Room需要先实现序列化implements Serializable
ArrayList<Room> list=null;
roomList = getDaoRoomListData(filterParams, sort, order, skip, limit);
List<String> ridList = new ArrayList();
//
=========================================================
for (Room roomr : roomList) {
String strKeyrid=key+roomr.getRid();
Map<String, ?> parse = Document.parse(JSON.toJSONString(roomr));
helper.setRoomone(strKeyrid,roomr);//以一个实体存进缓存
}
Map redisMap=new HashMap();
helper.setRoomList(keys,roomList);//以列表存进缓存
===============缓存获取实体,经过序列化后就可以直接显示了=========================
if (roomListOne.size() > 0) {
Room room=roomListOne.get(0);
String roomid=room.getRid();
String keyroomid=key+roomid;
Room roomone=helper.getRoomone(keyroomid);
if(null!=roomone){
roomList.add(roomone);
flagRoomOne=false;
}
}
================获取列表,经过序列化后就可以直接显示了==================
roomList =helper.getRoomList(keys);
=========================================================
/**
* 从缓存获取数据
* @param uid
* @return List
*/
@SuppressWarnings("unchecked")
public Room getRoomone(String key) {
Room levellist = new Room();
Jedis jedis = null;
try {
jedis = JedisPoolUtil.getInstance().getCocosJedis();
byte[] bs = jedis.get(key.getBytes());
if(null != bs){
levellist = (Room) unserialize(bs);
}else{
return null ;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != jedis)
jedis.close();
}
return levellist;
}
/**
* 从缓存获取房间列表数据
* @param uid
* @return List
*/
@SuppressWarnings("unchecked")
public ArrayList<Room> getRoomList(String keys) {
ArrayList<Room> levellist = new ArrayList<Room>();
Jedis jedis = null;
try {
jedis = JedisPoolUtil.getInstance().getCocosJedis();
byte[] bs = jedis.get(keys.getBytes());
if(null != bs){
levellist = (ArrayList<Room>) unserialize(bs);
}else{
return null ;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != jedis)
jedis.close();
}
return levellist;
}
/**
* 保存房间信息到缓存
* @param uid
* @return List
*/
public void setRoomone(String key,Room room) {
Jedis jedis = null;
try {
jedis = JedisPoolUtil.getInstance().getCocosJedis();
jedis.set(key.getBytes(), serialize(room));
jedis.expire(key, 24*3600);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != jedis)
jedis.close();
}
}
/**
* 保存房间列表信息到缓存
* @param uid
* @return List
*/
public void setRoomList(String kes,ArrayList<Room> roomlist) {
Jedis jedis = null;
try {
jedis = JedisPoolUtil.getInstance().getCocosJedis();
jedis.set(kes.getBytes(), serialize(roomlist));
jedis.expire(kes, 24*3600);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != jedis)
jedis.close();
}
}
以上是关于redis set序列化存入缓存的主要内容,如果未能解决你的问题,请参考以下文章
Redis序列化和java存入Redis数据序列化反序列化总结