日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

spring整合redis缓存

發布時間:2025/4/16 编程问答 15 豆豆
生活随笔 收集整理的這篇文章主要介紹了 spring整合redis缓存 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

如果對spring cache不了解,這邊有一篇文章詳細的介紹了
http://jinnianshilongnian.iteye.com/blog/2001040
改項目基于maven構建
相關依賴

Xml代碼

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 <dependency>?? ????<groupId>org.springframework.data</groupId>?? ????<artifactId>spring-data-redis</artifactId>?? ????<version>1.3.4.RELEASE</version>?? </dependency>?? ??? <dependency>?? ????<groupId>redis.clients</groupId>?? ????<artifactId>jedis</artifactId>?? ????<version>2.5.2</version>?? </dependency>?? ??? <dependency>?? ????<groupId>org.springframework</groupId>?? ????<artifactId>spring-jdbc</artifactId>?? ????<version>3.2.13.RELEASE</version>?? </dependency>?? ??? <!--?日志記錄依賴包,很多都依賴此包,像log4j,json-lib等等?-->?? <dependency>?? ????<groupId>commons-logging</groupId>?? ????<artifactId>commons-logging-api</artifactId>?? ????<version>1.1</version>?? </dependency>?? ??? <!--?junit?測試包?-->?? <dependency>?? ????<groupId>junit</groupId>?? ????<artifactId>junit</artifactId>?? ????<version>4.11</version>?? ????<scope>test</scope>?? </dependency>?? ??? <dependency>?? ????<groupId>org.springframework</groupId>?? ????<artifactId>spring-test</artifactId>?? ????<version>3.2.13.RELEASE</version>?? ????<scope>test</scope>?? </dependency>

Spring相關配置
Xml代碼 ?

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 52 53 54 55 56 <?xml?version="1.0"?encoding="UTF-8"?>?? <beans?xmlns="http://www.springframework.org/schema/beans"?? ????xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"?xmlns:context="http://www.springframework.org/schema/context"?? ????xmlns:cache="http://www.springframework.org/schema/cache"?xmlns:p="http://www.springframework.org/schema/p"?? ????xmlns:c="http://www.springframework.org/schema/c"?? ????xsi:schemaLocation="?? ????????http://www.springframework.org/schema/beans?http://www.springframework.org/schema/beans/spring-beans-3.2.xsd?? ????????http://www.springframework.org/schema/context?http://www.springframework.org/schema/context/spring-context-3.2.xsd?? ????????http://www.springframework.org/schema/jee?http://www.springframework.org/schema/jee/spring-jee-3.2.xsd?? ????????http://www.springframework.org/schema/cache??? ????????http://www.springframework.org/schema/cache/spring-cache-3.2.xsd"?? ????default-lazy-init="true">?? ??? ????<context:property-placeholder?location="classpath:redis.properties"?/>?? ??????? ????<!--?自動掃描包?-->?? ????<context:component-scan?base-package="com.layou"?/>?? ??? ??? ????<!--?啟用緩存注解功能,這個是必須的,否則注解不會生效,另外,該注解一定要聲明在spring主配置文件中才會生效?-->?? ????<cache:annotation-driven?cache-manager="cacheManager"?/>?? ??? ????<!--?spring自己的換管理器,這里定義了兩個緩存位置名稱?,既注解中的value?-->?? ????<bean?id="cacheManager"?class="org.springframework.cache.support.SimpleCacheManager">?? ????????<property?name="caches">?? ????????????<set>?? ????????????????<bean?class="com.layou.RedisCache">?? ????????????????????<property?name="redisTemplate"?ref="redisTemplate"?/>?? ????????????????????<property?name="name"?value="default"?/>?? ????????????????</bean>?? ????????????</set>?? ????????</property>?? ????</bean>?? ??? ????<!--?redis?相關配置?-->?? ????<bean?id="poolConfig"?class="redis.clients.jedis.JedisPoolConfig">?? ????????<property?name="maxIdle"?value="${redis.pool.maxIdle}"?/>?? ????????<property?name="maxTotal"?value="${redis.pool.maxActive}"?/>?? ????????<property?name="maxWaitMillis"?value="${redis.pool.maxWait}"?/>?? ????????<property?name="testOnBorrow"?value="${redis.pool.testOnBorrow}"?/>?? ????</bean>?? ??? ????<bean?id="connectionFactory"?? ????????class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">?? ????????<property?name="hostName"?value="${redis.host}"?/>?? ????????<property?name="port"?value="${redis.port}"?/>?? ????????<property?name="password"?value="${redis.password}"?/>?? ????????<property?name="timeout"?value="${redis.timeout}"?/>?? ????????<property?name="usePool"?value="true"?/>?? ????????<property?name="poolConfig"?ref="poolConfig"?/>?? ????</bean>?? ??? ????<bean?id="redisTemplate"?class="org.springframework.data.redis.core.RedisTemplate">?? ????????<property?name="connectionFactory"?ref="connectionFactory"?/>?? ????</bean>?? </beans>

