目录
- 一、 将对象序列化后保存到Redis
-
- 序列化工具类实现
- 获取jedis实例
- redis操作类
- User对象
- 测试
- 二、将对象用FastJSON转为JSON字符串后存储
-
- 三、将对象用Hash数据类型存储
-
一、 将对象序列化后保存到Redis
序列化工具类实现
public class SerializeUtil {
public static byte[] serizlize(Object object){
ObjectOutputStream oos = null;
ByteArrayOutputStream baos = null;
try {
baos = new ByteArrayOutputStream();
oos = new ObjectOutputStream(baos);
oos.writeObject(object);
byte[] bytes = baos.toByteArray();
return bytes;
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
if(baos != null){
baos.close();
}
if (oos != null) {
oos.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
return null;
}
public static Object deserialize(byte[] bytes){
ByteArrayInputStream bais = null;
ObjectInputStream ois = null;
try{
bais = new ByteArrayInputStream(bytes);
ois = new ObjectInputStream(bais);
return ois.readObject();
}catch(Exception e){
e.printStackTrace();
}finally {
try {
} catch (Exception e2) {
e2.printStackTrace();
}
}
return null;
}
}
获取jedis实例
public class RedisConnection {
private static String HOST = "127.0.0.1";
private static int PORT = 6379;
private static int MAX_ACTIVE = 1024;
private static int MAX_IDLE = 200;
private static int MAX_WAIT = 10000;
private static JedisPool jedisPool = null;
private static void initPool(){
try {
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(MAX_ACTIVE);
config.setMaxIdle(MAX_IDLE);
config.setMaxWaitMillis(MAX_WAIT);
jedisPool = new JedisPool(config, HOST, PORT);
} catch (Exception e) {
e.printStackTrace();
}
}
public synchronized static Jedis getJedis() {
try {
if(jedisPool == null){
initPool();
}
Jedis jedis = jedisPool.getResource();
jedis.auth("redis");
return jedis;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
redis操作类
public class RedisOps {
public static void set(String key,String value){
Jedis jedis = RedisConnection.getJedis();
jedis.set(key, value);
jedis.close();
}
public static String get(String key){
Jedis jedis = RedisConnection.getJedis();
String value = jedis.get(key);
jedis.close();
return value;
}
public static void setObject(String key,Object object){
Jedis jedis = RedisConnection.getJedis();
jedis.set(key.getBytes(), SerializeUtil.serizlize(object));
jedis.close();
}
public static Object getObject(String key){
Jedis jedis = RedisConnection.getJedis();
byte[] bytes = jedis.get(key.getBytes());
jedis.close();
return SerializeUtil.deserialize(bytes);
}
}
User对象
public class User implements Serializable{
private static final long serialVersionUID = -3210884885630038713L;
private int id;
private String name;
public User(){
}
public User(int id,String name){
this.id = id;
this.name = name;
}
}
测试
public class RedisTest {
@Test
public void testString(){
RedisOps.set("user:1", "sisu");
String user = RedisOps.get("user:1");
Assert.assertEquals("sisu", user);
}
@Test
public void testObject(){
RedisOps.setObject("user:2",new User(2,"lumia"));
User user = (User)RedisOps.getObject("user:2");
Assert.assertEquals("lumia", user.getName());
}
}
二、将对象用FastJSON转为JSON字符串后存储
redis操作类
public class RedisOps {
public static void setJsonString(String key,Object object){
Jedis jedis = RedisConnection.getJedis();
jedis.set(key, JSON.toJSONString(object));
jedis.close();
}
public static Object getJsonObject(String key,Class clazz){
Jedis jedis = RedisConnection.getJedis();
String value = jedis.get(key);
jedis.close();
return JSON.parseObject(value,clazz);
}
}
测试
@Test
public void testObject2(){
RedisOps.setJsonString("user:3", new User(3,"xiaoming"));
User user = (User)RedisOps.getJsonObject("user:3",User.class);
Assert.assertEquals("xiaoming", user.getName());
}
三、将对象用Hash数据类型存储
redis操作类
public class RedisOps {
public static void hSet(String key,String value){
Jedis jedis = RedisConnection.getJedis();
jedis.hSet(key, value);
jedis.close();
}
public static String hGet(String key){
Jedis jedis = RedisConnection.getJedis();
String value = jedis.hGet(key);
jedis.close();
return value;
}
}
测试
@Test
public void testObject3(){
RedisOps.hSet("user:3","id","3");
RedisOps.hSet("user:3","name","xiaoming");
String id = RedisOps..hGet("user:3","id");
String name = RedisOps.hGet("user:3","name");
Assert.assertEquals("3", id);
Assert.assertEquals("xiaoming", name);
}