RedisTemplate 是spring集成的redis上层操作模版,底层集成了很多redis 的API,今天开始我们从源码的角度依次分析每个API接口的功能。
首先分析 RedisTemplate类:如下图
1、六种类型的API接口
private @Nullable ValueOperations valueOps;
private @Nullable ListOperations listOps;
private @Nullable SetOperations setOps;
private @Nullable ZSetOperations zSetOps;
private @Nullable GeoOperations geoOps;
private @Nullable HyperLogLogOperations hllOps;
1、取到数据源-opsForValue
@Override
public ValueOperations opsForValue() {
if (valueOps == null) {
valueOps = new DefaultValueOperations<>(this);
}
return valueOps;
}
2、点击 DefaultValueOperations 类名,进入后显示其下所有API:
3、分析每个接口详情:
class DefaultValueOperations extends AbstractOperations implements ValueOperations {
DefaultValueOperations(RedisTemplate template) {
super(template);
}
/*
* 获取key键对应的值。
*/
@Override
public V get(Object key) {
return execute(new ValueDeserializingRedisCallback(key) {
@Override
protected byte[] inRedis(byte[] rawKey, RedisConnection connection) {
return connection.get(rawKey);
}
}, true);
}
/*
* 获取原来key键对应的值并重新赋新值
*/
@Override
public V getAndSet(K key, V newValue) {
byte[] rawValue = rawValue(newValue);
return execute(new ValueDeserializingRedisCallback(key) {
@Override
protected byte[] inRedis(byte[] rawKey, RedisConnection connection) {
return connection.getSet(rawKey, rawValue);
}
}, true);
}
/*
* 以增量的方式将值存储在变量中。
*/
@Override
public Long increment(K key) {
byte[] rawKey = rawKey(key);
return execute(connection -> connection.incr(rawKey), true);
}
/*
* 以增量的方式将long值存储在变量中。
*/
@Override
public Long increment(K key, long delta) {
byte[] rawKey = rawKey(key);
return execute(connection -> connection.incrBy(rawKey, delta), true);
}
/*
* 以增量的方式将double值存储在变量中
*/
@Override
public Double increment(K key, double delta) {
byte[] rawKey = rawKey(key);
return execute(connection -> connection.incrBy(rawKey, delta), true);
}
/*
* 以减量的方式将值存储在变量中。
*/
@Override
public Long decrement(K key) {
byte[] rawKey = rawKey(key);
return execute(connection -> connection.decr(rawKey), true);
}
/*
* 以减量的方式将long值存储在变量中
*/
@Override
public Long decrement(K key, long delta) {
byte[] rawKey = rawKey(key);
return execute(connection -> connection.decrBy(rawKey, delta), true);
}
/*
* 在原有的值基础上新增字符串到末尾
*/
@Override
public Integer append(K key, String value) {
byte[] rawKey = rawKey(key);
byte[] rawString = rawString(value);
return execute(connection -> {
Long result = connection.append(rawKey, rawString);
return (result != null) ? result.intValue() : null;
}, true);
}
/*
* 截取key键对应值得字符串,从开始下标位置开始到结束下标的位置(包含结束下标)的字符串
*/
@Override
public String get(K key, long start, long end) {
byte[] rawKey = rawKey(key);
byte[] rawReturn = execute(connection -> connection.getRange(rawKey, start, end), true);
return deserializeString(rawReturn);
}
/*
* 根据集合取出对应的value值
*/
@Override
public List multiGet(Collection keys) {
if (keys.isEmpty()) {
return Collections.emptyList();
}
byte[][] rawKeys = new byte[keys.size()][];
int counter = 0;
for (K hashKey : keys) {
rawKeys[counter++] = rawKey(hashKey);
}
List rawValues = execute(connection -> connection.mGet(rawKeys), true);
return deserializeValues(rawValues);
}
/*
* 设置map集合到redis
*/
@Override
public void multiSet(Map extends K, ? extends V> m) {
if (m.isEmpty()) {
return;
}
Map rawKeys = new LinkedHashMap<>(m.size());
for (Map.Entry extends K, ? extends V> entry : m.entrySet()) {
rawKeys.put(rawKey(entry.getKey()), rawValue(entry.getValue()));
}
execute(connection -> {
connection.mSet(rawKeys);
return null;
}, true);
}
/*
* 如果对应的map集合名称不存在,则添加,如果存在则不做修改
*/
@Override
public Boolean multiSetIfAbsent(Map extends K, ? extends V> m) {
if (m.isEmpty()) {
return true;
}
Map rawKeys = new LinkedHashMap<>(m.size());
for (Map.Entry extends K, ? extends V> entry : m.entrySet()) {
rawKeys.put(rawKey(entry.getKey()), rawValue(entry.getValue()));
}
return execute(connection -> connection.mSetNX(rawKeys), true);
}
/*
* 新增一个字符串类型的值,key是键,value是值
*/
@Override
public void set(K key, V value) {
byte[] rawValue = rawValue(value);
execute(new ValueDeserializingRedisCallback(key) {
@Override
protected byte[] inRedis(byte[] rawKey, RedisConnection connection) {
connection.set(rawKey, rawValue);
return null;
}
}, true);
}
/*
* 设置变量值的过期时间
*/
@Override
public void set(K key, V value, long timeout, TimeUnit unit) {
byte[] rawKey = rawKey(key);
byte[] rawValue = rawValue(value);
execute(new RedisCallback
到此,RedisTemplate源码详解-opsForValue到此结束,使用参考
下篇分享RedisTemplate源码详解-opsForList 敬请期待!