一、操作String类型

1.set(K key, V value):新增一个字符串类型的值,key是键,value是值。
redisTemplate.opsForValue().set(CommonConstant.TEST_STRING+"关羽","水淹七军");

2.setIfAbsent(K key, V value):如果键不存在则新增,存在则不改变已经有的值。
redisTemplate.opsForValue().setIfAbsent(CommonConstant.TEST_STRING + "关羽","桃园结义");

3.set(K key, V value, long timeout, TimeUnit unit):设置变量值的过期时间。
redisTemplate.opsForValue().set(CommonConstant.TEST_STRING + "关羽", "千里走单骑", 1, TimeUnit.MINUTES);

4.get(Object key):获取key键对应的值
String o = (String) redisTemplate.opsForValue().get(CommonConstant.TEST_STRING + "关羽");

5.getAndSet(K key, V value): 获取原来key键对应的值并重新赋新值。
String s1 = (String)redisTemplate.opsForValue().getAndSet(CommonConstant.TEST_STRING + "关羽", "败走麦城");

6.increment(K key, long delta):以增量的方式将long值存储在变量中。
redisTemplate.opsForValue().increment(CommonConstant.TEST_STRING + "关羽",3);

7.multiSet(Map<? extends K,? extends V> map):设置map集合到redis。
Map<String,String> map = new HashMap<>(2);
map.put("1","1");
map.put("2","2");
redisTemplate.opsForValue().multiSet(map);

8.multiSetIfAbsent(Map<? extends K,? extends V> map):如果对应的map集合名称不存在,则添加,如果存在则不做修改。如果map中的某个key存在,则整个map都存入失败。
redisTemplate.opsForValue().multiSetIfAbsent(map);

9.multiGet(Collection<K> keys):根据集合取出对应的value值。
List<String> list = new ArrayList<>();
list.add("1");
list.add("2");
List multiGet = redisTemplate.opsForValue().multiGet(list);

二、操作Hash类型

**hash 特别适合用于存储对象。**
1.put(H key, HK hashKey, HV value):新增一个键为key字段为hashKey值为value的记录。
StudentEntity studentEntity = StudentEntity.builder().studentName("织席贩履之辈").studentAge(18).id(1l).build();
StudentEntity studentEntity2 = StudentEntity.builder().studentName("方天画戟专捅义父").studentAge(28).id(2l).build();
redisTemplate.opsForHash().put(CommonConstant.TEST_HASH, studentEntity.getId(), studentEntity);
redisTemplate.opsForHash().put(CommonConstant.TEST_HASH, studentEntity2.getId(), studentEntity2);
        
2.putIfAbsent(H key, HK hashKey, HV value)
如果键存在,在键值可以添加不存在的的键值对,如果键不存在,则新增一个键,同时将键值对添加到该键。
 redisTemplate.opsForHash().putIfAbsent(CommonConstant.TEST_HASH, studentEntity.getId(), "89485");
 
 3.get(H key, Object hashKey):获取键中的指定键是否有值,如果有则获取值,没有则返回null。
StudentEntity o = (StudentEntity) redisTemplate.opsForHash().get(CommonConstant.TEST_HASH, 1);

4.values(H key):获取指定键的values值。
List values = redisTemplate.opsForHash().values(CommonConstant.TEST_HASH);

5.keys(H key):获取键中所有的keys。
Set keys = redisTemplate.opsForHash().keys(CommonConstant.TEST_HASH);

6.hasKey(H key, Object hashKey):判断键中是否有指定key。
Boolean b = redisTemplate.opsForHash().hasKey(CommonConstant.TEST_HASH, 1);

7.entries(H key):获取键中的键值对。
  Map entries = redisTemplate.opsForHash().entries(CommonConstant.TEST_HASH);
  
8.increment(H key, HK hashKey, long l):使键中key为hashKey的value以long值的大小进行自增长。
redisTemplate.opsForHash().increment(CommonConstant.TEST_HASH, 3, 1);

9.putAll(H key, Map<? extends HK,? extends HV> m):以map集合的形式添加键值对。
Map<Long, StudentEntity> map = new HashMap<>();
StudentEntity entity = StudentEntity.builder().id(5l).studentAge(148).studentName("庄子").build();
StudentEntity entity2 = StudentEntity.builder().id(4l).studentAge(288).studentName("老子").build();
map.put(entity.getId(), entity);
map.put(entity2.getId(), entity2);
redisTemplate.opsForHash().putAll(CommonConstant.TEST_HASH, map);

10.multiGet(H key, Collection<HK> hashKeys):以集合的方式获取键中的values。
List list = redisTemplate.opsForHash().multiGet(CommonConstant.TEST_HASH, Arrays.asList(1, 2, 3, 4, 5));

