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

Spring cache整合redis代碼實(shí)例

 更新時(shí)間:2020年04月28日 11:50:17   作者:Terry  
這篇文章主要介紹了Spring cache整合redis代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

Spring-Cache是Spring3.1引入的基于注解的緩存技術(shù),本質(zhì)上它并不是一個(gè)具體的緩存實(shí)現(xiàn),而是一個(gè)對(duì)緩存使用的抽象,通過(guò)Spring AOP技術(shù),在原有的代碼上添加少量的注解來(lái)實(shí)現(xiàn)將這個(gè)方法轉(zhuǎn)成緩存方法的效果。

本來(lái)想來(lái)個(gè)分析源碼,奈何水平有限,先從實(shí)戰(zhàn)搞起。

先引入依賴:

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-data-redis</artifactId>
  <version>2.1.6.RELEASE</version>
</dependency>
<dependency>
  <groupId>redis.clients</groupId>
  <artifactId>jedis</artifactId>
  <version>2.9.3</version>
</dependency>

redis配置:

server:
 port: 8000

spring:
 redis:
  host: 23.95.x.x
  port: 6379
  timeout: 20s
  database: 0
  jedis:
   pool:
    max-active: 5
    max-idle: 3
    max-wait: 5s
  password: testtest

配置類:

package me.yanand.config;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import java.time.Duration;
@Configuration
@EnableCaching
public class RedisConfig{
 
  private Duration timeOut = Duration.ofMinutes(30);
  @Bean
  public RedisCacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {
    RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig()
        //設(shè)置緩存超時(shí)時(shí)間 30分鐘
        .entryTtl(timeOut)
        //設(shè)置key序列化方式
        .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer()))
        //設(shè)置value序列化方式
        .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer()))
        .disableCachingNullValues();
    return RedisCacheManager.builder(redisConnectionFactory).cacheDefaults(config).transactionAware().build();
  }
}

主要看@EnableCaching注解,這個(gè)注解引入了@Import(CachingConfigurationSelector.class),通過(guò)CachingConfigurationSelector把代理創(chuàng)建類、CacheInterceptor、CacheOperationSource、BeanFactoryCacheOperationSourceAdvisor注入到容器,spring通過(guò)CacheInterceptor攔截器攔截相關(guān)帶有@Cacheable、@CacheEvict、@CachePut注解的方法并執(zhí)行相關(guān)緩存操作。

CacheInterceptor相關(guān)源碼:

@Nullable
private Object execute(final CacheOperationInvoker invoker, Method method, CacheOperationContexts contexts) {
 if (contexts.isSynchronized()) {
  CacheOperationContext context = contexts.get(CacheableOperation.class).iterator().next();
  //滿足條件執(zhí)行
  if (isConditionPassing(context, CacheOperationExpressionEvaluator.NO_RESULT)) {
   Object key = generateKey(context, CacheOperationExpressionEvaluator.NO_RESULT);
   Cache cache = context.getCaches().iterator().next();
   try {
     //這里主要看RedisCache的get方法
    return wrapCacheValue(method, cache.get(key, () -> unwrapReturnValue(invokeOperation(invoker))));
   }
   catch (Cache.ValueRetrievalException ex) {
    // The invoker wraps any Throwable in a ThrowableWrapper instance so we
    // can just make sure that one bubbles up the stack.
    throw (CacheOperationInvoker.ThrowableWrapper) ex.getCause();
   }
  }
  else {
   //不滿足直接執(zhí)行相關(guān)方法
   return invokeOperation(invoker);
  }
 }
 ...省略
}

RedisCache相關(guān)代碼:

public synchronized <T> T get(Object key, Callable<T> valueLoader) {
  ValueWrapper result = get(key);
        //緩存中有值則返回
  if (result != null) {
   return (T) result.get();
  }
        //緩存中不存在則執(zhí)行相關(guān)方法
  T value = valueFromLoader(key, valueLoader);
  put(key, value);
  return value;
 }

注解使用:

package me.yanand.dao;
import me.yanand.pojo.User;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;
@Component
public class UserDao {
  @Cacheable(cacheNames = "users",key = "#root.targetClass+#name", unless = "#result eq null")
  public User getUser(String name){
    return new User("張三",30);
  }
  @CacheEvict(cacheNames = "users", key = "#root.targetClass+#name")
  public void delUser(String name){
  }
}

測(cè)試:

通過(guò)postman觸發(fā)相關(guān)方法,現(xiàn)在我們連上redis查看緩存寫(xiě)入情況

