Skip to content

Latest commit

 

History

History
86 lines (62 loc) · 3.14 KB

README.md

File metadata and controls

86 lines (62 loc) · 3.14 KB

spring-enhanced-cache project

CI License Maven Central

Overview

This project aims to enhance springboot cache without code invasion in high concurrency scenarios.

Features

  • Support to evict cache again in a delay time by @CacheEvict
  • Provide retry mechanism when eviction of cache failed
  • Random cache TTL in a specific range

Usage

Add dependency

Latest version: Github release

<dependency>
    <groupId>cn.howaric.cache</groupId>
    <artifactId>spring-boot-starter-cache-enhancer</artifactId>
    <version>${latest.version}</version>
</dependency>

spring-boot-starter-cache won't be needed when this dependency is added in your project.

Add a cache implementation, here uses redis as an example.

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

You can define your own CacheManager, or use the default CacheManager which created by spring cache AutoConfiguration.

Setup configuration

With spring-enhanced-cache, there will be an EnhancedCacheManager created and injected as a spring bean automaticly. The default name of the EnhancedCacheManager is cn.howaric.cache.enhancer.EnhancedCacheManager.ENHANCED_CACHE_MANAGER, it can also be specified by spring.cache.enhanced.enhancedCacheManagerName in spring application.yml.

spring:
  cache:
    type: redis
    redis:
      time-to-live: 10000 # time unit is millis
    enhancer:
      # enhanced-cache-manager-name: "customEnhancedCacheManager" # name of the cache manager
      delay-time: 2000 # means deleting cache eviction again after 2s, default is 5s

By specifying this enhanced CacheManager in @CacheEvict annotation, it will trigger a delayed eviction of the cache in the specific delay time.

@CacheEvict(key = "#p0.username", cacheManager = EnhancedCacheManager.ENHANCED_CACHE_MANAGER)
public void updateUser(User user) {
  //update user
}

Troubleshooting

  1. How to make sure the EnhancedCacheManager really trigger the delayed eviction of cache?

You can just open the debug log for package cn.howaric.cache.enhancer, then you will see the related logs.

logging:
  level:
    cn.howaric.cache.enhancer: debug

Logs example,

2022-03-07 21:56:40.780 DEBUG 85337 --- [nio-8080-exec-1] c.h.c.e.listener.ListenableCache       : Evict cache
2022-03-07 21:56:40.782 DEBUG 85337 --- [nio-8080-exec-1] c.h.c.e.listener.ListenableCache       : Evict cache delayed operation published
2022-03-07 21:56:42.804 DEBUG 85337 --- [pool-1-thread-1] c.h.c.e.listener.EvictCacheOperation   : Re-evict cache