11.delete(H key, Object... hashKeys):删除键中的键值对,可以传入多个参数,删除多个键值对。
 redisTemplate.opsForHash().delete(CommonConstant.TEST_HASH, 1, 2);

三、操作List类型

1.leftPush(K key, V value):在key左边添加元素值
redisTemplate.opsForList().leftPush(CommonConstant.TEST_LIST, "插标卖首");
2.leftPushIfPresent(K key, V value):如果存在集合则添加元素
redisTemplate.opsForList().leftPushIfPresent(CommonConstant.TEST_LIST, "叉出去");
3.leftPushAll(K key, V... values):向左边批量添加参数元素
redisTemplate.opsForList().leftPushAll(CommonConstant.TEST_LIST, "无名鼠辈", "虎女岂能嫁于犬子");
4.leftPushAll(K key, Collection<V> values):以集合的方式向左边批量添加元素
redisTemplate.opsForList().leftPushAll(CommonConstant.TEST_LIST, Arrays.asList("七进七出救阿斗", "火烧赤壁"));
5.leftPush(K key, V pivot, V value)
// 草船借箭放到无名鼠辈之前,前提是无名鼠辈已存在
redisTemplate.opsForList().leftPush(CommonConstant.TEST_LIST, "无名鼠辈", "草船借箭");
6.rightPopAndLeftPush(K sourceKey, K destinationKey):移除sourceKey集合中右边的元素,同时从左边插入destinationKey集合
// 从TEST_LIST右边移除从TEST_LIST2左边插入
redisTemplate.opsForList().rightPopAndLeftPush(CommonConstant.TEST_LIST, "TEST_LIST2");
7.rightPopAndLeftPush(K sourceKey, K destinationKey, long timeout, TimeUnit unit)
在等待的时间里,从右边移除sourceKey集合中的元素同时在destinationKey左边添加元素,如果超过等待的时间sourceKey仍没有元素则退出。
redisTemplate.opsForList().rightPopAndLeftPush("TEST_LIST2", CommonConstant.TEST_LIST, 2, TimeUnit.SECONDS);
8.leftPop(K key):移除集合中的左边第一个元素。
Object o = redisTemplate.opsForList().leftPop(CommonConstant.TEST_LIST);
(JSON.toJSONString(o));
9.leftPop(K key, long timeout, TimeUnit unit)
移除集合中左边的元素在等待的时间里,如果超过等待的时间仍没有元素则退出。
Object o1 = redisTemplate.opsForList().leftPop("TEST_LIST2", 2, TimeUnit.SECONDS);
(JSON.toJSONString(o1));
10.range(K key, long start, long end):获取指定区间的值(0到-1代表全部)
List range = redisTemplate.opsForList().range(CommonConstant.TEST_LIST, 0, -1);
range.forEach(r -> (JSON.toJSONString(r)));
11.size(K key):获取集合长度
Long size = redisTemplate.opsForList().size(CommonConstant.TEST_LIST);
(JSON.toJSONString(size));
12.index(K key, long index):获取集合指定位置的值
String index = stringRedisTemplate.opsForList().index(CommonConstant.TEST_LIST, 1);
13.set(K key, long index, V value)
// 在集合的指定位置插入元素,如果指定位置已有元素,则覆盖,超过集合size-1则会报错。
redisTemplate.opsForList().set(CommonConstant.TEST_LIST, size - 1, "七步成诗");
14.trim(K key, long start, long end):截取集合,保留长度内的数据
redisTemplate.opsForList().trim(CommonConstant.TEST_LIST, 0, size - 2);

四、操作Set类型

