欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解Spring極速集成注解redis實(shí)錄

 更新時(shí)間:2017年07月06日 08:29:25   作者:Orson  
這篇文章主要介紹了詳解Spring極速集成注解redis實(shí)錄,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

Redis 做為基于內(nèi)存的 Key-Value 數(shù)據(jù)庫,用來做緩存服務(wù)器性價(jià)比相當(dāng)高。

官方推出的面向 Java 的 Client Jedis,提供了很多接口和方法,可以讓 Java 操作使用 Redis。

Spring Data Redis 為 Spring 團(tuán)隊(duì)對 Jedis 進(jìn)行了封裝,集成 Jedis 的一些命令和方法。

本文重點(diǎn)描述集成過程,能讓你迅速的通過 spring-data-redis 將 redis 集成到 spring 項(xiàng)目中,畢竟大家都忙的。

1. 添加項(xiàng)目依賴

    <!--redis 緩存-->
    <dependency>
      <groupId>org.springframework.data</groupId>
      <artifactId>spring-data-redis</artifactId>
      <version>1.8.4.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>redis.clients</groupId>
      <artifactId>jedis</artifactId>
      <version>2.9.0</version>
    </dependency>

2. 添加 spring-redis-context 配置

<?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"
    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.xsd">
  <description>redis 相關(guān)類 Spring 托管</description>

  <!--載入 redis 配置文件-->
  <context:property-placeholder location="classpath:redis.properties" ignore-unresolvable="true"/>

  <!-- 配置 JedisPoolConfig 實(shí)例 -->
  <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
    <property name="maxIdle" value="${redis.maxIdle}"/>
    <property name="maxTotal" value="${redis.maxActive}"/>
    <property name="maxWaitMillis" value="${redis.maxWait}"/>
    <property name="testOnBorrow" value="${redis.testOnBorrow}"/>
  </bean>

  <!-- 配置JedisConnectionFactory -->
  <bean id="jedisConnectionFactory" 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.pass}"/>
    <property name="database" value="${redis.dbIndex}"/>
    <property name="poolConfig" ref="poolConfig"/>
  </bean>

  <!-- 配置RedisTemplate -->
  <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
    <property name="connectionFactory" ref="jedisConnectionFactory"/>
  </bean>

  <!-- 配置RedisCacheManager -->
  <bean id="redisCacheManager" class="org.springframework.data.redis.cache.RedisCacheManager">
    <constructor-arg name="redisOperations" ref="redisTemplate"/>
    <property name="defaultExpiration" value="${redis.expiration}"/>
  </bean>

  <!-- 配置RedisCacheConfig -->
  <bean id="redisCacheConfig" class="com.rambo.sdh.common.util.RedisCacheConfig">
    <constructor-arg ref="jedisConnectionFactory"/>
    <constructor-arg ref="redisTemplate"/>
    <constructor-arg ref="redisCacheManager"/>
  </bean>
</beans>

JedisConnectionFactory 為 Jedis 連接工廠,配置由單獨(dú)抽象的 JedisPoolConfig 提供。

如果你熟悉 Spring 的 JdbcTemplate 對象的話,這里大概能猜出來 RedisTemplate 的作用,RedisTemplate 對 RedisConnection 進(jìn)行了封裝。

提供連接管理,序列化等功能,它對 Redis 的交互進(jìn)行了更高層次的抽象,極大的方便和簡化了 Redis 的操作。

RedisCacheManager 做為 redis 統(tǒng)一的調(diào)度和管理者,有興趣可以反編譯源碼看看。

繼承自 org.springframework.cache.transaction.AbstractTransactionSupportingCacheManager 并實(shí)現(xiàn) org.springframework.cache.CacheManager。

3. 添加 redis.properties

#============================#
#==== Redis settings ====#
#============================#
#redis 服務(wù)器 IP
redis.host=127.0.0.1

#redis 服務(wù)器端口
redis.port=6379

#redis 密碼
redis.pass=redis#2017

#redis 支持16個(gè)數(shù)據(jù)庫(相當(dāng)于不同用戶)可以使不同的應(yīng)用程序數(shù)據(jù)彼此分開同時(shí)又存儲(chǔ)在相同的實(shí)例上
redis.dbIndex=0

