Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring boot Caching with Redis and Store value as Redis Hash

I have very common requirement and surprised that not able to find the same after browsing lot of pages over the internet.

Below is the code:

 @Cacheable(value = "cache", key = "#request.userId", cacheManager = "defaultCacheManager")
    public UserDto createOrFetch(CreateUserRequest request) {

I want to store, returned UserDto object as Redis Hash, but by default it storing it as simple key value pair.

  1. I know this can achieved via HashOperations which we can get from Redis Template but it lacks to set ttl values to hashes.

  2. Also RedisRepository can also be used by adding @RedisHash on class definition.

Just wondering is it possible to acheive same via @Cacheable annotaion way. Note: I am using Jedis client with SpringBoot 2.1.4.RELEASE

like image 340
SBhogal Avatar asked Dec 06 '25 18:12

SBhogal


2 Answers

I want to store ... as Redis Hash, but by default it storing it as simple key value pair. Just wondering is it possible to acheive same via @Cacheable annotaion way.

The annotation based abstraction (part of the Spring Framework) seems designed for the simple data typed key-value caches. For more complicated data operation, as of redis, its the job done by Spring Data Redis (in the context of Spring).

See also

  • Provide high-level cache API in Spring container
like image 184
kiz Avatar answered Dec 08 '25 18:12

kiz


I think what you are looking for is @RedisHash

Reference: https://docs.spring.io/spring-data/redis/docs/current/api/org/springframework/data/redis/core/RedisHash.html

like image 30
Neal.Shan Avatar answered Dec 08 '25 19:12

Neal.Shan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!