2

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

SBhogal
  • 119
  • 2
  • 15

3 Answers3

0

If I understand you correctly, you could achieve this using:

@Cacheable(value="books", key="T(someType).hash(#isbn)") 
public Book findBook(ISBN isbn, boolean checkWarehouse, boolean includeUsed)

However, how can you guarantee that this hash would be unique? That is a problem that you could encounter.

Reference: @Cacheable key on multiple method arguments

JCompetence
  • 6,997
  • 3
  • 19
  • 26
  • actually i want to store UserDto object in Redis as Redis Hash. Redis Hash is a data structure provided by redis. – SBhogal Dec 31 '21 at 06:16
0

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

kiz
  • 47
  • 1
  • 8
0

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

Neal.Shan
  • 128
  • 1
  • 11