redis存储对象的⽅法对⽐分析
redis 存储对象的⽅法对⽐
问题背景:
原来项⽬⾥⾯全部是直接redis存储对象的json数据,需要频繁的序列化和反序列化,后来考虑更换项⽬中的redis存储对象为hash对象存储的,但是获取后不能⽅便的set get操作,很是蛋疼,怎么才能解决这个问题呢?
1.1 直接存储对象的json
存放redis的时候,直接先⽤fastJson 或者 jackJson或者Gson把对象序列化为json数据,然后⽤直接存放,key表⽰⽤户id或许和openid,value则是对象的json数据
public String get(String key) {
Object value = redisTemplate.boundValueOps(key).get();
return (String) value;
}
public void set(String key, String json) {
if (json == null) {
return;
}
redisTemplate.boundValueOps(key).set(json);
}
优点:虽然需要序列化和反序列化,但是可以直接操作对象的⽅法,⽅便快捷
缺点:需要序列化和反序列化,并且修改单个字段,需要获取整个json,修改后,序列化保存,浪费空间,浪费时间,效率低1.2 采⽤redis hash key field value 存储
key代表主键,⽐如⽤户id,或者openId,value是⼀个map,对应各个字段的属性和值
存放单个字段
public void hset(String key, String field, String obj) {
redisTemplate.boundHashOps(key).put(field,obj);
}
存放整个:
public void hSetMap(String key,Map<Object,Object> map){
redisTemplate.boundHashOps(key).putAll(map);fastjson常用方法
}
优点:存储⽅⽅便,节省内存空间,并且可以直接对单个字段修改,⽽不⽤获取整个对象,效率⾼
缺点:获取value后,是个map,不能⽅便的直接调⽤(set get)处理,需要⼿动(filed)或者map.put(field,value)
1.3 如何解决redis hash存储对象的操作⽅便性问题
其实关于map和pojo的转换问题,⽹上给出了利⽤反射做的转换⽅法,但是加上了转换和反转,这和序列化和反序列化的问题⼀样了,效率问题,也不敢指直接⽤,纠结,思考再三,还是先维持代码不动了,以后考虑好了再说,或者⼴发⽹友有啥好解决⽅法,请多多指教哈!
Redis存储对象的三种⽅式
⼀、将对象序列化后保存到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 = ByteArray();
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);
adObject();
}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;
/*
* 初始化redis连接池
* */
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();
}
}
/*
* 获取jedis实例
* */
public synchronized static Jedis getJedis() {
try {
if(jedisPool == null){
initPool();
}
Jedis jedis = Resource();
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 = Jedis();
jedis.set(key, value);
jedis.close();
}
public static String get(String key){
Jedis jedis = Jedis();
String value = (key);
jedis.close();
return value;
}
public static void setObject(String key,Object object){
Jedis jedis = Jedis();
jedis.Bytes(), SerializeUtil.serizlize(object));
jedis.close();
}
public static Object getObject(String key){
Jedis jedis = Jedis();
byte[] bytes = (Bytes());
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;
}
//setter和getter⽅法
}
测试
public class RedisTest {
@Test
public void testString(){
RedisOps.set("user:1", "sisu");
String user = ("user:1");
Assert.assertEquals("sisu", user);
}
@Test
public void testObject(){
RedisOps.setObject("user:2",new User(2,"lumia"));
User user = (Object("user:2");
Assert.assertEquals("lumia", Name());
}
}
⼆、将对象⽤FastJSON转为JSON字符串后存储redis操作类
public class RedisOps {
public static void setJsonString(String key,Object object){
Jedis jedis = Jedis();
jedis.set(key, JSONString(object));
jedis.close();
}
public static Object getJsonObject(String key,Class clazz){
Jedis jedis = Jedis();
String value = (key);
jedis.close();
return JSON.parseObject(value,clazz);
}
}
测试
@Test
public void testObject2(){
RedisOps.setJsonString("user:3", new User(3,"xiaoming"));
User user = (JsonObject("user:3",User.class);
Assert.assertEquals("xiaoming", Name());
}
三、将对象⽤Hash数据类型存储
redis操作类
public class RedisOps {
public static void hSet(String key,String value){
Jedis jedis = Jedis();
jedis.hSet(key, value);
jedis.close();
}
public static String hGet(String key){
Jedis jedis = Jedis();
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);
}
以上为个⼈经验,希望能给⼤家⼀个参考,也希望⼤家多多⽀持。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论