今天我们分析opsForSet主题中的每个API接口。首先,RedisTemplate类中有个无参的构造方法,如下:
@Override
public SetOperations opsForSet() {
if (setOps == null) {
setOps = new DefaultSetOperations<>(this);
}
return setOps;
}
1、点击进入DefaultSetOperations 类:
class DefaultSetOperations extends AbstractOperations implements SetOperations {
DefaultSetOperations(RedisTemplate template) {
super(template);
}
/*
* 向变量中批量添加值
*/
@Override
public Long add(K key, V... values) {
byte[] rawKey = rawKey(key);
byte[][] rawValues = rawValues((Object[]) values);
return execute(connection -> connection.sAdd(rawKey, rawValues), true);
}
/*
* 通过给定的key求2个set变量的差值
*/
@Override
public Set difference(K key, K otherKey) {
return difference(key, Collections.singleton(otherKey));
}
/*
* 通过集合求差值
*/
@Override
public Set difference(K key, Collection otherKeys) {
byte[][] rawKeys = rawKeys(key, otherKeys);
Set rawValues = execute(connection -> connection.sDiff(rawKeys), true);
return deserializeValues(rawValues);
}
/*
* 将求出来的差值元素保存
*/
@Override
public Long differenceAndStore(K key, K otherKey, K destKey) {
return differenceAndStore(key, Collections.singleton(otherKey), destKey);
}
/*
* 将求出来的差值元素保存
*/
@Override
public Long differenceAndStore(K key, Collection otherKeys, K destKey) {
byte[][] rawKeys = rawKeys(key, otherKeys);
byte[] rawDestKey = rawKey(destKey);
return execute(connection -> connection.sDiffStore(rawDestKey, rawKeys), true);
}
/*
* 获取2个变量中的交集
*/
@Override
public Set intersect(K key, K otherKey) {
return intersect(key, Collections.singleton(otherKey));
}
/*
* 获取多个变量之间的交集
*/
@Override
public Set intersect(K key, Collection otherKeys) {
byte[][] rawKeys = rawKeys(key, otherKeys);
Set rawValues = execute(connection -> connection.sInter(rawKeys), true);
return deserializeValues(rawValues);
}
/*
* 获取2个变量交集后保存到最后一个参数上
*/
@Override
public Long intersectAndStore(K key, K otherKey, K destKey) {
return intersectAndStore(key, Collections.singleton(otherKey), destKey);
}
/*
* 获取多个变量的交集并保存到最后一个参数上
*/
@Override
public Long intersectAndStore(K key, Collection otherKeys, K destKey) {
byte[][] rawKeys = rawKeys(key, otherKeys);
byte[] rawDestKey = rawKey(destKey);
return execute(connection -> connection.sInterStore(rawDestKey, rawKeys), true);
}
/*
* 检查给定的元素是否在变量中
*/
@Override
public Boolean isMember(K key, Object o) {
byte[] rawKey = rawKey(key);
byte[] rawValue = rawValue(o);
return execute(connection -> connection.sIsMember(rawKey, rawValue), true);
}
/*
* 获取变量中的值
*/
@Override
public Set members(K key) {
byte[] rawKey = rawKey(key);
Set rawValues = execute(connection -> connection.sMembers(rawKey), true);
return deserializeValues(rawValues);
}
/*
* 转移变量的元素值到目的变量
*/
@Override
public Boolean move(K key, V value, K destKey) {
byte[] rawKey = rawKey(key);
byte[] rawDestKey = rawKey(destKey);
byte[] rawValue = rawValue(value);
return execute(connection -> connection.sMove(rawKey, rawDestKey, rawValue), true);
}
/*
* 随机获取变量中的元素
*/
@Override
public V randomMember(K key) {
return execute(new ValueDeserializingRedisCallback(key) {
@Override
protected byte[] inRedis(byte[] rawKey, RedisConnection connection) {
return connection.sRandMember(rawKey);
}
}, true);
}
/*
* 获取去重的随机元素
*/
@Override
public Set distinctRandomMembers(K key, long count) {
Assert.isTrue(count >= 0, "Negative count not supported. " + "Use randomMembers to allow duplicate elements.");
byte[] rawKey = rawKey(key);
Set rawValues = execute(
(RedisCallback>) connection -> new HashSet<>(connection.sRandMember(rawKey, count)), true);
return deserializeValues(rawValues);
}
/*
* 随机获取变量中指定个数的元素
*/
@Override
public List randomMembers(K key, long count) {
Assert.isTrue(count >= 0,
"Use a positive number for count. " + "This method is already allowing duplicate elements.");
byte[] rawKey = rawKey(key);
List rawValues = execute(connection -> connection.sRandMember(rawKey, -count), true);
return deserializeValues(rawValues);
}
/*
* 批量移除变量中的元素
*/
@Override
public Long remove(K key, Object... values) {
byte[] rawKey = rawKey(key);
byte[][] rawValues = rawValues(values);
return execute(connection -> connection.sRem(rawKey, rawValues), true);
}
/*
* 弹出变量中的元素。
*/
@Override
public V pop(K key) {
return execute(new ValueDeserializingRedisCallback(key) {
@Override
protected byte[] inRedis(byte[] rawKey, RedisConnection connection) {
return connection.sPop(rawKey);
}
}, true);
}
/*
* 弹出变量中的几个元素。
*/
@Override
public List pop(K key, long count) {
byte[] rawKey = rawKey(key);
List rawValues = execute(connection -> connection.sPop(rawKey, count), true);
return deserializeValues(rawValues);
}
/*
* 获取变量中值的个数
*/
@Override
public Long size(K key) {
byte[] rawKey = rawKey(key);
return execute(connection -> connection.sCard(rawKey), true);
}
/*
* 获取2个变量的合集
*/
@Override
public Set union(K key, K otherKey) {
return union(key, Collections.singleton(otherKey));
}
/*
* 获取多个变量的合集
*/
@Override
public Set union(K key, Collection otherKeys) {
byte[][] rawKeys = rawKeys(key, otherKeys);
Set rawValues = execute(connection -> connection.sUnion(rawKeys), true);
return deserializeValues(rawValues);
}
/*
* 获取2个变量合集后保存到最后一个参数上
*/
@Override
public Long unionAndStore(K key, K otherKey, K destKey) {
return unionAndStore(key, Collections.singleton(otherKey), destKey);
}
/*
* 获取多个变量的合集并保存到最后一个参数上
*/
@Override
public Long unionAndStore(K key, Collection otherKeys, K destKey) {
byte[][] rawKeys = rawKeys(key, otherKeys);
byte[] rawDestKey = rawKey(destKey);
return execute(connection -> connection.sUnionStore(rawDestKey, rawKeys), true);
}
/*
* 匹配获取键值对,ScanOptions.NONE为获取全部键值对;ScanOptions.scanOptions().match("C").build()匹配获取键位map1的键值对,不能模糊匹配
*/
@Override
public Cursor scan(K key, ScanOptions options) {
byte[] rawKey = rawKey(key);
return template.executeWithStickyConnection(
(RedisCallback>) connection -> new ConvertingCursor<>(connection.sScan(rawKey, options),
this::deserializeValue));
}
}
2、参考使用参考