码迷,mamicode.com
首页 > 其他好文 > 详细

redis set、序列化存入缓存

时间:2016-06-02 22:00:54      阅读:180      评论:0      收藏:0      [点我收藏+]

标签:

 

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、序列化存入缓存

标签:

原文地址:http://www.cnblogs.com/xiaohaizhuimeng/p/5554399.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!