1.add(K key, V... values):向key中批量添加值
Long add = stringRedisTemplate.opsForSet().add(CommonConstant.TEST_SET, "温酒斩华雄", "温酒斩华雄", "三英战吕布", "卧龙凤雏", "落凤坡", "长坂坡");
(add.toString());
2.members(K key):获取key中的所有的值
Set<String> members = stringRedisTemplate.opsForSet().members(CommonConstant.TEST_SET);
members.forEach(r -> (r));
3.size(K key):获取set的长度
Long size = stringRedisTemplate.opsForSet().size(CommonConstant.TEST_SET);
(size.toString());
4.isMember(K key, Object o):检查给定的元素是否在set中
Boolean b = stringRedisTemplate.opsForSet().isMember(CommonConstant.TEST_SET, "温酒斩华雄");
(b.toString());
5.pop(K key):弹出变量中的元素
String pop = stringRedisTemplate.opsForSet().pop(CommonConstant.TEST_SET);
(pop);
6.randomMember(K key):随机获取变量中的元素
String s = stringRedisTemplate.opsForSet().randomMember(CommonConstant.TEST_SET);
(s);
7.randomMembers(K key, long count):随机获取变量中指定个数的元素。
List<String> list = stringRedisTemplate.opsForSet().randomMembers(CommonConstant.TEST_SET, 2);
list.forEach(r -> (r));
8.move(K key, Object... values):批量移除变量中的元素
Long l = stringRedisTemplate.opsForSet().remove(CommonConstant.TEST_SET, "三马同槽");
(l.toString());
9.move(K key, V value, K destKey):转移set的元素值到另一个set
Boolean b1 = stringRedisTemplate.opsForSet().move(CommonConstant.TEST_SET, "三马同槽", CommonConstant.TEST_SET_OTHER);
(b1.toString());
10.scan(K key, ScanOptions options):
匹配获取键值对,ScanOptions.NONE为获取全部键值对;ScanOptions.scanOptions().match("三英战吕布").build() 匹配获取键位"三英战吕布"的键值,不能模糊匹配。
Cursor<String> cursor = stringRedisTemplate.opsForSet().scan(CommonConstant.TEST_SET, ScanOptions.scanOptions().match("三英战吕布").build());
cursor.forEachRemaining(r -> (r));

Long add2 = stringRedisTemplate.opsForSet().add(CommonConstant.TEST_SET_OTHER, "三英战吕布", "卧龙凤雏", "落凤坡", "张飞", "赵云");
11.difference(K key, K otherKey):求key与otherKey两个集合的差集
// 求的是TEST_SET与TEST_SET_DEST的差集,即TEST_SET有而TEST_SET_OTHER没有
Set<String> difference = stringRedisTemplate.opsForSet().difference(CommonConstant.TEST_SET, CommonConstant.TEST_SET_OTHER);
difference.forEach(r -> (r));
12.differenceAndStore(K key, K otherKey, K destKey)
// 将TEST_SET与TEST_SET_DEST的差集保存到TEST_SET_DEST
Long aLong = stringRedisTemplate.opsForSet().differenceAndStore(CommonConstant.TEST_SET, CommonConstant.TEST_SET_OTHER, CommonConstant.TEST_SET_DEST);
(aLong.toString());
13.difference(K key, Collection<K> otherKeys):后面的集合的元素是set的key
// 求的是TEST_SET与TEST_SET_DEST的差集,即TEST_SET有而TEST_SET_DEST没有
Set<String> difference2 = stringRedisTemplate.opsForSet().difference(CommonConstant.TEST_SET, Arrays.asList(CommonConstant.TEST_SET_DEST));
difference2.forEach(r -> (r));
14.intersect(K key, K otherKey):获取2个set中的交集
Set<String> intersect = stringRedisTemplate.opsForSet().intersect(CommonConstant.TEST_SET, CommonConstant.TEST_SET_DEST);
intersect.forEach(r -> (r));
15.union(K key, K otherKey):获取2个set中的并集
Set<String> union = stringRedisTemplate.opsForSet().union(CommonConstant.TEST_SET, CommonConstant.TEST_SET_DEST);
union.forEach(r -> (r));
交集与并集的其他方法同差集,故不再赘述!

五、操作ZSet类型

