您好,登錄后才能下訂單哦!
本篇內(nèi)容主要講解“redis存儲(chǔ)對(duì)象方法的比較”,感興趣的朋友不妨來(lái)看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來(lái)帶大家學(xué)習(xí)“redis存儲(chǔ)對(duì)象方法的比較”吧!
問(wèn)題背景:
原來(lái)項(xiàng)目里面全部是直接redis存儲(chǔ)對(duì)象的json數(shù)據(jù),需要頻繁的序列化和反序列化,后來(lái)考慮更換項(xiàng)目中的redis存儲(chǔ)對(duì)象為hash對(duì)象存儲(chǔ)的,但是獲取后不能方便的set get操作,很是蛋疼,怎么才能解決這個(gè)問(wèn)題呢?
存放redis的時(shí)候,直接先用fastJson 或者 jackJson或者Gson把對(duì)象序列化為json數(shù)據(jù),然后用直接存放,key表示用戶id或許和openid,value則是對(duì)象的json數(shù)據(jù)
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); }
優(yōu)點(diǎn):雖然需要序列化和反序列化,但是可以直接操作對(duì)象的方法,方便快捷
缺點(diǎn):需要序列化和反序列化,并且修改單個(gè)字段,需要獲取整個(gè)json,修改后,序列化保存,浪費(fèi)空間,浪費(fèi)時(shí)間,效率低
key代表主鍵,比如用戶id,或者openId,value是一個(gè)map,對(duì)應(yīng)各個(gè)字段的屬性和值
存放單個(gè)字段
public void hset(String key, String field, String obj) { redisTemplate.boundHashOps(key).put(field,obj); }
存放整個(gè):
public void hSetMap(String key,Map<Object,Object> map){ redisTemplate.boundHashOps(key).putAll(map); }
優(yōu)點(diǎn):存儲(chǔ)方方便,節(jié)省內(nèi)存空間,并且可以直接對(duì)單個(gè)字段修改,而不用獲取整個(gè)對(duì)象,效率高
缺點(diǎn):獲取value后,是個(gè)map,不能方便的直接調(diào)用(set get)處理,需要手動(dòng)map.get(filed)或者map.put(field,value)
其實(shí)關(guān)于map和pojo的轉(zhuǎn)換問(wèn)題,網(wǎng)上給出了利用反射做的轉(zhuǎn)換方法,但是加上了轉(zhuǎn)換和反轉(zhuǎn),這和序列化和反序列化的問(wèn)題一樣了,效率問(wèn)題,也不敢指直接用,糾結(jié),思考再三,還是先維持代碼不動(dòng)了,以后考慮好了再說(shuō),或者廣發(fā)網(wǎng)友有啥好解決方法,請(qǐng)多多指教哈!
序列化工具類實(shí)現(xiàn)
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實(shí)例
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);//最大連接數(shù) config.setMaxIdle(MAX_IDLE);//最大空閑連接數(shù) config.setMaxWaitMillis(MAX_WAIT);//獲取可用連接的最大等待時(shí)間 jedisPool = new JedisPool(config, HOST, PORT); } catch (Exception e) { e.printStackTrace(); } } /* * 獲取jedis實(shí)例 * */ 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對(duì)象
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方法 }
測(cè)試
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()); } }
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); } }
測(cè)試
@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()); }
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; } }
測(cè)試
@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); }
到此,相信大家對(duì)“redis存儲(chǔ)對(duì)象方法的比較”有了更深的了解,不妨來(lái)實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。