3

I follow some tutorial on web to setup Spring Cache with redis,

my function look like this:

@Cacheable(value = "post-single", key = "#id", unless = "#result.shares < 500")
@GetMapping("/{id}")
public Post getPostByID(@PathVariable String id) throws PostNotFoundException {
 log.info("get post with id {}", id);
 return postService.getPostByID(id);
}

As I understand, the value inside @Cacheable is the cache name and key is the cache key inside that cache name. I also know Redis is an in-memory key/value store. But now I'm confused about how Spring will store cache name to Redis because looks like Redis only manages key and value, not cache name.

Looking for anyone who can explain to me.

Thanks in advance

asked Jan 20, 2021 at 8:35

1 Answer 1

4

Spring uses cache name as the key prefix when storing your data. For example, when you call your endpoint with id=1 you will see in Redis this key

post-single::1

You can customize the prefix format through CacheKeyPrefix class.

answered Jan 20, 2021 at 22:18
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @Aleh i also found how to config cache key prefix here stackoverflow.com/questions/56764225/…

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.