【发布时间】:2021-12-30 02:18:18
【问题描述】:
redis hasKey方法什么时候可以返回null?我已经看到,如果我们在方法参数中也传递 null ,它会返回 false 。那么是否有任何情况下该方法可以返回 null ?
【问题讨论】:
标签: spring spring-boot redis spring-data-redis
redis hasKey方法什么时候可以返回null?我已经看到,如果我们在方法参数中也传递 null ,它会返回 false 。那么是否有任何情况下该方法可以返回 null ?
【问题讨论】:
标签: spring spring-boot redis spring-data-redis
如果使用spring data redis,主要包括以下几种情况
你可以看看底层实现org. Springframework. Data. Redis. Connection. Jedis. Jediskeycommands #exists (byte []...)。详情如下:
@Override
public Boolean hasKey(K key) {
byte[] rawKey = rawKey(key);
return execute(connection -> connection.exists(rawKey), true);
}
@Nullable
@Override
public Long exists(byte[]... keys) {
Assert.notNull(keys, "Keys must not be null!");
Assert.noNullElements(keys, "Keys must not contain null elements!");
try {
if (**isPipelined**()) {
pipeline(connection.newJedisResult(connection.getRequiredPipeline().exists(keys)));
return **null**;
}
if (**isQueueing**()) {
transaction(connection.newJedisResult(connection.getRequiredTransaction().exists(keys)));
return **null**;
}
return connection.getJedis().exists(keys);
} catch (Exception ex) {
throw connection.convertJedisAccessException(ex);
}
}
【讨论】:
null在redis中可能已经不存在了