這里我們看到key已經(jīng)寫(xiě)入,過(guò)期時(shí)間也存在

現(xiàn)在我們刪除緩存

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

相關(guān)文章

  • Java狀態(tài)設(shè)計(jì)模式實(shí)現(xiàn)對(duì)象狀態(tài)轉(zhuǎn)換的優(yōu)雅方式

    Java狀態(tài)設(shè)計(jì)模式實(shí)現(xiàn)對(duì)象狀態(tài)轉(zhuǎn)換的優(yōu)雅方式

    Java狀態(tài)設(shè)計(jì)模式通過(guò)將對(duì)象的行為和狀態(tài)分離,使對(duì)象能夠根據(jù)不同的狀態(tài)進(jìn)行不同的行為操作。它通過(guò)將狀態(tài)抽象成一個(gè)獨(dú)立的類來(lái)實(shí)現(xiàn)對(duì)狀態(tài)的封裝,從而簡(jiǎn)化了復(fù)雜的條件判斷和狀態(tài)轉(zhuǎn)換
    2023-04-04
  • java通過(guò)客戶端訪問(wèn)服務(wù)器webservice的方法

    java通過(guò)客戶端訪問(wèn)服務(wù)器webservice的方法

    這篇文章主要介紹了java通過(guò)客戶端訪問(wèn)服務(wù)器webservice的方法,涉及java創(chuàng)建與調(diào)用webservice的相關(guān)技巧,需要的朋友可以參考下
    2016-08-08
  • Mybatis-Plus和Mybatis的區(qū)別詳解

    Mybatis-Plus和Mybatis的區(qū)別詳解

    這篇文章主要介紹了Mybatis-Plus和Mybatis的區(qū)別,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-08-08
  • Dubbo3和Spring?Boot整合過(guò)程源碼解析

    Dubbo3和Spring?Boot整合過(guò)程源碼解析

    Dubbo首先是提供了一個(gè)單獨(dú)的模塊來(lái)和Spring Boot做整合,利用 Spring Boot自動(dòng)裝配的功能,配置了一堆自動(dòng)裝配的組件,本文介紹Dubbo3和Spring?Boot整合過(guò)程,需要的朋友一起看看吧
    2023-08-08
  • Java設(shè)計(jì)模式之抽象工廠模式簡(jiǎn)析

    Java設(shè)計(jì)模式之抽象工廠模式簡(jiǎn)析

    這篇文章主要介紹了Java設(shè)計(jì)模式之抽象工廠模式簡(jiǎn)析, 抽象工廠模式是工廠方法模式的升級(jí)版本,他用來(lái)創(chuàng)建一組相關(guān)或者相互依賴的對(duì)象,他與工廠方法模式的區(qū)別就在于,工廠方法模式針對(duì)的是一個(gè)產(chǎn)品等級(jí)結(jié)構(gòu),需要的朋友可以參考下
    2023-12-12
  • MyBatis批量插入的五種方式

    MyBatis批量插入的五種方式

    這篇文章主要介紹了MyBatis批量插入的五種方式,每種方式結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-11-11
  • 劍指Offer之Java算法習(xí)題精講鏈表專項(xiàng)訓(xùn)練

    劍指Offer之Java算法習(xí)題精講鏈表專項(xiàng)訓(xùn)練

    跟著思路走,之后從簡(jiǎn)單題入手,反復(fù)去看,做過(guò)之后可能會(huì)忘記,之后再做一次,記不住就反復(fù)做,反復(fù)尋求思路和規(guī)律,慢慢積累就會(huì)發(fā)現(xiàn)質(zhì)的變化
    2022-03-03
  • Java內(nèi)存模型知識(shí)詳解

    Java內(nèi)存模型知識(shí)詳解

    這篇文章主要介紹了Java內(nèi)存模型知識(shí)詳解,文中通過(guò)對(duì)內(nèi)存訪問(wèn)時(shí)的交互關(guān)系圖解介紹的十分詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07
  • java 中線程等待與通知的實(shí)現(xiàn)

    java 中線程等待與通知的實(shí)現(xiàn)

    這篇文章主要介紹了java 中線程等待與通知的實(shí)現(xiàn)的相關(guān)資料,希望通過(guò)本文能幫助到大家,讓大家掌握這樣的功能,需要的朋友可以參考下
    2017-09-09
  • 詳解Java中的final關(guān)鍵字的使用

    詳解Java中的final關(guān)鍵字的使用

    這篇文章主要介紹了詳解Java中的final關(guān)鍵字的使用,是Java入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下
    2015-07-07

最新評(píng)論