AWS Elasticache using Lettuce: Date fields are not getting fetched correctly #3183
-
Hello there! I am using Lettuce java client to talk to AWS Elasticache (Valkey cache using serverless, IAM authentication). Below is the code for writing to cache & reading from cache: public void writeToCache(String key, Object value, long ttl) {
log.info("Writing to Cache with key:{} ttl:{} value:{}", key, ttl, value);
cacheAsyncConnectionPool.thenCompose(connectionPool -> {
try {
StatefulRedisConnection<String, String> connection = connectionPool.acquire().get();
RedisAsyncCommands<String, String> async = connection.async();
async.jsonSet(key, JsonPath.ROOT_PATH, async.getJsonParser().fromObject(value));
async.expire(key, ttl);
return async.exec().whenComplete((s, throwable) -> connectionPool.release(connection));
} catch (Exception e) {
log.warn("Exception while writing to Cache - e: {}", e);
return null;
}
});
} public Object readFromCache(String key) {
log.info("Reading from cache for key: {}", key);
final JsonValue[] value = {null};
cacheAsyncConnectionPool.thenCompose(connectionPool -> {
try {
StatefulRedisConnection<String, String> connection = connectionPool.acquire().get();
RedisAsyncCommands<String, String> async = connection.async();
List<JsonValue> jsonList = async.jsonGet(key, JsonPath.ROOT_PATH).get();
value[0] = jsonList.get(0).asJsonArray().get(0);
return async.exec().whenComplete((s, throwable) -> connectionPool.release(connection));
} catch (Exception e) {
log.warn("Exception while reading from Cache - e: {}", e.getLocalizedMessage());
return null;
}
});
return value[0].toObject(Object.class);
} Problem is when I store json containing date fields in the cache, they are returned in a different format. Write to Cache - Read from cache - { How can I make the date fields to be stored in the same format ? Regards, |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
Hey @alekhya538 , can we also see the model class definition? I assume that return value[0].toObject(Object.class); |
Beta Was this translation helpful? Give feedback.
-
Thanks @tishun I resolved it by using @DateTimeFormat annotation in my model class.
|
Beta Was this translation helpful? Give feedback.
Thanks @tishun
I resolved it by using @DateTimeFormat annotation in my model class.