On Spring Boot
I want to get millis of Redis server with RedisTemplate.
Like this
redis> TIME
- "1687736427"
- "339025"
redis> TIME - "1687736427"
- "339285"
redis>
But reference says, "Template does not Support Time".
Is there no way?
/* thank you!! lant. My code look like */
this.redisTemplate.execute(new SessionCallback<Long>() {
@Override
public Long execute(RedisOperations operations) throws DataAccessException {
do {
Long now = redisTemplate.execute(new RedisCallback<Long>() {
@Override
public Long doInRedis(RedisConnection connection) throws DataAccessException {
return connection.time();
}
});
} while()
}
}
2
Answers
RedisTemplate provides an
execute
method that can execute Redis Command.Actually, other redisTemplate methods like
get()
also execute usingexecute(RedisCallback<T> callback)
.get
source code isYou can use the execute method of RedisTemplate to execute the Redis command to obtain the server time. The specific implementation is as follows:
RedisCallback is used to encapsulate the Redis command. Here, the time command is used to obtain the server time. The return value is an array. The first element is the Unix time timestamp of the current time, and the second element is the microseconds. Use the toString method to convert an array into a string and return it.