#redis 緩存數(shù)據(jù)過期時(shí)間單位秒
redis.expiration=3000

#控制一個(gè) pool 最多有多少個(gè)狀態(tài)為 idle 的jedis實(shí)例
redis.maxIdle=300

#控制一個(gè) pool 可分配多少個(gè)jedis實(shí)例
redis.maxActive=600

#當(dāng)borrow一個(gè)jedis實(shí)例時(shí),最大的等待時(shí)間,如果超過等待時(shí)間,則直接拋出JedisConnectionException;
redis.maxWait=1000

#在borrow一個(gè)jedis實(shí)例時(shí),是否提前進(jìn)行alidate操作;如果為true,則得到的jedis實(shí)例均是可用的;
redis.testOnBorrow=true

當(dāng)然配置文件你也可以硬編碼到程序中,只是在參數(shù)發(fā)生改變的時(shí)候比較痛苦一點(diǎn)而已。

其中大部分配置項(xiàng)都是圍繞著 jedisPool ,如果你對數(shù)據(jù)庫連接池比較熟,你會(huì)發(fā)現(xiàn)它倆的配置項(xiàng)有點(diǎn)相似。

當(dāng)系統(tǒng) redis 遇到問題出現(xiàn)故障時(shí),理解這里的選項(xiàng)是個(gè)不錯(cuò)的選擇。

4. 編寫自定義 redis 配置類

@Configuration
@EnableCaching
public class RedisCacheConfig extends CachingConfigurerSupport {
  protected final static Logger log = LoggerFactory.getLogger(RedisCacheConfig.class);

  private volatile JedisConnectionFactory mJedisConnectionFactory;
  private volatile RedisTemplate<String, String> mRedisTemplate;
  private volatile RedisCacheManager mRedisCacheManager;

  public RedisCacheConfig() {
    super();
  }

  public RedisCacheConfig(JedisConnectionFactory mJedisConnectionFactory, RedisTemplate<String, String> mRedisTemplate, RedisCacheManager mRedisCacheManager) {
    super();
    this.mJedisConnectionFactory = mJedisConnectionFactory;
    this.mRedisTemplate = mRedisTemplate;
    this.mRedisCacheManager = mRedisCacheManager;
  }

  public JedisConnectionFactory redisConnectionFactory() {
    return mJedisConnectionFactory;
  }

  public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory cf) {
    return mRedisTemplate;
  }

  public CacheManager cacheManager(RedisTemplate<?, ?> redisTemplate) {
    return mRedisCacheManager;
  }

  @Bean
  public KeyGenerator keyGenerator() {
    return new KeyGenerator() {
      @Override
      public Object generate(Object o, Method method, Object... objects) {
        StringBuilder sb = new StringBuilder();
        sb.append(o.getClass().getName());
        sb.append(method.getName());
        for (Object obj : objects) {
          sb.append(obj.toString());
        }
        return sb.toString();
      }
    };
  }
}

該配置類繼承自 org.springframework.cache.annotation.CachingConfigurerSupport 并實(shí)現(xiàn) org.springframework.cache.annotation.CachingConfigurer 的方法。

通俗一點(diǎn),該類告訴 spring 當(dāng)前使用的緩存服務(wù)為 redis 并自定義了緩存 key 生成的規(guī)則。

5. 在你喜歡的地方進(jìn)行注解緩存

緩存一般使用在服務(wù)層,在你想緩存的方法上面添加相應(yīng)的注解即可,下面三個(gè)緩存的注解你得掌握。

@Cacheable  spring 會(huì)在其被調(diào)用后將返回值緩存起來,以保證下次利用同樣的參數(shù)來執(zhí)行該方法時(shí)可以直接從緩存中獲取結(jié)果,而不需要再次執(zhí)行該方法。

@CachePut  標(biāo)注的方法在執(zhí)行前不會(huì)去檢查緩存中是否存在之前執(zhí)行過的結(jié)果,而是每次都會(huì)執(zhí)行該方法,并將執(zhí)行結(jié)果以鍵值對的形式存入指定的緩存中。

