1

I am implementing redis db along with cache in my springboot application. I am inserting data in redis and using @CachePut to add it in cache and i can see the data using redis gui. While fetching the data i am using @Cacheable annotation with the same key as cache put but it always returns null result even if the value exists in the cache and not even calling the database method just returns null.

This is my redis configuration:

@Bean
    public RedisTemplate<String, LTERequest> ulrCacheTemplate() {
        RedisTemplate<String, LTERequest> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(redisConnectionFactory());
        redisTemplate.setEnableTransactionSupport(true);
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        return redisTemplate;
    }

This is my service layer

@Cacheable(value = "ulrs", key = "#sessionId")
    public LTERequest getLTERequest(String sessionId) {
        System.out.println("in cache comp");
        logger.info(" Request UUID [{}] getting record from ULR cache for id [{}]",Thread.currentThread().getName(),sessionId);

        return requestRepo.get(sessionId);

    }
        @CachePut(value = "ulrs", key = "#lterequest.sessionId")
    public void addLTERequest(LTERequest lterequest, int partition) {
        lterequest=requestRepo.add(lterequest,partition);

    }

Here is my LTERequest class:

public class LTERequest implements Serializable{

    private static final long serialVersionUID = 1L;
    private String imsi;
    private String sessionId;
    private long lastUpdateTimestamp;
}

Can anyone point out what am doing wrong.

kirti
  • 4,499
  • 4
  • 31
  • 60

0 Answers0