Quantcast
Channel: Active questions tagged redis+java - Stack Overflow
Viewing all articles
Browse latest Browse all 2204

How caching works with Redis in Spring Boot?

$
0
0

I implement the following caching mechanism using Redis, but I am not sure how it works for different resultset.

CacheConfig:

@Configurationpublic class CacheConfig {    @Bean    public RedisCacheConfiguration cacheConfiguration() {        return RedisCacheConfiguration.defaultCacheConfig()                .entryTtl(Duration.ofMinutes(60))                .disableCachingNullValues()                .serializeValuesWith(SerializationPair.fromSerializer(                     new GenericJackson2JsonRedisSerializer()));    }    @Bean    public RedisCacheManagerBuilderCustomizer redisCacheManagerBuilderCustomizer() {        return (builder) -> builder                .withCacheConfiguration("productCache",                        RedisCacheConfiguration.defaultCacheConfig()                            .entryTtl(Duration.ofMinutes(10)));    }}

ProductService:

@Cacheable(value = "productCache")@Overridepublic List<ProductDTO> findAllByCategory(Category category) {    // code omitted     return productDTOList;}

Could you please clarify me about the following caching scenarios by assuming retrieving products by category (category1, category2, category3...):

1. Suppose that I first retrieve category1 from database and then if the same category is requested in expiration period, this result will be returned from cache, not from database.

If another type e.g. category2 is retrieved, then the result will be retrieved from database as it is not in cache.

Then if category1 type is retrieved again in expiration period, this will be returned from cache as it had already been cached. Is this scenarios all true?

2. As far as I see, caching works by resultset. When retrieving 3 product categories one by one, it caches each category separately, (not all of them in a cache) is that true?

3. Assume that I retrieve category1 and then retrieve category2. Then there is an update that affects category1 results. In this case I think I should clear cache related to this resultset after each CREATE or UPDATE. So, can I delete only the first resultset? Because I use cache name as "productCache" that is used for service method.


Viewing all articles
Browse latest Browse all 2204

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>