@CacheEvict 用來標(biāo)注在需要清除緩存元素的方法或類上的。

當(dāng)然這些注解里面還有很多其他的屬性配置,配合 spring-el 表達(dá)式能做的事情還有很多,大概只有你想不到,沒有做不到。

在業(yè)務(wù)規(guī)則比較復(fù)雜的情況下,緩存 key 的設(shè)計(jì)相當(dāng)重要,設(shè)計(jì)出色可以使你的應(yīng)用飛起來。

整個(gè)集成工作就結(jié)束了,是不是很簡單,上述算是 redis 的冰山一角,還有很多像 redis 路由/分布式/集群....,有機(jī)會(huì)實(shí)踐慢慢體會(huì)。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • SpringBoot整合Swagger Api自動(dòng)生成文檔的實(shí)現(xiàn)

    SpringBoot整合Swagger Api自動(dòng)生成文檔的實(shí)現(xiàn)

    本文主要介紹了SpringBoot整合Swagger Api自動(dòng)生成文檔的實(shí),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-06-06
  • icePDF去水印的方法(推薦)

    icePDF去水印的方法(推薦)

    下面小編就為大家?guī)硪黄猧cePDF去水印的方法(推薦)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2016-12-12
  • 一文了解jJava中的加密與安全

    一文了解jJava中的加密與安全

    常見的編碼有ASCII碼、Unicode編碼。最簡單的編碼是直接給每個(gè)字符指定一個(gè)若干字節(jié)表示的整數(shù),復(fù)雜一點(diǎn)的編碼就需要根據(jù)已有的編碼推算出來。本文將為大家詳細(xì)講講Java重點(diǎn)加密與安全,感興趣的可以了解一下
    2022-07-07
  • java制作簡單的坦克大戰(zhàn)

    java制作簡單的坦克大戰(zhàn)

    坦克大戰(zhàn)是我們小時(shí)候玩紅白機(jī)時(shí)代的經(jīng)典游戲,看到有不少小伙伴都使用各種語言實(shí)現(xiàn)了一下,手癢癢,也使用java做的一個(gè)比較簡單的坦克大戰(zhàn),主要面向于學(xué)過Java的人群,與學(xué)了一段時(shí)間的人,有利于面向?qū)ο笏枷氲奶岣撸扑]給大家。
    2015-03-03
  • MybatisPlus EntityWrapper如何自定義SQL

    MybatisPlus EntityWrapper如何自定義SQL

    這篇文章主要介紹了MybatisPlus EntityWrapper如何自定義SQL,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • Spring使用RestTemplate和Junit單元測試的注意事項(xiàng)

    Spring使用RestTemplate和Junit單元測試的注意事項(xiàng)

    這篇文章主要介紹了Spring使用RestTemplate和Junit單元測試的注意事項(xiàng),具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-10-10
  • Java用BigDecimal類解決Double類型精度丟失的問題

    Java用BigDecimal類解決Double類型精度丟失的問題

    這篇文章主要介紹了Java用BigDecimal類解決Double類型精度丟失的問題,幫助大家更好的理解和使用Java,感興趣的朋友可以了解下
    2020-12-12
  • Java如何使用httpclient檢測url狀態(tài)及鏈接是否能打開

    Java如何使用httpclient檢測url狀態(tài)及鏈接是否能打開

    這篇文章主要介紹了Java如何使用httpclient檢測url狀態(tài)及鏈接是否能打開,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • @JsonSerialize序列化注解的使用

    @JsonSerialize序列化注解的使用

    這篇文章主要介紹了@JsonSerialize序列化注解的使用方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-08-08
  • JAVA中 Spring定時(shí)器的兩種實(shí)現(xiàn)方式

    JAVA中 Spring定時(shí)器的兩種實(shí)現(xiàn)方式

    本文向您介紹Spring定時(shí)器的兩種實(shí)現(xiàn)方式,包括Java Timer定時(shí)和Quartz定時(shí)器,兩種Spring定時(shí)器的實(shí)現(xiàn)方式各有優(yōu)點(diǎn),可結(jié)合具體項(xiàng)目考慮是否采用。
    2015-09-09

最新評論