redis 存储对象的方法对比
问题背景:
原来项目里面全部是直接redis存储对象的json数据,需要频繁的序列化和反序列化,后来考虑更换项目中的redis存储对象为hash对象存储的,但是获取后不能方便的set get操作,很是蛋疼,怎么才能解决这个问题呢?
1.1 直接存储对象的json
存放redis的时候,直接先用fastjson 或者 jackjson或者gson把对象序列化为json数据,然后用直接存放,key表示用户id或许和openid,value则是对象的json数据
1
2
3
4
5
6
7
8
9
10
|
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,对应各个字段的属性和值
存放单个字段
1
2
3
|
public void hset(string key, string field, string obj) { redistemplate.boundhashops(key).put(field,obj); } |
存放整个:
1
2
3
|
public void hsetmap(string key,map<object,object> map){ redistemplate.boundhashops(key).putall(map); } |
优点:存储方方便,节省内存空间,并且可以直接对单个字段修改,而不用获取整个对象,效率高
缺点:获取value后,是个map,不能方便的直接调用(set get)处理,需要手动map.get(filed)或者map.put(field,value)
1.3 如何解决redis hash存储对象的操作方便性问题
其实关于map和pojo的转换问题,网上给出了利用反射做的转换方法,但是加上了转换和反转,这和序列化和反序列化的问题一样了,效率问题,也不敢指直接用,纠结,思考再三,还是先维持代码不动了,以后考虑好了再说,或者广发网友有啥好解决方法,请多多指教哈!
redis存储对象的三种方式
一、 将对象序列化后保存到redis
序列化工具类实现
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
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实例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
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 = jedispool.getresource(); jedis.auth( "redis" ); //密码 return jedis; } catch (exception e) { e.printstacktrace(); return null ; } } } |
redis操作类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
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对象
1
2
3
4
5
6
7
8
9
10
11
12
13
|
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方法 } |
测试
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
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操作类
1
2
3
4
5
6
7
8
9
10
11
12
13
|
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); } } |
测试
1
2
3
4
5
6
|
@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操作类
1
2
3
4
5
6
7
8
9
10
11
12
13
|
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; } } |
测试
1
2
3
4
5
6
7
8
9
10
11
12
|
@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); } |
以上为个人经验,希望能给大家一个参考,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/afsvsv/article/details/78969541