1

I have EmployeeDto as an Object, which is not synced with the database.

I want to put EmployeeDto inside Redis cache. I have gone through an example where I can see database entity is getting cached using Redis cache but how can we store and retrieve non-database entity in Redis cache?

@Getter
@Setter 
public class EmployeeDto {
    private long employeeId;
    private String requestId;
    private String timestamp;
    private String employeeName;
}

1 Answers1

0

You're missing a concept of CacheManager in Spring cache lib.

CacheManager allows you to create Cache with any name, for your use case you can create a cache with the name default and this cache must be of Redis as you need it.

You can inject CacheManager in any bean, once you have cacheManager object you can call getCache method to get the Cache object now you need to call the appropriate method on this cache object to store/retrieve data.

See many usage here

https://www.codota.com/code/java/classes/org.springframework.cache.CacheManager

sonus21
  • 5,178
  • 2
  • 23
  • 48
  • but CacheManager belongs to Spring .I want to store data in redis cache. – santosh gore Jan 19 '21 at 06:44
  • You can create a Cache with the underlying data store as Redis. In spring boot you need to set `spring.cache.type=redis` and this would create your cache using Redis. – sonus21 Jan 19 '21 at 08:00
  • @Autowired RedisTemplate redisTemplate; redisTemplate.opsForValue().set("KEY","Value"); redisTemplate.opsForValue().get("KEY"); How about above way? – santosh gore Jan 19 '21 at 08:51