Code Monkey home page Code Monkey logo

spring-enhanced-cache's Introduction

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

spring-enhanced-cache's People

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.