获取使用RedisTemplate的Redis服务器时间。

huangapple go评论135阅读模式
英文:

get redis server time with redistemplate

问题

I want to get millis of Redis server with RedisTemplate.

Like this

redis> TIME

  1. "1687736427"
  2. "339025"
    redis> TIME
  3. "1687736427"
  4. "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

  1. "1687736427"
  2. "339025"
    redis> TIME
  3. "1687736427"
  4. "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&lt;Long&gt;() {
        @Override
        public Long doInRedis(RedisConnection redisConnection) throws DataAccessException {
            return redisConnection.time();
        }
    });

Actually, other redisTemplate methods like get() also execute using execute(RedisCallback&lt;T&gt; callback).

redisTemplate.opsForValue().get(&quot;key&quot;);

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
&lt;T&gt; T execute(RedisCallback&lt;T&gt; 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&lt;String&gt;) connection 
     -&gt; 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.

huangapple
  • 本文由 发表于 2023年6月26日 08:06:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76552867.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定