亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

@CacheEvict + redis實現(xiàn)批量刪除緩存

 更新時間:2021年10月12日 14:57:19   作者:llllllllll4er5ty  
這篇文章主要介紹了@CacheEvict + redis實現(xiàn)批量刪除緩存方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

@CacheEvict + redis批量刪除緩存

一、@Cacheable注解

添加緩存。

    /**
     * @Cacheable
     * 將方法的運行結(jié)果進行緩存;以后再要相同的數(shù)據(jù),直接從緩存中獲取,不用調(diào)用方法;
     * CacheManager管理多個Cache組件,對緩存的真正CRUD操作在Cache組件中,每一個緩存組件有自己唯一一個名字;
     *
     *
     * 原理:
     *   1、自動配置類;CacheAutoConfiguration
     *   2、緩存的配置類
     *   org.springframework.boot.autoconfigure.cache.GenericCacheConfiguration
     *   org.springframework.boot.autoconfigure.cache.JCacheCacheConfiguration
     *   org.springframework.boot.autoconfigure.cache.EhCacheCacheConfiguration
     *   org.springframework.boot.autoconfigure.cache.HazelcastCacheConfiguration
     *   org.springframework.boot.autoconfigure.cache.InfinispanCacheConfiguration
     *   org.springframework.boot.autoconfigure.cache.CouchbaseCacheConfiguration
     *   org.springframework.boot.autoconfigure.cache.RedisCacheConfiguration
     *   org.springframework.boot.autoconfigure.cache.CaffeineCacheConfiguration
     *   org.springframework.boot.autoconfigure.cache.GuavaCacheConfiguration
     *   org.springframework.boot.autoconfigure.cache.SimpleCacheConfiguration【默認】
     *   org.springframework.boot.autoconfigure.cache.NoOpCacheConfiguration
     *   3、哪個配置類默認生效:SimpleCacheConfiguration;
     *
     *   4、給容器中注冊了一個CacheManager:ConcurrentMapCacheManager
     *   5、可以獲取和創(chuàng)建ConcurrentMapCache類型的緩存組件;他的作用將數(shù)據(jù)保存在ConcurrentMap中;
     *
     *   運行流程:
     *   @Cacheable:
     *   1、方法運行之前,先去查詢Cache(緩存組件),按照cacheNames指定的名字獲取;
     *      (CacheManager先獲取相應(yīng)的緩存),第一次獲取緩存如果沒有Cache組件會自動創(chuàng)建。
     *   2、去Cache中查找緩存的內(nèi)容,使用一個key,默認就是方法的參數(shù);
     *      key是按照某種策略生成的;默認是使用keyGenerator生成的,默認使用SimpleKeyGenerator生成key;
     *          SimpleKeyGenerator生成key的默認策略;
     *                  如果沒有參數(shù);key=new SimpleKey();
     *                  如果有一個參數(shù):key=參數(shù)的值
     *                  如果有多個參數(shù):key=new SimpleKey(params);
     *   3、沒有查到緩存就調(diào)用目標(biāo)方法;
     *   4、將目標(biāo)方法返回的結(jié)果,放進緩存中
     *
     *   @Cacheable標(biāo)注的方法執(zhí)行之前先來檢查緩存中有沒有這個數(shù)據(jù),默認按照參數(shù)的值作為key去查詢緩存,
     *   如果沒有就運行方法并將結(jié)果放入緩存;以后再來調(diào)用就可以直接使用緩存中的數(shù)據(jù);
     *
     *   核心:
     *      1)、使用CacheManager【ConcurrentMapCacheManager】按照名字得到Cache【ConcurrentMapCache】組件
     *      2)、key使用keyGenerator生成的,默認是SimpleKeyGenerator
     *
     *
     *   幾個屬性:
     *      cacheNames/value:指定緩存組件的名字;將方法的返回結(jié)果放在哪個緩存中,是數(shù)組的方式,可以指定多個緩存;
     *
     *      key:緩存數(shù)據(jù)使用的key;可以用它來指定。默認是使用方法參數(shù)的值  1-方法的返回值
     *              編寫SpEL; #i d;參數(shù)id的值   #a0  #p0  #root.args[0]
     *              getEmp[2]
     *
     *      keyGenerator:key的生成器;可以自己指定key的生成器的組件id
     *              key/keyGenerator:二選一使用;
     *
     *
     *      cacheManager:指定緩存管理器;或者cacheResolver指定獲取解析器
     *
     *      condition:指定符合條件的情況下才緩存;
     *              ,condition = "#id>0"
     *          condition = "#a0>1":第一個參數(shù)的值》1的時候才進行緩存
     *
     *      unless:否定緩存;當(dāng)unless指定的條件為true,方法的返回值就不會被緩存;可以獲取到結(jié)果進行判斷
     *              unless = "#result == null"
     *              unless = "#a0==2":如果第一個參數(shù)的值是2,結(jié)果不緩存;
     *      sync:是否使用異步模式
     *
     */

