日韩性视频-久久久蜜桃-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缓存的全部內容,希望文章能夠幫你解決所遇到的問題。

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

    主站蜘蛛池模板: 99热视 | 国产人与zoxxxx另类 | 娇妻玩4p被三个男人伺候电影 | 日本黄视频在线观看 | 超碰中文字幕在线 | 人妻视频一区二区 | 女人喂男人奶水做爰视频 | aaa人片在线| 日韩一区二区欧美 | 天堂√ | 少妇人妻精品一区二区三区 | 东京av在线| 三级av毛片 | 日本不卡专区 | 老司机在线看片 | 国产午夜小视频 | 免费不卡视频 | 少妇捆绑紧缚av | 久久无码视频网站 | 一本色道久久综合熟妇 | 天天爽天天爱 | 日日骑 | 欧美做受高潮中文字幕 | 欧美成a | 久久99国产精品久久99果冻传媒 | 五月天六月色 | 一区二区欧美视频 | 国产视频不卡一区 | 人妻一区在线 | 精品动漫3d一区二区三区免费版 | 亚洲巨乳| 落日余晖图片 | 男人天堂国产 | 国产精品久久一区二区三区动 | 天堂а√在线中文在线鲁大师 | h网站在线播放 | 欧美黄色高清视频 | 在线观看日韩一区 | 特级丰满少妇一级aaaa爱毛片 | 国模少妇一区二区 | 国产日韩在线观看一区 | 中出 在线 | 最近中文字幕 | 国产1区2区3区中文字幕 | 寻找身体恐怖电影免费播放 | 免费无码毛片一区二三区 | 美女扒开下面让男人捅 | 成年人在线播放视频 | 日本一区二区三区免费视频 | 日本黄色录象 | 国产精品夫妻自拍 | 91亚洲网站 | 三度诱惑免费版电影在线观看 | 伊人久久综合影院 | 性欧美大战久久久久久久久 | 精品久久久久久久久久久久久久久久 | av777777| 女人特黄大aaaaaa大片 | 91大神福利视频 | 98在线视频| 理论片第一页 | 中文字幕乱码人妻二区三区 | 成人视屏在线观看 | 成人免费看片&#39; | 噜噜噜精品欧美成人 | 91精品视频网站 | 中国免费一级片 | 成人黄色小视频 | 成人免费在线 | 国产a自拍| 国产精品igao | xxxx性视频 | 亚洲精品福利网站 | 美女视频在线观看免费 | 欧美日韩一级二级 | 国产真人真事毛片 | 丰满人妻一区二区三区四区 | 毛片毛片毛片毛片毛片毛片毛片毛片毛片毛片 | 激情国产视频 | 国产探花在线精品一区二区 | 福利视频在线导航 | 都市激情 亚洲 | 亚洲一区激情 | 好吊妞一区二区三区 | 放几个免费的毛片出来看 | 国产999精品 | 人人妻人人澡人人爽欧美一区 | 91福利网站 | 中文字幕第九页 | 怡红院国产 | 男人天堂av网站 | 精品久久久一区二区 | 一级久久 | 自拍偷拍视频在线观看 | 亚洲少妇色 | 欧美日韩在线观看成人 | 99久久久久久久久久 | 一级做a爱片性色毛片 | 欧美 日韩 国产 中文 |