1.add(K key, V value, double score):添加元素到zset中同时指定元素的分值
Boolean b = stringRedisTemplate.opsForZSet().add(CommonConstant.TEST_ZSET, "铜雀台", System.currentTimeMillis());
stringRedisTemplate.opsForZSet().add(CommonConstant.TEST_ZSET, "洛神赋", System.currentTimeMillis() - 1000 * 258);
stringRedisTemplate.opsForZSet().add(CommonConstant.TEST_ZSET, "老骥伏枥", System.currentTimeMillis() + 666 * 2578);
stringRedisTemplate.opsForZSet().add(CommonConstant.TEST_ZSET, "志在千里", System.currentTimeMillis() - 258 * 698714);
2.range(K key, long start, long end):获取zset指定区间的元素,zset默认正序排列
// 获取下标0,1 志在千里 洛神赋
Set<String> range = stringRedisTemplate.opsForZSet().range(CommonConstant.TEST_ZSET, 0, 1);
range.forEach(r -> (r));
3.rangeByScore(K key, double min, double max):根据设置的score获取区间值
// 志在千里 洛神赋 铜雀台
Set<String> set = stringRedisTemplate.opsForZSet().rangeByScore(CommonConstant.TEST_ZSET, 0, System.currentTimeMillis());
set.forEach(r -> (r));
4.count(K key, double min, double max):获取区间值的个数
Long count = stringRedisTemplate.opsForZSet().count(CommonConstant.TEST_ZSET, 0, System.currentTimeMillis());
(count.toString());
5.rank(K key, Object o):获取变量中元素的索引,下标开始位置为0
Long l2 = stringRedisTemplate.opsForZSet().rank(CommonConstant.TEST_ZSET, "老骥伏枥");
// 3
(l2.toString());
6.(K key, ScanOptions options):匹配获取键值对,ScanOptions.NONE为获取全部键值对;ScanOptions.scanOptions().match("老骥伏枥").build()匹配获取键位"老骥伏枥"的键值对,不能模糊匹配。
Cursor<ZSetOperations.TypedTuple<String>> cursor = stringRedisTemplate.opsForZSet().scan(CommonConstant.TEST_ZSET, ScanOptions.scanOptions().match("老骥伏枥").build());
cursor.forEachRemaining(r -> (r.getScore() + r.getValue()));
7.zCard(K key):获取zset中元素的个数
Long aLong = stringRedisTemplate.opsForZSet().zCard(CommonConstant.TEST_ZSET);
(aLong.toString());
8.score(K key, Object o):获取元素的分值
Double d = stringRedisTemplate.opsForZSet().score(CommonConstant.TEST_ZSET, "洛神赋");
(d.toString());
9.incrementScore(K key, V value, double delta):修改zset中的元素的分值
// 返回的是加后的值
Double d1 = stringRedisTemplate.opsForZSet().incrementScore(CommonConstant.TEST_ZSET, "洛神赋", 100000);
(d1.toString());
10.reverseRange(K key, long start, long end):索引倒序排列指定区间元素
// 所有元素倒序排列
Set<String> set1 = stringRedisTemplate.opsForZSet().reverseRange(CommonConstant.TEST_ZSET, 0, -1);
set1.forEach(r -> (r));
11.reverseRangeByScore(K key, double min, double max):倒序排列指定分值区间元素
Set<String> set2 = stringRedisTemplate.opsForZSet().reverseRangeByScore(CommonConstant.TEST_ZSET, 0, System.currentTimeMillis());
set2.forEach(r -> (r));
12.reverseRangeByScore(K key, double min, double max, long offset, long count)
倒序排列从给定下标和给定长度分值区间元素
Set<String> set3 = stringRedisTemplate.opsForZSet().reverseRangeByScore(CommonConstant.TEST_ZSET, 0, System.currentTimeMillis(), 0, 2);
set3.forEach(r -> (r));
13.reverseRank(K key, Object o):获取倒序排列的索引值
Long ll = stringRedisTemplate.opsForZSet().reverseRank(CommonConstant.TEST_ZSET, "老骥伏枥");
(String.valueOf(ll));

stringRedisTemplate.opsForZSet().add(CommonConstant.TEST_ZSET_OTHER, "老骥伏枥", System.currentTimeMillis());
stringRedisTemplate.opsForZSet().add(CommonConstant.TEST_ZSET_OTHER, "肤如凝脂", System.currentTimeMillis());
14.removeRange(K key, long start, long end):根据索引值移除区间元素
stringRedisTemplate.opsForZSet().removeRange(CommonConstant.TEST_ZSET_DEST, 0, -1);
15.intersectAndStore(K key, K otherKey, K destKey):将key和otherKey的交集存放到destKey里面
// 交集分值相加
Long aLong3 = stringRedisTemplate.opsForZSet().intersectAndStore(CommonConstant.TEST_ZSET, CommonConstant.TEST_ZSET_OTHER, CommonConstant.TEST_ZSET_DEST);
Set<String> range3 = stringRedisTemplate.opsForZSet().range(CommonConstant.TEST_ZSET_DEST, 0, -1);
range3.forEach(r -> (r));
Long aLong2 = stringRedisTemplate.opsForZSet().removeRange(CommonConstant.TEST_ZSET_DEST, 0, -1);
(aLong2.toString());
16.unionAndStore(K key, K otherKey, K destKey):将key和otherKey的并集存放到destKey里面
Long aLong1 = stringRedisTemplate.opsForZSet().unionAndStore(CommonConstant.TEST_ZSET, CommonConstant.TEST_ZSET_OTHER, CommonConstant.TEST_ZSET_DEST);
(aLong1.toString());
17.remove(K key, Object... values):根据元素值批量移除元素
stringRedisTemplate.opsForZSet().remove(CommonConstant.TEST_ZSET, "老骥伏枥");
18.removeRangeByScore(K key, double min, double max):根据分值移除区间元素
Long aLong4 = stringRedisTemplate.opsForZSet().removeRangeByScore(CommonConstant.TEST_ZSET, 0, System.currentTimeMillis());
(aLong4.toString());