二、@CacheEvict注解

清除緩存。

cacheNames/value: 指定緩存組件的名字;將方法的返回結(jié)果放在哪個緩存中,是數(shù)組的方式,可以指定多個緩存;
key 緩存數(shù)據(jù)使用的key
allEntries 是否清除這個緩存中所有的數(shù)據(jù)。true:是;false:不是
beforeInvocation 緩存的清除是否在方法之前執(zhí)行,默認代表緩存清除操作是在方法執(zhí)行之后執(zhí)行;如果出現(xiàn)異常緩存就不會清除。true:是;false:不是

三、批量刪除緩存

現(xiàn)實應(yīng)用中,某些緩存都有相同的前綴或者后綴,數(shù)據(jù)庫更新時,需要刪除某一類型(也就是相同前綴)的緩存。

而@CacheEvict只能單個刪除key,不支持模糊匹配刪除。

解決辦法:使用redis + @CacheEvict解決。

@CacheEvict實際上是調(diào)用RedisCache的evict方法刪除緩存的。下面為RedisCache的部分代碼,可以看到,evict方法是不支持模糊匹配的,而clear方法是支持模糊匹配的。

  
    /*
	 * (non-Javadoc)
	 * @see org.springframework.cache.Cache#evict(java.lang.Object)
	 */
	@Override
	public void evict(Object key) {
		cacheWriter.remove(name, createAndConvertCacheKey(key));
	}
 
	/*
	 * (non-Javadoc)
	 * @see org.springframework.cache.Cache#clear()
	 */
	@Override
	public void clear() {
 
		byte[] pattern = conversionService.convert(createCacheKey("*"), byte[].class);
		cacheWriter.clean(name, pattern);
	}

所以,只需重寫RedisCache的evict方法就可以解決模糊匹配刪除的問題。

四、代碼

4.1 自定義RedisCache:

public class CustomizedRedisCache extends RedisCache {
    private static final String WILD_CARD = "*"; 
    private final String name;
    private final RedisCacheWriter cacheWriter;
    private final ConversionService conversionService; 
    protected CustomizedRedisCache(String name, RedisCacheWriter cacheWriter, RedisCacheConfiguration cacheConfig) {
        super(name, cacheWriter, cacheConfig);
        this.name = name;
        this.cacheWriter = cacheWriter;
        this.conversionService = cacheConfig.getConversionService();
    }
 
    @Override
    public void evict(Object key) {
        if (key instanceof String) {
            String keyString = key.toString();
            if (keyString.endsWith(WILD_CARD)) {
                evictLikeSuffix(keyString);
                return;
            }
            if (keyString.startsWith(WILD_CARD)) {
                evictLikePrefix(keyString);
                return;
            }
        }
        super.evict(key);
    }
 
    /**
     * 前綴匹配
     *
     * @param key
     */
    public void evictLikePrefix(String key) {
        byte[] pattern = this.conversionService.convert(this.createCacheKey(key), byte[].class);
        this.cacheWriter.clean(this.name, pattern);
    }
 
    /**
     * 后綴匹配
     *
     * @param key
     */
    public void evictLikeSuffix(String key) {
        byte[] pattern = this.conversionService.convert(this.createCacheKey(key), byte[].class);
        this.cacheWriter.clean(this.name, pattern);
    }  
}

4.2 重寫RedisCacheManager,使用自定義的RedisCache:

public class CustomizedRedisCacheManager extends RedisCacheManager { 
    private final RedisCacheWriter cacheWriter;
    private final RedisCacheConfiguration defaultCacheConfig;
    private final Map<String, RedisCacheConfiguration> initialCaches = new LinkedHashMap<>();
    private boolean enableTransactions; 
 
    public CustomizedRedisCacheManager(RedisCacheWriter cacheWriter, RedisCacheConfiguration defaultCacheConfiguration) {
        super(cacheWriter, defaultCacheConfiguration);
        this.cacheWriter = cacheWriter;
        this.defaultCacheConfig = defaultCacheConfiguration;
    }
 
    public CustomizedRedisCacheManager(RedisCacheWriter cacheWriter, RedisCacheConfiguration defaultCacheConfiguration, String... initialCacheNames) {
        super(cacheWriter, defaultCacheConfiguration, initialCacheNames);
        this.cacheWriter = cacheWriter;
        this.defaultCacheConfig = defaultCacheConfiguration;
    }
 
