【redis对象,集合序列化Demo】

package org.seckill.dao.cache;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.List;

import org.seckill.entity.Seckill;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import com.dyuproject.protostuff.LinkedBuffer;
import com.dyuproject.protostuff.ProtostuffIOUtil;
import com.dyuproject.protostuff.runtime.RuntimeSchema;

public class RedisDao2 {
private Logger logger = LoggerFactory.getLogger(this.getClass());
private final JedisPool jedisPool;
private RuntimeSchema<Seckill> schema = RuntimeSchema.createFrom(Seckill.class);//自定义schema
RedisDao2(String ip, int port){
jedisPool = new JedisPool(ip, port);
}
public Seckill getSeckill(long seckillId) {
try {
Jedis jedis = jedisPool.getResource();
try {
String key = "" + seckillId;
byte[] bs = jedis.get(key.getBytes());
if (bs != null) {
Seckill seckill = schema.newMessage();
ProtostuffIOUtil.mergeFrom(bs,seckill,schema);//反序列化
return seckill;
}
} finally{
jedis.close();
}
} catch (Exception e) {
logger.error(e.getMessage(),e);
}
return null;
}
//对象序列化:
public String putSeckill(Seckill seckill) {
try {
Jedis jedis = jedisPool.getResource();
try {
String key = ""+ seckill.getSeckillId();
byte[] bytes = ProtostuffIOUtil.toByteArray(seckill, schema,LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE)/*缓存器*/);// 序列化到二进制数组
int timeOut = 60 * 60 ;// 单位是s:缓存1h
String result = jedis.setex(key.getBytes(), timeOut , bytes);
return result;
} finally{
jedis.close();
}
} catch (Exception e) {
logger.error(e.getMessage(),e);
}
return null;
}
public Seckill getSeckillAll() {
RuntimeSchema<Seckill> schema = (RuntimeSchema<Seckill>) RuntimeSchema.getSchema(Seckill.class);
try {
Jedis jedis = jedisPool.getResource();
try {
String key = "seckillList";
byte[] bs = jedis.get(key.getBytes());
if (bs != null) {
Seckill seckill = schema.newMessage();
ProtostuffIOUtil.mergeFrom(bs,seckill,schema);//反序列化
return seckill;
}
} finally{
jedis.close();
}
} catch (Exception e) {
logger.error(e.getMessage(),e);
}
return null;
}
//集合序列化:
private <T> byte[] serializeList(List<T> objList) {
if (objList == null || objList.isEmpty()) {
throw new RuntimeException("序列化对象列表(" + objList + ")参数异常!");
}
@SuppressWarnings("unchecked")
RuntimeSchema<T> schema = (RuntimeSchema<T>) RuntimeSchema.getSchema(objList.get(0).getClass());
LinkedBuffer buffer = LinkedBuffer.allocate(1024 * 1024);
byte[] protostuff = null;
ByteArrayOutputStream bos = null;
try {
bos = new ByteArrayOutputStream();
ProtostuffIOUtil.writeListTo(bos, objList, schema, buffer);
protostuff = bos.toByteArray();
} catch (Exception e) {
throw new RuntimeException("序列化对象列表(" + objList + ")发生异常!", e);
} finally {
buffer.clear();
try {
if(bos!=null){
bos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}

return protostuff;
}
private <T> List<T> deserializeList(byte[] paramArrayOfByte, Class<T> targetClass) {
if (paramArrayOfByte == null || paramArrayOfByte.length == 0) {
throw new RuntimeException("反序列化对象发生异常,byte序列为空!");
}

RuntimeSchema<T> schema = (RuntimeSchema<T>) RuntimeSchema.getSchema(targetClass);
List<T> result = null;
try {
result = ProtostuffIOUtil.parseListFrom(new ByteArrayInputStream(paramArrayOfByte), schema);
} catch (IOException e) {
throw new RuntimeException("反序列化对象列表发生异常!",e);
}
return result;
}

//集合反序列化:
public <T> String putSeckillAll(List<T> objList) {
try {
Jedis jedis = jedisPool.getResource();
try {
String key = "seckillAllList:001";
int timeOut = 60 * 60 ;// 单位是s:缓存1h
byte[] serializeList = serializeList(objList);
String result = jedis.setex(key.getBytes(), timeOut , serializeList);
return result;
} finally{
jedis.close();
}
} catch (Exception e) {
logger.error(e.getMessage(),e);
}

return null;

//byte[] serializeList = serializeList(objList);

}
//集合序列化:
public <T> List<T> getSeckillAllList(Class<T> targetClass) {
try {
Jedis jedis = jedisPool.getResource();
try {
String key = "seckillAllList:001";
byte[] bs = jedis.get(key.getBytes());
if (bs != null) {
@SuppressWarnings("unchecked")
List<T> deserializeList = (List<T>) deserializeList(bs,targetClass);
return deserializeList;
}
} finally{
jedis.close();
}
} catch (Exception e) {
logger.error(e.getMessage(),e);
}
return null;
}
}

原文地址:https://www.cnblogs.com/yangjian-java/p/6700938.html