/**
 * 获取系统时间
 * @author ff
 * @date 2021/10/25 15:39
 */
@Component
public class GetSystemTime {

    private final RedisTemplate<String, Object> redisTemplate;

    private static final String SCRIPT_TIME = "local a=redis.call('TIME'); return (a[1]*1000000+a[2])/1000";

    public GetSystemTime(RedisTemplate<String, Object> redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    /**
     * 获取当前时间戳,13位
     * @return
     */
    public Long currentTimeMillis() {
        final DefaultRedisScript<Long> script = new DefaultRedisScript<>(SCRIPT_TIME, Long.class);
        return redisTemplate.execute(script, Collections.EMPTY_LIST);
    }
}

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