    public CustomizedRedisCacheManager(RedisCacheWriter cacheWriter, RedisCacheConfiguration defaultCacheConfiguration, boolean allowInFlightCacheCreation, String... initialCacheNames) {
        super(cacheWriter, defaultCacheConfiguration, allowInFlightCacheCreation, initialCacheNames);
        this.cacheWriter = cacheWriter;
        this.defaultCacheConfig = defaultCacheConfiguration;
    }
 
    public CustomizedRedisCacheManager(RedisCacheWriter cacheWriter, RedisCacheConfiguration defaultCacheConfiguration, Map<String, RedisCacheConfiguration> initialCacheConfigurations) {
        super(cacheWriter, defaultCacheConfiguration, initialCacheConfigurations);
        this.cacheWriter = cacheWriter;
        this.defaultCacheConfig = defaultCacheConfiguration;
    }
 
    public CustomizedRedisCacheManager(RedisCacheWriter cacheWriter, RedisCacheConfiguration defaultCacheConfiguration, Map<String, RedisCacheConfiguration> initialCacheConfigurations, boolean allowInFlightCacheCreation) {
        super(cacheWriter, defaultCacheConfiguration, initialCacheConfigurations, allowInFlightCacheCreation);
        this.cacheWriter = cacheWriter;
        this.defaultCacheConfig = defaultCacheConfiguration;
    }
 
    /**
     * 這個構(gòu)造方法最重要
     **/
    public CustomizedRedisCacheManager(RedisConnectionFactory redisConnectionFactory, RedisCacheConfiguration cacheConfiguration) {
        this(RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory),cacheConfiguration);
    }
 
    /**
     * 覆蓋父類創(chuàng)建RedisCache
     */
    @Override
    protected RedisCache createRedisCache(String name, @Nullable RedisCacheConfiguration cacheConfig) {
        return new CustomizedRedisCache(name, cacheWriter, cacheConfig != null ? cacheConfig : defaultCacheConfig);
    }
 
    @Override
    public Map<String, RedisCacheConfiguration> getCacheConfigurations() {
        Map<String, RedisCacheConfiguration> configurationMap = new HashMap<>(getCacheNames().size());
        getCacheNames().forEach(it -> {
            RedisCache cache = CustomizedRedisCache.class.cast(lookupCache(it));
            configurationMap.put(it, cache != null ? cache.getCacheConfiguration() : null);
        });
        return Collections.unmodifiableMap(configurationMap);
    }
}

4.3 在RedisTemplateConfig中使用自定義的CacheManager

@Bean
 public CacheManager oneDayCacheManager(RedisConnectionFactory factory) {
  RedisSerializer<String> redisSerializer = new StringRedisSerializer();
  Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
 
  //解決查詢緩存轉(zhuǎn)換異常的問題
  ObjectMapper om = new ObjectMapper();
  om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
  om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
  jackson2JsonRedisSerializer.setObjectMapper(om);
 
  // 配置序列化(解決亂碼的問題)
  RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig()
    // 1天緩存過期
    .entryTtl(Duration.ofDays(1))
    .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(redisSerializer))
    .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer))
    .computePrefixWith(name -> name + ":")
    .disableCachingNullValues();
  return new CustomizedRedisCacheManager(factory, config);
 }

4.4 在代碼方法上使用@CacheEvict模糊匹配刪除

@Cacheable(value = "current_group", cacheManager = "oneDayCacheManager",
            key = "#currentAttendanceGroup.getId() + ':' + args[1]", unless = "#result eq null")
    public String getCacheAttendanceId(CurrentAttendanceGroupDO currentAttendanceGroup, String dateStr) {
        // 方法體
    } 
 
    @CacheEvict(value = "current_group", key = "#currentAttendanceGroup.getId() + ':' + '*'", beforeInvocation = true)
    public void deleteCacheAttendanceId(CurrentAttendanceGroupDO currentAttendanceGroup) { 
    }

注意:如果RedisTemplateConfig中有多個CacheManager,可以使用@Primary注解標(biāo)注默認生效的CacheManager

@CacheEvict清除指定下所有緩存

@CacheEvict(cacheNames = "parts:grid",allEntries = true) 

此注解會清除part:grid下所有緩存

@CacheEvict要求指定一個或多個緩存,使之都受影響。

此外,還提供了一個額外的參數(shù)allEntries 。表示是否需要清除緩存中的所有元素。

默認為false,表示不需要。當(dāng)指定了allEntries為true時,Spring Cache將忽略指定的key。

有的時候我們需要Cache一下清除所有的元素。

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論