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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

redis与spring的完全集成

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

2019獨角獸企業重金招聘Python工程師標準>>>

redis與spring的完全集成 博客分類: 緩存

下載spring-data-redis,gav如下:

[html]? view plain copy print ?
  • ????????????????<dependency>??
  • ????<groupId>org.springframework.data</groupId>??
  • ????<artifactId>spring-data-redis</artifactId>??
  • ????<version>1.0.1.RELEASE</version>??
  • ????<exclusions>??
  • ????????<exclusion>??
  • ????????????<groupId>org.slf4j</groupId>??
  • ????????????<artifactId>slf4j-log4j12</artifactId>??
  • ????????</exclusion>??
  • ????????<exclusion>??
  • ????????????<groupId>org.slf4j</groupId>??
  • ????????????<artifactId>jcl-over-slf4j</artifactId>??
  • ????????</exclusion>??
  • ????</exclusions>??
  • ??
  • </dependency>??

  • 其中exclusion了兩個包,原因是與項目里其它包沖突。

    bean配置如下,可在web.xml里配置加載bean文件:

    [html]? view plain copy print ?
  • <bean?id="redisCacheManager"?class="com.cr.common.cache.base.RedisCacheManger">??
  • ????<property?name="pool"?ref="shardedJedisPool"/>??
  • </bean>??
  • <!--?jedis?連接池配置-->??
  • <bean?id="jedisPoolConfig"?class="redis.clients.jedis.JedisPoolConfig">????
  • ????<property?name="maxActive"?????value="${redis.pool.maxActive}"?/>????
  • ????<property?name="maxIdle"???????value="${redis.pool.maxIdle}"?/>????
  • ????<property?name="maxWait"???????value="${redis.pool.maxWait}"?/>????
  • ????<property?name="testOnBorrow"??value="${redis.pool.testOnBorrow}"?/>????
  • </bean>????
  • <!--?jedis?多個服務器配置-->??
  • <bean?id="jedisShardInfo1"?class="redis.clients.jedis.JedisShardInfo">????
  • ????<constructor-arg?index="0"?value="${redis2.ip}"?/>????
  • ????<constructor-arg?index="1"?value="${redis.port}"?type="int"?/>????
  • </bean>?????
  • ??
  • <bean?id="jedisShardInfo2"?class="redis.clients.jedis.JedisShardInfo">????
  • ????<constructor-arg?index="0"?value="${redis.ip}"?/>????
  • ????<constructor-arg?index="1"?value="${redis.port}"?type="int"?/>????
  • </bean>?????
  • ??
  • <bean?id="shardedJedisPool"?class="redis.clients.jedis.ShardedJedisPool">????
  • ????<constructor-arg?index="0"?ref="jedisPoolConfig"?/>????
  • ????<constructor-arg?index="1">??
  • ????????<list>??
  • ????????????<ref?bean="jedisShardInfo1"?/>??
  • ????????????<ref?bean="jedisShardInfo2"/>??
  • ????????</list>??
  • ????</constructor-arg>????
  • </bean>??
  • ??
  • <bean?id="connectionFactory"?class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"??
  • ????>??
  • ????<property?name="hostName"???value="${redis.ip}"?/>????
  • ????<property?name="port"???????value="${redis.port}"?/>????
  • ????<property?name="poolConfig"?ref="jedisPoolConfig"?/>???
  • ??
  • ????<!--<property?name="shardInfo"??ref="shardedJedisPool"></property>-->??
  • </bean>???
  • ??????
  • <context:property-placeholder??location="/WEB-INF/spring/SystemContext.properties"/>???
  • <context:component-scan?base-package="org.springframework.data.redis.samples"/>??
  • ?????????
  • ?

    屬性文件內容如下:

    [plain]? view plain copy print ?
  • redis.ip=192.168.1.110??
  • redis2.ip=192.168.1.112??
  • #Port?????
  • redis.port=6379??
  • ??
  • #最大分配的對象數???
  • redis.pool.maxActive=1024??
  • #最大能夠保持idel狀態的對象數??
  • redis.pool.maxIdle=200??
  • #當池內沒有返回對象時,最大等待時間??
  • redis.pool.maxWait=1000??
  • #當調用borrow?Object方法時,是否進行有效性檢查???
  • redis.pool.testOnBorrow=true??
  • #當調用return?Object方法時,是否進行有效性檢查?????
  • redis.pool.testOnReturn=true??

  • 緩存管理接口:

    [java]? view plain copy print ?
  • public?interface?RedisCache?{??
  • ??????
  • ????public?<T>?T?getRedisCacheInfo(String?key);??
  • ??
  • ????public?<T>?boolean?setRedisCacheInfo(String?key,?T?value);??
  • ??????
  • }??

  • 緩存管理實現:

    [java]? view plain copy print ?
  • public?class?RedisCacheManger?implements?RedisCache?{??
  • ??????
  • ????private?ShardedJedisPool?pool?;??
  • ??????
  • ????private?Logger?log?=?Logger.getLogger(RedisCacheManger.class);???
  • ????public?ShardedJedisPool?getPool()?{??
  • ????????return?pool;??
  • ????}??
  • ??
  • ????public?void?setPool(ShardedJedisPool?pool)?{??
  • ????????this.pool?=?pool;??
  • ????}??
  • ??
  • ????public?<T>?T?getRedisCacheInfo(String?key)?{??
  • ??????????
  • ????????try?{??
  • ????????????log.info("get?from?redisCache?:"+key);??
  • ????????????System.out.println("get?from?rediscache");??
  • ????????????ShardedJedis?jedis?=?pool.getResource();??
  • ????????????pool.returnResource(jedis);??
  • ????????????return?(T)jedis.get(key);??
  • ????????}?catch?(Exception?e)?{??
  • ????????????e.printStackTrace();??
  • ????????}??
  • ????????return?null;??
  • ????}??
  • ???
  • ??
  • ????public?<T>?boolean?setRedisCacheInfo(String?key,?T?value)?{??
  • ??
  • ????????try?{??
  • ????????????log.info("add?to?redisCache?:"+key);??
  • ????????????System.out.println("add?to?rediscache");??
  • ????????????ShardedJedis?jedis?=?pool.getResource();??
  • ????????????jedis.set(key,?(String)value);??
  • ????????????pool.returnResource(jedis);??
  • ????????????return?true;??
  • ????????}?catch?(Exception?e)?{??
  • ????????????e.printStackTrace();??
  • ????????}??
  • ????????return?false;??
  • ????}??
  • ????public?static?void?main(String[]?args)?{??
  • ????????new?RedisCacheManger().setRedisCacheInfo("12345",?"asdfg");??
  • ????}??
  • }??

  • 緩存切面注解:

    [java]? view plain copy print ?
  • @Retention(RetentionPolicy.RUNTIME)??
  • @Target (ElementType.METHOD)??
  • public?@interface ?NeedRedisCached?{}??

  • 緩存切面處理類:

    [java]? view plain copy print ?
  • @Aspect ??
  • public?class?RedisCacheAspect?implements?Ordered?{??
  • ??????
  • ????private?static?Logger?log?=?Logger.getLogger(RedisCacheAspect.class);???
  • ????private?RedisCache?redisCacheManager;??
  • ????private?int?orderValue?=?3;??
  • ????public?RedisCache?getRedisCacheManager()?{??
  • ????????return?redisCacheManager;??
  • ????}??
  • ??
  • ????public?void?setRedisCacheManager(RedisCache?redisCacheManager)?{??
  • ????????this.redisCacheManager?=?redisCacheManager;??
  • ????}??
  • ??
  • ????@Pointcut("@annotation(com.jd.bi.odp.common.cache.core.NeedRedisCached)")??
  • ????public?void?needRedisCached()?{??
  • ??
  • ????}??
  • ??????
  • ????@Around("needRedisCached()?&&?args(filter,..)")??
  • ????public?Object?aroundInvoke(ProceedingJoinPoint?pjp,?QueryFilter?filter)?throws?Throwable?{??
  • ????????log.info("enter?aroundInvoke!!!");??
  • ????????if?(filter.getValue()?==?null)?{??
  • ????????????return?null;??
  • ????????}??
  • ??
  • ????????boolean?cacheEnabled?=?CommonUtil.parseBoolean(HBaseConfig.getProperty("redisCache.enabled"),?false);??
  • ??
  • ????????if?(cacheEnabled)?{??
  • ??????????????
  • ????????????String?md5key?=?MD5Util.getMD5(filter.getValue().toString());??
  • ????????????Object?value?=?redisCacheManager.getRedisCacheInfo(md5key);??
  • ????????????boolean?flag?=?false;??
  • ????????????if?(null?!=?value)?{??
  • ??
  • ????????????????JSONObject?json?=?new?JSONObject(value.toString());??
  • ????????????????return?json;??
  • ????????????}?else?if?("null".equals(value))?{??
  • ????????????????return?null;??
  • ????????????}?else?{?//執行hbase查詢??
  • ????????????????value?=?pjp.proceed();??
  • ????????????????if(null!=value){//此處根據業務邏輯判斷不緩存的條件??
  • ????????????????????}??
  • ????????????????????else{??
  • ????????????????????????flag?=?redisCacheManager.setRedisCacheInfo(md5key,?value.toString());??
  • ??????????????????????????
  • ????????????????????????if(flag)??
  • ????????????????????????????log.info("add?a?cache?success?by?key:?"+md5key);??
  • ????????????????????????else??
  • ????????????????????????????log.warn("add?a?cache?failure?by?key:?"+md5key);??
  • ????????????????????}??
  • ????????????????}??
  • ????????????????return?value;??
  • ????????????}??
  • ????????}?else?{//?執行hbase查詢??
  • ????????????return?pjp.proceed();??
  • ????????}??
  • ????}??
  • ????@Override??
  • ????public?int?getOrder()?{??
  • ????????return?orderValue;??
  • ????}??
  • ????public?int?getOrderValue()?{??
  • ????????return?orderValue;??
  • ????}??
  • ??
  • ????public?void?setOrderValue(int?orderValue)?{??
  • ????????this.orderValue?=?orderValue;??
  • ????}??
  • }??

  • 緩存存在直接返回,不存在的話執行數據源查詢,此處是hbase查詢,并設置緩存。

    ?

    切面配置:

    [html]? view plain copy print ?
  • <!--?redis緩存運行切面?-->??
  • <bean?id="RedisCacheAspect"??
  • ????class="com.cr.common.cache.core.RedisCacheAspect">??
  • ????<property?name="orderValue"?value="3"?/>??
  • ????<property?name="redisCacheManager"?ref="redisCacheManager"/>??
  • </bean>??
  • <!--?切面申明配置-->??
  • <aop:aspectj-autoproxy>??
  • ????<aop:include?name="RedisCacheAspect"?/>??
  • </aop:aspectj-autoproxy>??

  • 此時,前端web頁面用戶的訪問觸發的action如果滿足條件,則會進入切面方法處理,完成redis緩存的使用。

    http://blog.csdn.net/cuirong1986/article/details/8213159?utm_source=tuicool&utm_medium=referral

    轉載于:https://my.oschina.net/xiaominmin/blog/1597205

    總結

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

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