Search code examples
javaspring-bootcachingredisspring-cache

@Cacheable in Spring Cache Stores value in redis outside cache . How do i put it inside cache in redis?


@Override
@Cacheable("stu")
public EmployeeEntity getEmployee(Integer id) {

    return employeeDAO.findById(id).get(); 
} 

Above code save the key in redis in this format "stu::7" here "stu" is the name of the cache and 7 is the key but it stores the cache name and id as one key.

but i want to store in this format in redis STU ->7 Stu should be name of the cache and inside that all the key value pair.


Solution

  • You can set custom key generator to @Cacheable annotation where you can customise it as you want: https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/cache/annotation/Cacheable.html#keyGenerator--

    @Cacheable(value = "stu", keyGenerator = "customKeyGenerator")
    

    Where customKeyGenerator is the name of your custom key generator bean.