redis相關配置
Java代碼 ?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #redis.host=localhost?? redis.host=192.168.71.145?? redis.port=6379?? redis.password=?? #客戶端超時時間單位是毫秒?? redis.timeout=100000?? ??? #最大連接數?? redis.pool.maxActive=500???? #最大能夠保持idel狀態的對象數???? redis.pool.maxIdle=200???? #最大建立連接等待時間???? redis.pool.maxWait=2000???? #當調用borrow?Object方法時,是否進行有效性檢查???? redis.pool.testOnBorrow=true

首先,通過自己實現Cache接口來實現對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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 package?com.layou;?? ??? import?java.io.ByteArrayInputStream;?? import?java.io.ByteArrayOutputStream;?? import?java.io.IOException;?? import?java.io.ObjectInputStream;?? import?java.io.ObjectOutputStream;?? ??? import?org.springframework.cache.Cache;?? import?org.springframework.cache.support.SimpleValueWrapper;?? import?org.springframework.dao.DataAccessException;?? import?org.springframework.data.redis.connection.RedisConnection;?? import?org.springframework.data.redis.core.RedisCallback;?? import?org.springframework.data.redis.core.RedisTemplate;?? ??? public?class?RedisCache?implements?Cache?{?? ??? ????private?RedisTemplate<String,?Object>?redisTemplate;?? ????private?String?name;?? ??? ????public?RedisTemplate<String,?Object>?getRedisTemplate()?{?? ????????return?redisTemplate;?? ????}?? ??? ????public?void?setRedisTemplate(RedisTemplate<String,?Object>?redisTemplate)?{?? ????????this.redisTemplate?=?redisTemplate;?? ????}?? ??? ????public?void?setName(String?name)?{?? ????????this.name?=?name;?? ????}?? ??? ????@Override?? ????public?String?getName()?{?? ????????return?this.name;?? ????}?? ??? ????@Override?? ????public?Object?getNativeCache()?{?? ????????return?this.redisTemplate;?? ????}?? ??? ????@Override?? ????public?ValueWrapper?get(Object?key)?{?? ????????final?String?keyf?=?(String)?key;?? ????????Object?object?=?null;?? ????????object?=?redisTemplate.execute(new?RedisCallback<Object>()?{?? ????????????public?Object?doInRedis(RedisConnection?connection)?throws?DataAccessException?{?? ??? ????????????????byte[]?key?=?keyf.getBytes();?? ????????????????byte[]?value?=?connection.get(key);?? ????????????????if?(value?==?null)?{?? ????????????????????return?null;?? ????????????????}?? ????????????????return?toObject(value);?? ??? ????????????}?? ????????});?? ????????return?(object?!=?null???new?SimpleValueWrapper(object)?:?null);?? ????}?? ??? ????@Override?? ????public?void?put(Object?key,?Object?value)?{?? ????????final?String?keyf?=?(String)?key;?? ????????final?Object?valuef?=?value;?? ????????final?long?liveTime?=?86400;?? ??? ????????redisTemplate.execute(new?RedisCallback<Long>()?{?? ????????????public?Long?doInRedis(RedisConnection?connection)?throws?DataAccessException?{?? ????????????????byte[]?keyb?=?keyf.getBytes();?? ????????????????byte[]?valueb?=?toByteArray(valuef);?? ????????????????connection.set(keyb,?valueb);?? ????????????????if?(liveTime?>?0)?{?? ????????????????????connection.expire(keyb,?liveTime);?? ????????????????}?? ????????????????return?1L;?? ????????????}?? ????????});?? ????}?? ??? ????@Override?? ????public?void?evict(Object?key)?{?? ????????final?String?keyf?=?(String)?key;?? ????????redisTemplate.execute(new?RedisCallback<Long>()?{?? ????????????public?Long?doInRedis(RedisConnection?connection)?throws?DataAccessException?{?? ????????????????return?connection.del(keyf.getBytes());?? ????????????}?? ????????});?? ????}?? ??? ????@Override?? ????public?void?clear()?{?? ????????redisTemplate.execute(new?RedisCallback<String>()?{?? ????????????public?String?doInRedis(RedisConnection?connection)?throws?DataAccessException?{?? ????????????????connection.flushDb();?? ????????????????return?"ok";?? ????????????}?? ????????});?? ????}?? ??? ????/**? ?????*?描述?:?<Object轉byte[]>.?<br>? ?????*?<p>? ?????*?<使用方法說明>? ?????*?</p>? ?????*?? ?????*?@param?obj? ?????*?@return? ?????*/?? ????private?byte[]?toByteArray(Object?obj)?{?? ????????byte[]?bytes?=?null;?? ????????ByteArrayOutputStream?bos?=?new?ByteArrayOutputStream();?? ????????try?{?? ????????????ObjectOutputStream?oos?=?new?ObjectOutputStream(bos);?? ????????????oos.writeObject(obj);?? ????????????oos.flush();?? ????????????bytes?=?bos.toByteArray();?? ????????????oos.close();?? ????????????bos.close();?? ????????}?catch?(IOException?ex)?{?? ????????????ex.printStackTrace();?? ????????}?? ????????return?bytes;?? ????}?? ??? ????/**? ?????*?描述?:?<byte[]轉Object>.?<br>? ?????*?<p>? ?????*?<使用方法說明>? ?????*?</p>? ?????*?? ?????*?@param?bytes? ?????*?@return? ?????*/?? ????private?Object?toObject(byte[]?bytes)?{?? ????????Object?obj?=?null;?? ????????try?{?? ????????????ByteArrayInputStream?bis?=?new?ByteArrayInputStream(bytes);?? ????????????ObjectInputStream?ois?=?new?ObjectInputStream(bis);?? ????????????obj?=?ois.readObject();?? ????????????ois.close();?? ????????????bis.close();?? ????????}?catch?(IOException?ex)?{?? ????????????ex.printStackTrace();?? ????????}?catch?(ClassNotFoundException?ex)?{?? ????????????ex.printStackTrace();?? ????????}?? ????????return?obj;?? ????}?? }

