java用redis缓存的步骤_详解在Java程序中运用Redis缓存对象的方法|chu
這段時(shí)間一直有人問如何在Redis中緩存Java中的List 集合數(shù)據(jù),其實(shí)很簡(jiǎn)單,常用的方式有兩種:
1. 利用序列化,把對(duì)象序列化成二進(jìn)制格式,Redis 提供了 相關(guān)API方法存儲(chǔ)二進(jìn)制,取數(shù)據(jù)時(shí)再反序列化回來,轉(zhuǎn)換成對(duì)象。
2. 利用 Json與java對(duì)象之間可以相互轉(zhuǎn)換的方式進(jìn)行存值和取值。
正面針對(duì)這兩種方法,特意寫了一個(gè)工具類,來實(shí)現(xiàn)數(shù)據(jù)的存取功能。
1. 首頁(yè)在Spring框架中配置?JedisPool 連接池對(duì)象,此對(duì)象可以創(chuàng)建 Redis的連接 Jedis對(duì)象。當(dāng)然,必須導(dǎo)入Redis的相關(guān)Jar包。
Jedis 的Jar包如下:commons-pool2-2.3.jar
jedis-2.9.0.jar
要用到 Json,所以還需要導(dǎo)入Json的Jar包:commons-beanutils-1.8.0.jar
commons-collections-3.1.jar
commons-lang-2.5.jar
commons-logging-1.1.3.jar
ezmorph-1.0.6.jar
json-lib-2.3-jdk15.jar
在配置文件中配置JedisPool 連接池對(duì)象
2. 創(chuàng)建一個(gè)Redis的工具類RedisUtil,這個(gè)類中實(shí)現(xiàn)了上面所說的兩種方法的存取操作
package com.sgxy.util; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import net.sf.json.JSONArray; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; @Component public class RedisUtil { @Autowired JedisPool pool; // Jedis連接池 // 判斷Redis中是否存在鍵 public boolean existsKey(String key) { Jedis jedis = pool.getResource(); boolean bool; try { bool = jedis.exists(key); } finally { jedis.close(); } return bool; } // 取緩存中的二進(jìn)制數(shù)據(jù),反序列化成List集合對(duì)象 @SuppressWarnings("unchecked") public List getObject(String key, Class clazz) { Jedis jedis = pool.getResource(); // 二進(jìn)制 IO 輸入流 ByteArrayInputStream is = null; ObjectInputStream ois = null; try { // 從緩存中取二進(jìn)制數(shù)據(jù) byte[] b = jedis.get(key.getBytes()); is = new ByteArrayInputStream(b); ois = new ObjectInputStream(is); // 把二進(jìn)制轉(zhuǎn)換成T指定類型的集合 return (List) ois.readObject(); } catch (Exception e) { e.printStackTrace(); } finally { try { is.close(); ois.close(); } catch (Exception e2) { e2.printStackTrace(); } jedis.close(); } return null; } // 把對(duì)象序列化二進(jìn)制格式并保證到Redis緩存中 public void saveObject(Object object, String key) { Jedis jedis = pool.getResource(); // 二進(jìn)制 IO 輸出流 ByteArrayOutputStream os = null; ObjectOutputStream oos = null; try { os = new ByteArrayOutputStream(); oos = new ObjectOutputStream(os); oos.writeObject(object); // 二進(jìn)制數(shù)據(jù) byte[] b = os.toByteArray(); // 存入二進(jìn)制數(shù)據(jù)到Redis緩存中 jedis.set(key.getBytes(), b); } catch (Exception e) { e.printStackTrace(); } finally { try { os.close(); oos.close(); } catch (Exception e2) { e2.printStackTrace(); } jedis.close(); } } // 把List集合對(duì)象轉(zhuǎn)換成json格式保存到指定的鍵中 public void saveJsonArray(Object object, String key) { Jedis jedis = pool.getResource(); try { // 格式化成Json字符串 JSONArray array = JSONArray.fromObject(object); jedis.set(key, array.toString()); // 存入緩存 } finally { jedis.close(); } } // 通過鍵取出Json字符串并轉(zhuǎn)換成 Class這個(gè)T所指定的類型 @SuppressWarnings({ "static-access", "unchecked" }) public List getJsonArray(String key, Class clazz) { Jedis jedis = pool.getResource(); try { String str = jedis.get(key); JSONArray array = JSONArray.fromObject(str); // 把字符串轉(zhuǎn)換回集合對(duì)象 clazz是指定的類型 return (List) array.toCollection(array, clazz); } finally { jedis.close(); } } }
在Java程序其他地方操作這個(gè)工具類做數(shù)據(jù)的處理:
@Controller //注解這個(gè)類為控制器 @RequestMapping("grade") //注冊(cè)訪問此控制器的URL public class GradeController { @Autowired // 從IOC容器注入業(yè)務(wù)層對(duì)象 GradeService gradeService; @Autowired JedisPool pool; @Autowired RedisUtil redisUtil; @RequestMapping("list") //注冊(cè)URL public ModelAndView list() { List grades = null; if (redisUtil.existsKey("g")) { System.out.println("從Redis 緩存中取數(shù)據(jù).."); //調(diào)用反序列化方法取緩存的數(shù)據(jù) grades = redisUtil.getObject("g",Grade.class); //調(diào)用Json格式轉(zhuǎn)換的方法取緩存數(shù)據(jù) //grades = redisUtil.getJsonArray("gradeList", Grade.class); } else { System.out.println("從數(shù)據(jù)庫(kù)中取數(shù)據(jù),并存入緩存.."); //調(diào)用底層方法從數(shù)據(jù)庫(kù)中取數(shù)據(jù) grades = gradeService.find(); //調(diào)用序列化方法把數(shù)據(jù)緩存到Redis中 redisUtil.saveObject(grades, "g"); //調(diào)用Json格式化方法把數(shù)據(jù)緩存到Redis中 //redisUtil.saveJsonArray(grades, "gradeList"); } return new ModelAndView("gradeList", "grades", grades); } }
寫到此,希望對(duì)大家有所幫助。
以上所述是小編給大家介紹的在Java程序中運(yùn)用Redis緩存對(duì)象的方法詳解整合,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)華域聯(lián)盟網(wǎng)站的支持!
總結(jié)
以上是生活随笔為你收集整理的java用redis缓存的步骤_详解在Java程序中运用Redis缓存对象的方法|chu的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java string 内存占用_JVM
- 下一篇: java绘制_Java 绘制简单图形的问