Quantcast
Channel: Active questions tagged redis+java - Stack Overflow
Viewing all articles
Browse latest Browse all 2223

RedisCache : TTL updating on each hit instead of DAYS

$
0
0

I'm trying to update a field in a cache object on each hit. Similar to rate limiting with a different purpose.

My Cache Object:

@Getter@Setterpublic class CacheDO implements Serializable {    private static final long serialVersionUID = 1L;    int simCount;    Date date;    int viewCount;}

Cache Service:

public CacheDO updateCacheDO(boolean isDateToBeUpdated, Date date, int customerId, String customerIdStr) {   CacheDO cacheDO = new CacheDO();   byte[] valueInBytes = redisTemplate.getConnectionFactory().getConnection().get((customerIdStr).getBytes());   if(valueInBytes == null){      return addCacheDO(customerId, customerIdStr);   }   CacheDO deserialisedCacheDO;   try {      ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(valueInBytes);      deserialisedCacheDO = (CacheDO) new ClassLoaderObjectInputStream(CacheDO.class.getClassLoader(), byteArrayInputStream).readObject();   } catch (IOException | ClassNotFoundException e) {      LOGGER.error("Error deserializing object");      throw new RuntimeException(e.getMessage(),e);   }   cacheDO.setAbc(deserialisedCacheDO.getAbc());   if (isDateToBeUpdated) {      cacheDO.setDate(date);   } else {      cacheDO.setDate(deserialisedCacheDO.getDate());   }   cacheDO.setViewCount(deserialisedCacheDO.getViewCount() + 1);   Long ttl = redisTemplate.getConnectionFactory().getConnection().keyCommands().ttl(customerIdStr.getBytes(), TimeUnit.DAYS);   LOGGER.info("Updating cache for customer {} ", customerId);   redisTemplate.getConnectionFactory().getConnection().set(customerIdStr.getBytes(), SerializationUtils.serialize(cacheDO), Expiration.from(ttl, TimeUnit.DAYS), RedisStringCommands.SetOption.SET_IF_PRESENT);   return cacheDO;}

On every hit, ttl decreases by 1 instead of decreasing per day. I'm unable to figure out what could be done here, please help me understand my mistake.


Viewing all articles
Browse latest Browse all 2223

Latest Images

Trending Articles



Latest Images