使用junit進行測試

  • package?com.layou;??

  • ??

  • import?org.junit.Before;??

  • import?org.junit.Test;??

  • import?org.springframework.beans.factory.annotation.Autowired;??

  • import?org.springframework.cache.CacheManager;??

  • import?org.springframework.test.context.ContextConfiguration;??

  • import?org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;??

  • ??

  • @ContextConfiguration(locations?=?{"classpath:applicationContext-cache-redis.xml"})??

  • public?class?RedisTest?extends?AbstractJUnit4SpringContextTests?{??

  • ????private?RedisCache?cache;??

  • ????@Autowired??

  • ????private?CacheManager?manager;??

  • ??????

  • ????@Before??

  • ????public?void?before()?{??

  • ????????cache?=?(RedisCache)?manager.getCache("default");??

  • ????}??

  • ??????

  • ????@Test??

  • ????public?void?testAdd()?throws?Exception?{??

  • ????????cache.put("test",?1);??

  • ????}??

  • ??????

  • ????@Test??

  • ????public?void?testGet()?throws?Exception?{??

  • ????????System.out.println(cache.get("test").get());??

  • ????}??

  • ??????

  • ????@Test??

  • ????public?void?testEvict()?throws?Exception?{??

  • ????????cache.evict("test");??

  • ????}??

  • ??????

  • ????@Test??

  • ????public?void?testClear()?throws?Exception?{??

  • ????????cache.clear();??

  • ????}??

  • }

  • 在實際項目中,多是通過Spring的注解完成相關操作

    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 package?com.layou;?? ??? import?org.springframework.cache.annotation.CacheEvict;?? import?org.springframework.cache.annotation.Cacheable;?? import?org.springframework.stereotype.Service;?? ??? @Service?? public?class?RedisAnnotation?{?? ??????? ????/**? ?????*?第一次查詢會到數據庫中差尋,第二次就會從緩存中取? ?????*?#id代表使用參數id? ?????*?一定要放在spring容器中管理,不然攔截不到? ?????*?@param?id? ?????*?@return? ?????*/?? ????@Cacheable(key="'name'?+?#id",?value="default")?? ????public?String?getNameById(int?id)?{?? ????????//TODO?進行數據庫查詢并返回查詢數據庫內容?? ????????System.out.println("查詢數據庫");?? ????????return?""?+?id;?? ????}?? ??????? ????/**? ?????*?從緩存中刪除? ?????*?@param?id? ?????*/?? ????@CacheEvict(key="'name'?+?#id",?value="default")?? ????public?void?deleteById(int?id)?{?? ????????System.out.println("數據庫刪除");?? ????}?? }

    使用junit進行相關測試










    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 package?com.layou;?? ??? import?org.junit.Test;?? import?org.springframework.beans.factory.annotation.Autowired;?? import?org.springframework.test.context.ContextConfiguration;?? import?org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;?? ??? @ContextConfiguration(locations?=?{"classpath:applicationContext-cache-redis.xml"})?? public?class?RedisAnnotationTest?extends?AbstractJUnit4SpringContextTests?{?? ????@Autowired?? ????private?RedisAnnotation?redis;?? ??????? ????@Test?? ????public?void?testQuery()?{?? ????????//第二次查詢的時候就會從緩存中取?? ????????System.out.println(redis.getNameById(1));?? ????}?? ??????? ????@Test?? ????public?void?testDelete()?{?? ????????redis.deleteById(1);?? ????}?? } ??










    本文轉自yunlielai51CTO博客,原文鏈接:http://blog.51cto.com/4925054/1741066,如需轉載請自行聯系原作者



    總結

    以上是生活随笔為你收集整理的spring整合redis缓存的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

    主站蜘蛛池模板: 日韩欧美aaa | 天天干天天谢 | 午夜男人网| 中文字幕十一区 | 国产aⅴ一区二区三区 | 亚洲25p | 成年人精品视频 | 91在线在线 | 亚洲天堂网在线观看 | 污片免费网站 | 国产综合久久久久久鬼色 | 国产精品乱码久久久久久 | 国产精品久久久久久免费免熟 | 激情欧美一区二区三区精品 | 国语粗话呻吟对白对白 | 日韩一区二区三区在线视频 | 四虎视频 | 农村妇女毛片精品久久久 | 艳妇乳肉亭妇荡乳av | 国产+日韩+欧美 | 久久视频网 | 第四色激情 | 亚洲国产第一页 | 91视频综合 | 操操干| 亚洲色图 校园春色 | 亚洲第一黄色 | 色呦呦视频在线观看 | 国产一区二区不卡 | 献给魔王伊伏洛基亚吧动漫在线观看 | 911色 | 久久久欧美 | 亚洲aaa视频 | 国产另类av| 精品久久久蜜桃 | 欧美日韩午夜 | 成人午夜视频免费 | 日韩综合第一页 | 成人自拍av | 中文字幕精品国产 | 国语对白少妇spa私密按摩 | 来吧亚洲综合网 | 麻豆国产精品 | 2019狠狠干| av中文字幕免费在线观看 | 制服丝袜手机在线 | 无码国产69精品久久久久网站 | 99久久久国产精品 | 久久靠逼视频 | av手机在线免费观看 | 9999re| 欧美大胆视频 | 色翁荡息又大又硬又粗又爽 | 男生把女生困困的视频 | 深夜成人福利视频 | 在线观看的网站 | 成人免费视屏 | 日韩精品激情 | 人妻熟女aⅴ一区二区三区汇编 | 在线观看日本一区 | 亚洲视频一区二区三区 | 兄弟兄弟全集免费观看 | 99毛片| 无码人妻一区二区三区在线视频 | 在线观看免费黄色小视频 | 国产成人+综合亚洲+天堂 | 天堂在线观看av | xxx性欧美 | 日本公妇乱淫免费视频一区三区 | 国产九九九九 | 久久三级 | av免费看网站 | 欧美18av | 天堂а√在线中文在线鲁大师 | 天天干天天干天天干天天 | 日日射日日操 | 国产高清一区二区三区 | 亚洲欧美网 | 狠狠干快播 | 一区二区在线视频免费观看 | 悠悠色在线 | 超碰免费公开 | jjzz在线| 久久久夜夜 | 国产精品久久久久久三级 | 中文字幕 国产 | 玖玖成人 | 日产欧产va高清 | 亚洲欧美偷拍视频 | 日日夜夜天天 | 国产一区二区免费在线观看 | 玖玖综合网 | 激情久久婷婷 | 日韩伦乱 | 国产精品免费视频观看 | 色吧综合 | 免费公开在线视频 | 人妻久久一区二区三区 | 色视频综合|