ListOperations不允许value为空

使用redisTemplate向ListOperations里面存储长度为0的list的时候,会报错:Values must not be 'null' or empty redis
典型业务代码如下:

String key = String.format(Consts.OFFER_LIST_COUNTRY_NEW, country, fornew);
ListOperations<String, Robad> operations = redisTemplate.opsForList();
List<Robad> raList = new ArrayList<>();
// 缓存存在
boolean hasKey = redisTemplate.hasKey(key);
if (hasKey) {
    raList=operations.range(key, 0, 1000);
}else {
    raList = robadMapper.getListByCountry(country, fornew);
    if(raList!=null&&raList.size()>0) {
        operations.rightPushAll(key, raList);
    }
    
}

上面raList 如果为空的会报错,所以我判断了,但此时可能会导致缓存击穿,需要额外处理。
经查询源代码发现确实有判单。

/*
     * (non-Javadoc)
     * @see org.springframework.data.redis.core.ListOperations#rightPushAll(java.lang.Object, java.util.Collection)
     */
    @Override
    public Long rightPushAll(K key, Collection<V> values) {

        final byte[] rawKey = rawKey(key);
        final byte[][] rawValues = rawValues(values);

        return execute(new RedisCallback<Long>() {
            public Long doInRedis(RedisConnection connection) {
                return connection.rPush(rawKey, rawValues);
            }
        }, true);
    }
/**
     * @param values must not be {@literal empty} nor contain {@literal null} values.
     * @return
     * @since 1.5
     */
    byte[][] rawValues(Collection<V> values) {

        Assert.notEmpty(values, "Values must not be 'null' or empty.");
        Assert.noNullElements(values.toArray(), "Values must not contain 'null' value.");

        byte[][] rawValues = new byte[values.size()][];
        int i = 0;
        for (V value : values) {
            rawValues[i++] = rawValue(value);
        }

        return rawValues;
    }

标签: none

添加新评论