github地址:https://github.com/jiangcaijun/ssmhtml
github版本號java
<!-- config redis data and client jar--> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version>1.7.1.RELEASE</version> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.8.1</version> </dependency>
注意:起初,我將redis.clients jedis的version設爲 2.1.0 ,致使jedis.close()方法不存在。將其版本號升高便可。node
redis.minIdle=5 redis.maxIdle=10 redis.maxTotal=50 redis.maxWaitMillis=1500 redis.testOnBorrow=true redis.numTestsPerEvictionRun=1024 redis.timeBetweenEvictionRunsMillis=30000 redis.minEvictableIdleTimeMillis=1800000 redis.softMinEvictableIdleTimeMillis=10000 redis.testWhileIdle=true redis.blockWhenExhausted=false #redis單機版信息 redis.host=182.254.xxx.xx redis.port=7001 #redis集羣版信息 cluster.host1=182.254.xxx.xx cluster.port1=7001 cluster.host2=182.254.xxx.xx cluster.port2=7002 cluster.host3=182.254.xxx.xx cluster.port3=7003 cluster.host4=182.254.xxx.xx cluster.port4=7004 cluster.host5=182.254.xxx.xx cluster.port5=7005 cluster.host6=182.254.xxx.xx cluster.port6=7006
<!-- 引入jdbc、redis等配置文件 --> <context:property-placeholder location="classpath:*.properties" />
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd"> <!-- jedis鏈接池配置 --> <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig"> <!-- 最小空閒鏈接數 --> <property name="minIdle" value="${redis.minIdle}"/> <!-- 最大空閒鏈接數 --> <property name="maxIdle" value="${redis.maxIdle}"/> <!-- 最大鏈接數 --> <property name="maxTotal" value="${redis.maxTotal}"/> <!-- 獲取鏈接時的最大等待毫秒數,小於零:阻塞不肯定的時間,默認-1 --> <property name="maxWaitMillis" value="${redis.maxWaitMillis}"/> <!-- 在獲取鏈接的時候檢查有效性, 默認false --> <property name="testOnBorrow" value="${redis.testOnBorrow}"/> <!-- 每次釋放鏈接的最大數目 --> <property name="numTestsPerEvictionRun" value="${redis.numTestsPerEvictionRun}"/> <!-- 釋放鏈接的掃描間隔(毫秒) --> <property name="timeBetweenEvictionRunsMillis" value="${redis.timeBetweenEvictionRunsMillis}"/> <!-- 鏈接最小空閒時間 --> <property name="minEvictableIdleTimeMillis" value="${redis.minEvictableIdleTimeMillis}"/> <!-- 鏈接空閒多久後釋放, 當空閒時間>該值 且 空閒鏈接>最大空閒鏈接數 時直接釋放 --> <property name="softMinEvictableIdleTimeMillis" value="${redis.softMinEvictableIdleTimeMillis}"/> <!-- 在空閒時檢查有效性, 默認false --> <property name="testWhileIdle" value="${redis.testWhileIdle}"/> <!-- 鏈接耗盡時是否阻塞, false報異常,ture阻塞直到超時, 默認true --> <property name="blockWhenExhausted" value="${redis.blockWhenExhausted}"/> </bean> <!-- 單機版 --> <!-- 配置jedis鏈接池 --> <bean class="redis.clients.jedis.JedisPool"> <constructor-arg name="poolConfig" ref="poolConfig"/> <constructor-arg name="host" value="${redis.host}" /> <constructor-arg name="port" value="${redis.port}" /> </bean> <!-- 配置單機版工具類 --> <bean class="com.ssm.redis.RedisPool" /> </beans>
兩種方案:使用jedis鏈接池(單機版)和jedis集羣,上述配置爲單機版linux
通常生產環境使用集羣,開發環境使用單機版git
使用哪一種,能夠將另外一種註釋掉github
注意:若是在注入工具類的時候是按類型注入的話,那麼不使用的工具類必須註釋掉redis
集羣版以下:spring
<!-- 集羣版 --> <!-- 配置jedis集羣 --> <bean class="redis.clients.jedis.JedisCluster"> <constructor-arg name="nodes"> <set> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg name="host" value="${cluster.host1}" /> <constructor-arg name="port" value="${cluster.port1}" /> </bean> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg name="host" value="${cluster.host2}" /> <constructor-arg name="port" value="${cluster.port2}" /> </bean> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg name="host" value="${cluster.host3}" /> <constructor-arg name="port" value="${cluster.port3}" /> </bean> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg name="host" value="${cluster.host4}" /> <constructor-arg name="port" value="${cluster.port4}" /> </bean> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg name="host" value="${cluster.host5}" /> <constructor-arg name="port" value="${cluster.port5}" /> </bean> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg name="host" value="${cluster.host6}" /> <constructor-arg name="port" value="${cluster.port6}" /> </bean> </set> </constructor-arg> </bean> <!--配置集羣版工具類--> <bean class="com.ssm.redis.RedisCluster" />
package com.ssm.redis; /** * Redis工具接口 * */ public interface RedisUtil { /** * 保存 * * @param key * 鍵 * @param value * zhi */ public void set(String key, String value); /** * 保存並設置生存時間 * * @param key * 鍵 * @param value * 值 * @param seconds * 時間,秒s爲單位 */ public void set(String key, String value, Integer seconds); /** * 根據key查詢 * * @param key * 鍵 * @return 值 */ public String get(String key); /** * 刪除 * * @param key * 鍵 */ public void del(String key); /** * 根據key設置生存時間 * * @param key * 鍵 * @param seconds * 時間,秒s爲單位 */ public void expire(String key, Integer seconds); /** * value加一<br/> * 注意key必須是整型 * * @param key * 鍵 * @return 加一後的結果 */ public Long incr(String key); }
package com.ssm.redis; import org.springframework.beans.factory.annotation.Autowired; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; /** * Redis鏈接池工具類 */ public class RedisPool implements RedisUtil { @Autowired private JedisPool jedisPool; @Override public void set(String key, String value) { Jedis jedis = this.getJedis(); jedis.set(key, value); this.releaseJedis(jedis); } @Override public void set(String key, String value, Integer seconds) { Jedis jedis = this.getJedis(); jedis.set(key, value); jedis.expire(key, seconds); this.releaseJedis(jedis); } @Override public String get(String key) { Jedis jedis = this.getJedis(); String result = jedis.get(key); this.releaseJedis(jedis); return result; } @Override public void del(String key) { Jedis jedis = this.getJedis(); jedis.del(key); this.releaseJedis(jedis); } @Override public void expire(String key, Integer seconds) { Jedis jedis = this.getJedis(); jedis.expire(key, seconds); this.releaseJedis(jedis); } @Override public Long incr(String key) { Jedis jedis = this.getJedis(); Long count = jedis.incr(key); this.releaseJedis(jedis); return count; } /** * 獲取Jedis鏈接 * * @return Jedis鏈接 */ private Jedis getJedis() { return this.jedisPool.getResource(); } /** * 釋放Jedis鏈接,返還到鏈接池 * * @param jedis * jedis鏈接 */ private void releaseJedis(Jedis jedis) { jedis.close(); } }
package com.ssm.redis; import org.springframework.beans.factory.annotation.Autowired; import redis.clients.jedis.JedisCluster; /** * Redis集羣工具類 */ public class RedisCluster implements RedisUtil { @Autowired private JedisCluster jedisCluster; @Override public void set(String key, String value) { this.jedisCluster.set(key, value); } @Override public void set(String key, String value, Integer seconds) { this.jedisCluster.set(key, value); this.jedisCluster.expire(key, seconds); } @Override public String get(String key) { return this.jedisCluster.get(key); } @Override public void del(String key) { this.jedisCluster.del(key); } @Override public void expire(String key, Integer seconds) { this.jedisCluster.expire(key, seconds); } @Override public Long incr(String key) { return this.jedisCluster.incr(key); } /** * 獲取JedisCluster對象 * 能夠直接使用它來進行redis操做 * * @return JedisCluster對象 */ public JedisCluster getJedisCluster() { return jedisCluster; } }
@RequestMapping(value = "/guest", method = RequestMethod.GET) public String guest(Model model,String parameter1) { LOG.info("進入guest的index"); String key = parameter1; // 1.從緩存中命中 try { String redisJson = redisUtil.get(key); if (StringUtils.isNotBlank(redisJson)) { LOG.info("從緩存中命中:"+redisJson); }else{ // 2.若是沒被命中,執行原有邏輯 LOG.info("沒被命中"); // 此處模擬從數據庫獲取數據 String json = "getFromDB()"; // 3.將查詢出來的結果加入緩存 redisUtil.set(key, json); redisUtil.expire(key, 60 * 60 * 24); } } catch (Exception e) { e.printStackTrace(); } return "guest/guestIndex"; }
注意:若是redis沒有啓動,會在catch塊中報 JedisConnectionException: Could not get a resource from the poolsql
瀏覽器地址欄輸入 :http://127.0.0.1:8080/項目名/guest?parameter1=1,控制檯輸出:數據庫
[ INFO ] 進入guest的index [ INFO ] 沒被命中
瀏覽器刷新頁面,控制檯輸出:
[ INFO ] 進入guest的index [ INFO ] 從緩存中命中:getFromDB()
測試成功,在部署redis的系統上登陸 redis-cli,利用get命令,也能夠查詢到該key-value值
對於某些需求,例如只須要作查詢的項目,或者部分數據只作查詢,不作增刪改的項目(例如省市區表等),能夠利用aop與redis,查詢數據時先從redis中查詢,若是查詢不到,則到數據庫中查詢,而後將數據庫中查詢的數據放到redis中一份,下次查詢時就能直接從redis中查到,不須要查詢數據庫了。
連接:SSM框架_6(spring與redis整合)(spring+ redis + aop ) - 開源小菜鳥2333
連接:
爲了解決Jedis經過公網IP沒法連接上redis的狀況:DENIED Redis is running in protected mode because protected mode is enabled, no bind address was spe_NoSQL_第七城市
修改默認端口號爲7001後,linux下客戶端如何訪問新port:./redis-cli -h 127.0.0.1 -p 7001linux redis-cli 根據端口號進入交互頁面-CSDN問答