redis sadd存缓存
以sadd序列化后存进缓存:
for (Room room : roomList) {
String strrid=room.getRid();//获取房间id
Map parse = Document.parse(JSON.toJSONString(room));//转map<String,String>
helper.saveRoomInfo(strrid,parse);//以实体存进缓存
ridList.add(strrid);
}
Map redisMap=new HashMap();
redisMap.put("roomlist", JSON.toJSON(roomList));
helper.sadd(key,ridList.toArray(new String[ridList.size()]));//以列表形式存进缓存,不需要经过序列化
//获取单个实体
Room room=roomList.get(0);
String roomid=room.getRid();
Map oneRoommap=getRoomOnly(roomid);
if(oneRoommap.size()>0){
list.add(oneRoommap);
result=new Document("roomlist",JSON.toJSON(list));
flagRoomOne=false;
}
//经过序列化存进去后获取出来后需要反序列化
public Map getRoomOnly(String rid){
RedisHelper helper = RedisHelper.getInstance();
Map<byte[], byte[]> rmap =helper.getRoomInfo(rid);
Map returnmap=new HashMap();
for (Entry<byte[], byte[]> entry : rmap.entrySet()) {
Room levelben=new Room();
Map retmap=new HashMap();
byte[] keylbyte= entry.getKey();
byte[] valuebyte = entry.getValue();
Object objkey= null == keylbyte ? "0" : unserialize(keylbyte);
Object objvalue = null == valuebyte ? "0" : unserialize(valuebyte);
returnmap.put(objkey, objvalue.toString());
}
return returnmap;
}
==================先从set中获取房间id后循环取出房间数据===========================
Set<String> reumap=helper.getValue(key);//获取一sadd存进缓存的数据
//判断缓存是否有资料
if(null != reumap && !reumap.isEmpty()){
Iterator<String> it=reumap.iterator();
while(it.hasNext()){
Map returnmap=new HashMap();
String roomid=it.next();
returnmap=getRoomOnly(roomid);
Map<byte[], byte[]> rmap =helper.getRoomInfo(it.next());//通过房间id获取缓存房间信息
Map returnmap=new HashMap();
for (Entry<byte[], byte[]> entry : rmap.entrySet()) {//分别循环房间序列化的信息来反序列化
Room levelben=new Room();
Map retmap=new HashMap();
byte[] keylbyte= entry.getKey();
byte[] valuebyte = entry.getValue();
Object objkey= null == keylbyte ? "0" : unserialize(keylbyte);//获取的key值
Object objvalue = null == valuebyte ? "0" : unserialize(valuebyte);//获取value值
returnmap.put(objkey, objvalue.toString());//添加map里面所有的元素进去
}
list.add(returnmap);//将map添加到列表中去
}
order(list);//list排序
result=new Document("roomlist",JSON.toJSON(list));
}
==============================================
/**
* 从set 中获取数据
*
* @param key
* @param members
* @return
*/
public Set<String> getValue(String key) {
Jedis jedis = null;
try {
jedis = JedisPoolUtil.getInstance().getCocosJedis();
return jedis.smembers(key);
} catch (Exception e) {
} finally {
if (null != jedis)
jedis.close();
}
return new HashSet<String>();
}
======================================================================
//操作redis
/**
* 从缓存获取房间信息
* @param key
* 带key的用户数据
* @param value
*/
public Map<byte[], byte[]> getRoomInfo(String rid) {
Jedis jedis = null;
try {
jedis = JedisPoolUtil.getInstance().getCocosJedis();
String headkey=COCO_ROOM_RID_KEY+rid;
// Map<String ,?> map= jedis.hgetAll(headkey);
// return (Map<String, Object>)map;
Map<byte[], byte[]> hgetAll = jedis.hgetAll(headkey.getBytes());
return hgetAll;
} catch (Exception e) {
} finally {
if (null != jedis)
jedis.close();
}
return new TreeMap<byte[], byte[]>();
}
================================================================================================
/**
* 添加数据到set中
*
* @param key
* @param members
* @return
*/
public void sadd(String key, String... members) {
Jedis jedis = null;
try {
jedis = JedisPoolUtil.getInstance().getCocosJedis();
jedis.sadd(key, members);
} catch (Exception e) {
} finally {
if (null != jedis)
jedis.close();
}
}
================================================================================================
/**
* 将房间信息保存到缓存
*
* @param key
* 带key的房间数据
* @param value
*/
public void saveRoomInfo(String roomid,Map<String,String> hash) {
Jedis jedis = null;
try {
jedis = JedisPoolUtil.getInstance().getCocosJedis();
String headkey=COCO_ROOM_RID_KEY+roomid;
byte[] rawKey = headkey.getBytes();
Map<String, String> map=new HashMap<String,String>(hash);
final Map<byte[], byte[]> hashes = new LinkedHashMap<byte[], byte[]>(map.size());
for (Entry<String, String> entry : map.entrySet()) {
hashes.put(serialize(entry.getKey()), serialize(entry.getValue()));
}
jedis.hmset(rawKey, hashes);
} catch (Exception e) {
} finally {
if (null != jedis)
jedis.close();
}
}
================================================================================================