Expand SpringBoot Cache and add business logic.

Asked 2 years ago, Updated 2 years ago, 70 views

We are considering implementing SpringBoot Cache.The cache provider becomes Redis.
@ Cacheable caches the results of the method with key:value.
As a business requirement, it is not only necessary to cache results, but also to store keys in hset.
Therefore, is there a means to execute business logic after caching the results?
I looked it up and it doesn't look like CacheResolver.The sources are as follows:

@Caching(
            cacheable={@Cacheable(value="hello", key="#myUser.name+#root.methodName")},
            put={@CachePut(value="hello", key="#myUser.name+#root.methodName", condition="#myUser.age==12")},
            event={@CacheEvict(value="hello", key="#myUser.name+#root.methodName", allEntries=false, condition="#myUser.age==99")}
    )
    publicList<Map<String,Object>>getResultList(MyUser myUser){
        Map<String, Object>resultMap1 = new HashMap<>();
        resultMap1.put("result", "result10");
        Map<String, Object>resultMap2 = new HashMap<>();
        resultMap2.put("result", "result20");
        System.out.println("MyRedisService.getResultList executed!input:"+myUser.toString());
        return List.of (resultMap1, resultMap2);
    }

java spring spring-boot java8 redis

2022-09-30 19:29

1 Answers

I found a way.

Step 1: Expand RedisCache.Override the get method and add business logic.
Step 2: Expand RedisCacheManager.Override the createRedisCache method and utilize RedisCache extended to Step 1.
Step 3: Create the RedisCacheManager extended in Step 2 as Bean in the configuration class.


2022-09-30 19:29

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.