1. 程式人生 > 程式設計 >Spring cache整合redis程式碼例項

Spring cache整合redis程式碼例項

Spring-Cache是Spring3.1引入的基於註解的快取技術,本質上它並不是一個具體的快取實現,而是一個對快取使用的抽象,通過Spring AOP技術,在原有的程式碼上新增少量的註解來實現將這個方法轉成快取方法的效果。

本來想來個分析原始碼,奈何水平有限,先從實戰搞起。

先引入依賴:

<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()
        //設定快取超時時間 30分鐘
        .entryTtl(timeOut)
        //設定key序列化方式
        .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer()))
        //設定value序列化方式
        .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer()))
        .disableCachingNullValues();
    return RedisCacheManager.builder(redisConnectionFactory).cacheDefaults(config).transactionAware().build();
  }
}

主要看@EnableCaching註解,這個註解引入了@Import(CachingConfigurationSelector.class),通過CachingConfigurationSelector把代理建立類、CacheInterceptor、CacheOperationSource、BeanFactoryCacheOperationSourceAdvisor注入到容器,spring通過CacheInterceptor攔截器攔截相關帶有@Cacheable、@CacheEvict、@CachePut註解的方法並執行相關快取操作。

CacheInterceptor相關原始碼:

@Nullable
private Object execute(final CacheOperationInvoker invoker,Method method,CacheOperationContexts contexts) {
 if (contexts.isSynchronized()) {
  CacheOperationContext context = contexts.get(CacheableOperation.class).iterator().next();
  //滿足條件執行
  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 {
   //不滿足直接執行相關方法
   return invokeOperation(invoker);
  }
 }
 ...省略
}

RedisCache相關程式碼:

public synchronized <T> T get(Object key,Callable<T> valueLoader) {
  ValueWrapper result = get(key);
        //快取中有值則返回
  if (result != null) {
   return (T) result.get();
  }
        //快取中不存在則執行相關方法
  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){
  }
}

測試:

Spring cache整合redis程式碼例項

通過postman觸發相關方法,現在我們連上redis檢視快取寫入情況

Spring cache整合redis程式碼例項

這裡我們看到key已經寫入,過期時間也存在

現在我們刪除快取

Spring cache整合redis程式碼例項

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。