今天我们分析opsForList主题中的每个API接口。首先,RedisTemplate类中有个无参的构造方法,如下:
@Override
public ListOperations<K, V> opsForList() {
if (listOps == null) {
listOps = new DefaultListOperations<>(this);
}
return listOps;
}
1、点击进入DefaultListOperations 类:
class DefaultListOperations<K, V> extends AbstractOperations<K, V> implements ListOperations<K, V> {
DefaultListOperations(RedisTemplate<K, V> template) {
super(template);
}
/*
* 获取集合指定位置的值
*/
@Override
public V index(K key, long index) {
return execute(new ValueDeserializingRedisCallback(key) {
@Override
protected byte[] inRedis(byte[] rawKey, RedisConnection connection) {
return connection.lIndex(rawKey, index);
}
}, true);
}
/*
* 移除集合中的左边第一个元素
*/
@Override
public V leftPop(K key) {
return execute(new ValueDeserializingRedisCallback(key) {
@Override
protected byte[] inRedis(byte[] rawKey, RedisConnection connection) {
return connection.lPop(rawKey);
}
}, true);
}
/*
* 移除集合中左边的元素在等待的时间里,如果超过等待的时间仍没有元素则退出
*/
@Override
public V leftPop(K key, long timeout, TimeUnit unit) {
int tm = (int) TimeoutUtils.toSeconds(timeout, unit);
return execute(new ValueDeserializingRedisCallback(key) {
@Override
protected byte[] inRedis(byte[] rawKey, RedisConnection connection) {
List<byte[]> lPop = connection.bLPop(tm, rawKey);
return (CollectionUtils.isEmpty(lPop) ? null : lPop.get(1));
}
}, true);
}
/*
* 向左边添加参数元素
*/
@Override
public Long leftPush(K key, V value) {
byte[] rawKey = rawKey(key);
byte[] rawValue = rawValue(value);
return execute(connection -> connection.lPush(rawKey, rawValue), true);
}
/*
* 向左边批量添加参数元素
*/
@Override
public Long leftPushAll(K key, V... values) {
byte[] rawKey = rawKey(key);
byte[][] rawValues = rawValues(values);
return execute(connection -> connection.lPush(rawKey, rawValues), true);
}
/*
* 以集合的方式向左边批量添加元素
*/
@Override
public Long leftPushAll(K key, Collection<V> values) {
byte[] rawKey = rawKey(key);
byte[][] rawValues = rawValues(values);
return execute(connection -> connection.lPush(rawKey, rawValues), true);
}
/*
* 如果存在集合则添加元素
*/
@Override
public Long leftPushIfPresent(K key, V value) {
byte[] rawKey = rawKey(key);
byte[] rawValue = rawValue(value);
return execute(connection -> connection.lPushX(rawKey, rawValue), true);
}
/*
* 把最后一个参数值放到指定集合的第一个出现中间参数的前面,如果中间参数值存在的话。否则返回-1
*/
@Override
public Long leftPush(K key, V pivot, V value) {
byte[] rawKey = rawKey(key);
byte[] rawPivot = rawValue(pivot);
byte[] rawValue = rawValue(value);
return execute(connection -> connection.lInsert(rawKey, Position.BEFORE, rawPivot, rawValue), true);
}
/*
* 获取集合长度
*/
@Override
public Long size(K key) {
byte[] rawKey = rawKey(key);
return execute(connection -> connection.lLen(rawKey), true);
}
/*
* 获取指定区间的值
*/
@Override
public List<V> range(K key, long start, long end) {
byte[] rawKey = rawKey(key);
return execute(connection -> deserializeValues(connection.lRange(rawKey, start, end)), true);
}
/*
* 从存储在键中的列表中删除等于值的元素的第一个计数事件。count> 0:删除等于从左到右移动的值的第一个元素;count< 0:删除等于从右到左移动的值的第一个元素;count = 0:删除等于value的所有元素。
*/
@Override
public Long remove(K key, long count, Object value) {
byte[] rawKey = rawKey(key);
byte[] rawValue = rawValue(value);
return execute(connection -> connection.lRem(rawKey, count, rawValue), true);
}
/*
* 移除集合中右边的元素
*/
@Override
public V rightPop(K key) {
return execute(new ValueDeserializingRedisCallback(key) {
@Override
protected byte[] inRedis(byte[] rawKey, RedisConnection connection) {
return connection.rPop(rawKey);
}
}, true);
}
/*
* 移除集合中右边的元素在等待的时间里,如果超过等待的时间仍没有元素则退出
*/
@Override
public V rightPop(K key, long timeout, TimeUnit unit) {
int tm = (int) TimeoutUtils.toSeconds(timeout, unit);
return execute(new ValueDeserializingRedisCallback(key) {
@Override
protected byte[] inRedis(byte[] rawKey, RedisConnection connection) {
List<byte[]> bRPop = connection.bRPop(tm, rawKey);
return (CollectionUtils.isEmpty(bRPop) ? null : bRPop.get(1));
}
}, true);
}
/*
* 向集合最右边添加元素
*/
@Override
public Long rightPush(K key, V value) {
byte[] rawKey = rawKey(key);
byte[] rawValue = rawValue(value);
return execute(connection -> connection.rPush(rawKey, rawValue), true);
}
/*
* 向右边批量添加元素
*/
@Override
public Long rightPushAll(K key, V... values) {
byte[] rawKey = rawKey(key);
byte[][] rawValues = rawValues(values);
return execute(connection -> connection.rPush(rawKey, rawValues), true);
}
/*
* 以集合方式向右边添加元素
*/
@Override
public Long rightPushAll(K key, Collection<V> values) {
byte[] rawKey = rawKey(key);
byte[][] rawValues = rawValues(values);
return execute(connection -> connection.rPush(rawKey, rawValues), true);
}
/*
* 向已存在的集合中添加元素
*/
@Override
public Long rightPushIfPresent(K key, V value) {
byte[] rawKey = rawKey(key);
byte[] rawValue = rawValue(value);
return execute(connection -> connection.rPushX(rawKey, rawValue), true);
}
/*
* 向集合中第一次出现第二个参数变量元素的右边添加第三个参数变量的元素值
*/
@Override
public Long rightPush(K key, V pivot, V value) {
byte[] rawKey = rawKey(key);
byte[] rawPivot = rawValue(pivot);
byte[] rawValue = rawValue(value);
return execute(connection -> connection.lInsert(rawKey, Position.AFTER, rawPivot, rawValue), true);
}
/*
* 移除集合中右边的元素,同时在左边加入一个元素
*/
@Override
public V rightPopAndLeftPush(K sourceKey, K destinationKey) {
byte[] rawDestKey = rawKey(destinationKey);
return execute(new ValueDeserializingRedisCallback(sourceKey) {
@Override
protected byte[] inRedis(byte[] rawSourceKey, RedisConnection connection) {
return connection.rPopLPush(rawSourceKey, rawDestKey);
}
}, true);
}
/*
* 移除集合中右边的元素在等待的时间里,同时在左边添加元素,如果超过等待的时间仍没有元素则退出
*/
@Override
public V rightPopAndLeftPush(K sourceKey, K destinationKey, long timeout, TimeUnit unit) {
int tm = (int) TimeoutUtils.toSeconds(timeout, unit);
byte[] rawDestKey = rawKey(destinationKey);
return execute(new ValueDeserializingRedisCallback(sourceKey) {
@Override
protected byte[] inRedis(byte[] rawSourceKey, RedisConnection connection) {
return connection.bRPopLPush(tm, rawSourceKey, rawDestKey);
}
}, true);
}
/*
* 在集合的指定位置插入元素,如果指定位置已有元素,则覆盖,没有则新增,超过集合下标+n则会报错
*/
@Override
public void set(K key, long index, V value) {
byte[] rawValue = rawValue(value);
execute(new ValueDeserializingRedisCallback(key) {
@Override
protected byte[] inRedis(byte[] rawKey, RedisConnection connection) {
connection.lSet(rawKey, index, rawValue);
return null;
}
}, true);
}
/*
* 截取集合元素长度,保留长度内的数据
*/
@Override
public void trim(K key, long start, long end) {
execute(new ValueDeserializingRedisCallback(key) {
@Override
protected byte[] inRedis(byte[] rawKey, RedisConnection connection) {
connection.lTrim(rawKey, start, end);
return null;
}
}, true);
}
}
到此源码API分享完毕,具体使用可参考:使用参考,下篇分享RedisTemplate 源码之集合使用详解-opsForHash,敬请期待!