英文:
get redis server time with redistemplate
问题
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()
}
}
英文:
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()
}
}
答案1
得分: 0
RedisTemplate提供了一个execute
方法,可以执行Redis命令。
Object execute = redisTemplate.execute(new RedisCallback<Long>() {
@Override
public Long doInRedis(RedisConnection redisConnection) throws DataAccessException {
return redisConnection.time();
}
});
实际上,其他redisTemplate方法如get()
也是使用execute(RedisCallback<T> callback)
来执行的。
redisTemplate.opsForValue().get("key");
get
的源代码如下:
@Override
public V get(Object key) {
return execute(new ValueDeserializingRedisCallback(key) {
@Override
protected byte[] inRedis(byte[] rawKey, RedisConnection connection) {
return connection.get(rawKey);
}
});
}
@Nullable
<T> T execute(RedisCallback<T> callback) {
return template.execute(callback, true);
}
英文:
RedisTemplate provides an execute
method that can execute Redis Command.
Object execute = redisTemplate.execute(new RedisCallback<Long>() {
@Override
public Long doInRedis(RedisConnection redisConnection) throws DataAccessException {
return redisConnection.time();
}
});
Actually, other redisTemplate methods like get()
also execute using execute(RedisCallback<T> callback)
.
redisTemplate.opsForValue().get("key");
get
source code is
@Override
public V get(Object key) {
return execute(new ValueDeserializingRedisCallback(key) {
@Override
protected byte[] inRedis(byte[] rawKey, RedisConnection connection) {
return connection.get(rawKey);
}
});
}
@Nullable
<T> T execute(RedisCallback<T> callback) {
return template.execute(callback, true);
}
答案2
得分: 0
你可以使用RedisTemplate的execute方法执行Redis命令以获取服务器时间。具体实现如下:
@Autowired
private RedisTemplate redisTemplate;
public String getServerTime() {
return (String) redisTemplate.execute((RedisCallback<String>) connection
-> connection.time().toString());
}
RedisCallback用于封装Redis命令。在这里,使用time命令来获取服务器时间。返回值是一个数组。第一个元素是当前时间的Unix时间戳,第二个元素是微秒数。使用toString方法将数组转换为字符串并返回。
英文:
You can use the execute method of RedisTemplate to execute the Redis command to obtain the server time. The specific implementation is as follows:
@Autowired
private RedisTemplate redisTemplate;
public String getServerTime() {
return (String) redisTemplate.execute((RedisCallback<String>) connection
-> connection.time().toString());
}
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论