前言

有时候熟悉了redis命令,但是用到spring的RedisTemplte的时候就又糊涂了,命令和操作是谁对谁的,我就有点晕了,所以花了点时间整理了一下。

首先,redis命令执行可以在本地环境启动redis后,再启动redis-cli的客户端来敲命令,如果不想或没有安装redis,那就可以用这个网站,可以直接执行redis命令。我参考的redis命令大全地址:https://www.redis.net.cn/order/3687.html

说明

在代码中,我是使用的spring的RedisTemplate来操作的,那么就得受spring管理。
我是在一个maven项目中写了一个简单的controller-service来操作redis的,实际应用中的场景涉及到对数据库的增删改查,再对redis做同样的增删改查。

maven

<dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>

测试

我使用postman来调用的

redisTemplate 实体 redistemplate setifpresent_redis


只需要传一下自定义的类型即可。

配置

#服务配置
server.port=8885
server.tomcat.accept-count=1000
server.tomcat.max-connections=20000
server.tomcat.max-threads=1000
server.tomcat.min-spare-threads=100

#本地redis配置
spring.redis.database=0
spring.redis.host=127.0.0.1
spring.redis.port=6379
spring.redis.timeout=20000
spring.redis.jedis.pool.max-idle=5
spring.redis.jedis.pool.max-wait=5
spring.redis.jedis.pool.min-idle=0
spring.redis.jedis.pool.max-active=5

一、准备操作

先上一下开胃小菜

controller

package com.fanhf.javastudy.mybatistest.controller;

import com.fanhf.javastudy.mybatistest.bean.BondsBean;
import com.fanhf.javastudy.mybatistest.service.BondsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/bonds")
@Api(tags = "redis测试")
@Slf4j
public class BondsController {
    @Autowired
    private BondsService bondsService;

    @PostMapping(value = "/redisDemo")
    @ApiOperation(value = "redis不同数据结构demo")
    public void getBondsRedis(@RequestBody String  type){
         bondsService.getBondsRedis(type);
    }
}

service

package com.fanhf.javastudy.mybatistest.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fanhf.javastudy.mybatistest.bean.BondsBean;
import com.fanhf.javastudy.mybatistest.dao.BondsDao;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.DefaultTypedTuple;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Service;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.concurrent.TimeUnit;

@Service
@Slf4j
public class BondsService {
    private static final  String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";

      public void getBondsRedis(String type) {
        switch (Integer.parseInt(type)){
            case 0:
                log.info("==================Keys操作==========================");
                testRedisKeysCommand();
                break;
            case 1:
                log.info("==================string操作==========================");
                testRedisStringCommand();
                break;
            case 2:
                log.info("==================list操作============================");
                testRedisListCommand();
                break;
            case 3:
                log.info("====================hash操作==========================");
                testRedisHashCommand();
                break;
            case 4:
                log.info("====================set操作===========================");
                testRedisSetCommand();
                break;
            case 5:
                log.info("====================zset操作==========================");
                testRedisZSetCommand();
                break;
            default: return;
        }
    }

二、数据类型操作

下面都是调用的方法

1、keys的操作

public void testRedisKeysCommand(){
        log.info("========expire=============={}",redisTemplate.expire("string1",3L, TimeUnit.MILLISECONDS));
        log.info("========persist============={}",redisTemplate.persist("string1"));
        log.info("========hasKey=============={}",redisTemplate.hasKey("string1"));
        log.info("========getExpire==========={}",redisTemplate.getExpire("string1"));
        log.info("========type================{}",redisTemplate.type("score"));
        log.info("========delete=============={}",redisTemplate.delete("string1"));
    }
 }

命令

redisTemplate

说明

expire string1 3

redisTemplate.expire(“string1”,3L, TimeUnit.MILLISECONDS)

设置key3秒后到期

persist string1

redisTemplate.persist(“string1”)

移除 key 的生存时间

exists string1

redisTemplate.hasKey(“string1”)

判断key 是否存在

ttl string1

redisTemplate.getExpire(“string1”)

剩余过期时间

type score

redisTemplate.type(“score”)

查看当前key的类型string

del string1

redisTemplate.delete(“string1”)

删除key

*说明:有些可能有遗漏,或者没有对应,如有错误之处还望指正。

2、String操作

String 的使用场景:value 除了是字符串以外还可以是数字。

  • 计数器
  • 统计多单位的数量
  • 粉丝数
  • 对象缓存存储
public void testRedisStringCommand(){
        redisTemplate.opsForValue().set("redisKey","redisValue");
        log.info("========get获取value========={}",redisTemplate.opsForValue().get("redisKey"));
        log.info("========getAndSet==========={}",redisTemplate.opsForValue().getAndSet("string1","value1"));
        log.info("========setIfAbsent========={}",redisTemplate.opsForValue().setIfAbsent("wwwwwwwwwww","value2"));
        log.info("========setIfPresent========{}",redisTemplate.opsForValue().setIfPresent("string3","value3"));
        log.info("========get================={}",redisTemplate.opsForValue().get("string3"));
        redisTemplate.opsForValue().set("score","1000");
        log.info("========increment==========={}",redisTemplate.opsForValue().increment("score",100));
        log.info("========score==============={}",redisTemplate.opsForValue().get("score"));
        log.info("========score==============={}",redisTemplate.opsForValue().decrement("score"));
        log.info("========score==============={}",redisTemplate.opsForValue().decrement("score"),200);
        log.info("========get================={}",redisTemplate.opsForValue().get("string1"));
        log.info("========get================={}",redisTemplate.opsForValue().get("string1"));
        redisTemplate.opsForValue().set("mystr","mystrvalue");
        log.info("========append=============={}",redisTemplate.opsForValue().append("mystr1","mystrvalue1"));
        log.info("========get================={}",redisTemplate.opsForValue().get("mystr"));
        log.info("========get================={}",redisTemplate.opsForValue().get("mystr1"));
        log.info("========size================{}",redisTemplate.opsForValue().size("mystr"));
        log.info("========size================{}",redisTemplate.opsForValue().size("mystr1"));
    }

命令

redisTemplate

说明

set key1 value1

redisTemplate.opsForValue().set(“key1”,“value1”);

将key和value放到redis中

get key1

redisTemplate.opsForValue().get(redisKey)

根据key从redis中取出value

getset

redisTemplate.opsForValue().getAndSet(“string1”,“value1”)

先 get 再 set

expire string1 3

redisTemplate.expire(“string1”,3L, TimeUnit.MILLISECONDS)

设置key3秒后到期

persist string1

redisTemplate.persist(“string1”)

移除 key 的生存时间

exists string1

redisTemplate.hasKey(“string1”)

判断key 是否存在

ttl string1

redisTemplate.getExpire(“string1”)

剩余过期时间

setnx string1 value2

redisTemplate.opsForValue().setIfAbsent(“string1”,“value2”)

如果不存在,则设置,返回true


redisTemplate.opsForValue().setIfPresent(“string3”,“value3”)

如果存在,则设置,返回true

type score

redisTemplate.type(“score”)

查看当前key的类型string

incr score

redisTemplate.opsForValue().increment(“score”,100)

自增1

incrby score 100

redisTemplate.opsForValue().increment(“score”,100)

自增步长100,并自减少100

decr score

redisTemplate.opsForValue().decrement(“score”)

自减1

decrby score 200

redisTemplate.opsForValue().decrement(“score”),200)

自减步长200,并自增长100

del string1

redisTemplate.delete(“string1”)

删除key

append mystr1 “hello”

redisTemplate.opsForValue().append(“mystr1”,“mystrvalue1”)

追加值,如果不存在,相当于 set

strlen mystr

redisTemplate.opsForValue().size(“mystr”)

获取字符串长度

*说明:有些可能有遗漏,或者没有对应,如有错误之处还望指正。

3、List操作

在 Redis 中可以把 list 用作栈、队列、阻塞队列。

list 实际上是一个链表,前后都可以插入

如果key不存在,创建新的链表

如果移除了所有的值,空链表,也代表不存在

在两边插入或者改动值,效率最高。

/**
     * redisTemplate.opsForList();//操作list
     **/
    
    public void testRedisListCommand(){
        List<String> list1 =  new ArrayList();
        list1.add("1111");
        list1.add("2222");
        list1.add("3333");
        list1.add("4444");
        log.info("=======leftPush============={}", redisTemplate.opsForList().leftPush("listkey1", JSONObject.toJSONString(list1)));
        log.info("=======range1==============={}", redisTemplate.opsForList().range("listkey1",0,-1));
        log.info("=======rightPush============{}", redisTemplate.opsForList().rightPush("listkey2","qqqq"));
        log.info("=======rightPush============{}", redisTemplate.opsForList().rightPush("listkey2","wwww"));
        log.info("=======rightPush============{}", redisTemplate.opsForList().rightPush("listkey2","eeee"));
        log.info("=======range2==============={}", redisTemplate.opsForList().range("listkey2",0,-1));
        log.info("=======leftPop=============={}", redisTemplate.opsForList().leftPop("listkey1"));
        log.info("=======leftPush============={}", redisTemplate.opsForList().leftPush("listkey1","aaaa"));
        log.info("=======leftPush============={}", redisTemplate.opsForList().leftPush("listkey1","  bbbb"));
        log.info("=======leftPush============={}", redisTemplate.opsForList().leftPush("listkey1","cccc"));
        log.info("=======leftPush============={}", redisTemplate.opsForList().leftPush("listkey1","  dddd"));
        log.info("=======leftPush============={}", redisTemplate.opsForList().leftPush("listkey1","ffff"));
        log.info("=======range3==============={}", redisTemplate.opsForList().range("listkey1",0,-1));
        //截取第1个到第4个元素
        redisTemplate.opsForList().trim("listkey1",0,3);
        log.info("=======range4==============={}", redisTemplate.opsForList().range("listkey1",0,-1));
        log.info("=======leftPop=============={}", redisTemplate.opsForList().leftPop("listkey1"));
        log.info("=======range5==============={}", redisTemplate.opsForList().range("listkey1",0,-1));
        log.info("=======rightPop============={}", redisTemplate.opsForList().rightPop("listkey1"));
        log.info("=======range6==============={}", redisTemplate.opsForList().range("listkey1",0,-1));
        log.info("=======size================={}", redisTemplate.opsForList().size("listkey1"));
        log.info("=======index================{}", redisTemplate.opsForList().index("listkey1",1));
        log.info("=======range7==============={}", redisTemplate.opsForList().range("listkey1",0,-1));
        log.info("=======remove==============={}", redisTemplate.opsForList().remove("listkey1",0,"cccc"));
        log.info("=======range8==============={}", redisTemplate.opsForList().range("listkey1",0,-1));
        //从listkey2([qqqq, wwww, eeee])取出右边第一个给元素,放到listkey3里
        log.info("=======rightPopAndLeftPush=={}",  redisTemplate.opsForList().rightPopAndLeftPush("listkey2","listkey3"));
        //[qqqq, wwww]
        log.info("=======range9==============={}", redisTemplate.opsForList().range("listkey2",0,-1));
        //[eeee]
        log.info("=======range10==============={}", redisTemplate.opsForList().range("listkey3",0,-1));
        //如果listkey3存在的话,将下标为0的改为”1111“
        redisTemplate.opsForList().set("listkey3",0,"1111");
        //如果listkey3存在的话,将下标为0的改为”qqqq“
        redisTemplate.opsForList().set("listkey3",0,"qqqq");
        log.info("=======range11=============={}", redisTemplate.opsForList().range("listkey3",0,-1));
        //如果listkey3存在的话,在左边添加”6666“
        log.info("=======leftPushIfPresent===={}", redisTemplate.opsForList().leftPushIfPresent("listkey3","6666"));
        log.info("=======range12=============={}", redisTemplate.opsForList().range("listkey3",0,-1));

        String[] strings =  new String[]{"asd","fgh","jkl"};
        log.info("=======rightPushAll========={}",redisTemplate.opsForList().rightPushAll("listkey2",strings));
        log.info("=======range13=============={}", redisTemplate.opsForList().range("listkey2",0,-1));
    }

命令

redisTemplate

说明

lpush listkey1 one

redisTemplate.opsForList().leftPush(“listkey1”, JSONObject.toJSONString(list1))

将一个值或者多个值,插入到列表的头部(左)

lrange listkey1 0 -1

redisTemplate.opsForList().range(“listkey1”,0,-1)

查看全部元素

rpush listkey2 qqqq

redisTemplate.opsForList().rightPush(“listkey2”,“qqqq”)

将一个值或者多个值,插入到列表的头部(右)

lpop listkey1

redisTemplate.opsForList().leftPop(“listkey1”)

移除list的从左边数第一个元素

rpop listkey1

redisTemplate.opsForList().rightPop(“listkey1”)

移除list的从右边数第一个元素

llen listkey1

redisTemplate.opsForList().size(“listkey1”)

长度

lindex listkey1 1

redisTemplate.opsForList().index(“listkey1”,1)

通过下标获取list中的某一个值

lrem listkey1 0 cccc

redisTemplate.opsForList().remove(“listkey1”,0,“cccc”)

rpoplpush listkey2 listkey3

redisTemplate.opsForList().rightPopAndLeftPush(“listkey2”,“listkey3”)

从listkey2取出右边第一个给元素,放到listkey3里

lset listkey3 0 qqqq

redisTemplate.opsForList().set(“listkey3”,0,“qqqq”)

如果存在,更新当前下标的值OK

lpush listkey3 6666

redisTemplate.opsForList().leftPushIfPresent(“listkey3”,“6666”)

如果存在listkey3,将"6666"放到listkey3中

rpush listkey2 “{“asd”,“fgh”,“jkl”}”

redisTemplate.opsForList().rightPushAll(“listkey2”,strings)

将一个或多个值存到listkey2中

ltrim listkey1 0 3

redisTemplate.opsForList().trim(“listkey1”,0,3)

截取第1个到第4个元素

*说明:有些可能有遗漏,或者没有对应,如有错误之处还望指正。

4、Hash操作

也是 key - value 形式的,但是value 是一个map。
Hash 适合存储经常变动的对象信息,String 更适合于存储字符串

/**
     * redisTemplate.opsForHash();//操作hash
     **/
    
    public void testRedisHashCommand(){
        redisTemplate.opsForHash().put("hash1","h1k1","v1");
        redisTemplate.opsForHash().put("hash1","h1k2","v2");
        log.info("========entries============={}",redisTemplate.opsForHash().entries("hash1"));
        redisTemplate.opsForHash().put("redishash","name","tom");
        log.info("=========get1==============={}",redisTemplate.opsForHash().get("redishash","name"));
        Map<String, String> map =  new HashMap<>();
        map.put("hashMapKey1","hashMapValue1");
        map.put("hashMapKey2","hashMapValue2");
        redisTemplate.opsForHash().putAll("hashMap",map);
        log.info("========entries============={}",redisTemplate.opsForHash().entries("hashMap"));
        log.info("========get2================{}",redisTemplate.opsForHash().get("hashMap","hashMapKey2"));
        redisTemplate.opsForHash().put("hincrby","number","5");
        log.info("========increment==========={}",redisTemplate.opsForHash().increment("hincrby","number",10L));
        log.info("========putIfAbsent========={}",redisTemplate.opsForHash().putIfAbsent("redishash1","hash","absent"));
        log.info("========get3================{}",redisTemplate.opsForHash().get("redishash1","hash"));
        log.info("========lengthOfValue======={}",redisTemplate.opsForHash().lengthOfValue("hashMap","hashMapKey1"));
        log.info("========delete=============={}",redisTemplate.opsForHash().delete("hash1","h1k1"));
        log.info("========get3================{}",redisTemplate.opsForHash().get("hash1","h1k1"));
        log.info("========keys================{}",redisTemplate.opsForHash().keys("hashMap"));
        log.info("========size================{}",redisTemplate.opsForHash().size("hashMap"));
        log.info("========size================{}",redisTemplate.opsForHash().hasKey("hincrby","number"));
    }

命令

redisTemplate

说明

hset redishash name tom

redisTemplate.opsForHash().put(“redishash”,“name”,“tom”)

将哈希表 key 中的字段 field 的值设为 value

hmset hashMap name:lily age:18

redisTemplate.opsForHash().putAll(“hashMap”,map)

同时将多个 field-value (域-值)对设置到哈希表 key 中

hmget hashMap name age

redisTemplate.opsForHash().entries(“hashMap”)

获取所有给定字段的值

hget redishash name

redisTemplate.opsForHash().get(“redishash”,“name”)

获取存储在哈希表中指定字段的值

hincrby hincrby number 10

redisTemplate.opsForHash().increment(“hincrby”,“number”,10L)

为哈希表 key 中的指定字段的整数值加上增量 increment

hsetnx redishash1 hash absent

redisTemplate.opsForHash().putIfAbsent(“redishash1”,“hash”,“absent”)

只有在字段 field 不存在时,设置哈希表字段的值

hdel redishash name

redisTemplate.opsForHash().delete(“redishash”,“name”)

删除一个或多个哈希表字段

hkeys hashMap

redisTemplate.opsForHash().keys(“hashMap”)

获取所有哈希表中的字段

hlen hashMap

redisTemplate.opsForHash().size(“hashMap”)

获取哈希表中字段的数量

hexists “hincrby” “number”

redisTemplate.opsForHash().hasKey(“hincrby”,“number”)

查看哈希表 key 中,指定的字段是否存在

*说明:有些可能有遗漏,或者没有对应,如有错误之处还望指正。

5、Set操作

/**
     * redisTemplate.opsForSet();//操作set
     **/
    public void testRedisSetCommand(){
          String[] strings =  new  String[]{"aaaa","bbbb","cccc","dddd"};
        redisTemplate.opsForSet().add("setKey",strings);
        redisTemplate.opsForSet().add("setKey1","eeee");
        redisTemplate.opsForSet().add("setKey2","ffff");
        redisTemplate.opsForSet().add("setKey3","hhhh");
        redisTemplate.opsForSet().add("setKey3","iiii");
        redisTemplate.opsForSet().add("setKey3","cccc");
        redisTemplate.opsForSet().add("setKey3","llll");
        redisTemplate.opsForSet().add("setKey3","aaaa");

        log.info("=========difference============{}", redisTemplate.opsForSet().difference("setKey2","setKey1"));
        log.info("=========differenceAndStore===={}", redisTemplate.opsForSet().differenceAndStore("setKey2","setKey1","diffKey2"));
        log.info("=========members1=============={}", redisTemplate.opsForSet().members("diffKey2"));
        log.info("=========union================={}", redisTemplate.opsForSet().union("setKey2","setKey1"));
        log.info("=========unionAndStore========={}", redisTemplate.opsForSet().unionAndStore("setKey2","setKey1","setKey1-setKey2-union"));
        log.info("=========members2=============={}", redisTemplate.opsForSet().members("setKey1-setKey2-union"));
        log.info("=========isMember=============={}", redisTemplate.opsForSet().isMember("setKey","bbbb"));
        log.info("=========size=================={}", redisTemplate.opsForSet().size("setKey"));
        //pop是随机弹出某个元素,弹出后就不存在了
        log.info("=========pop==================={}", redisTemplate.opsForSet().pop("setKey3"));
        log.info("=========members3=============={}", redisTemplate.opsForSet().members("setKey3"));
        //不存在就返回0,存在则返回1
        log.info("=========remove================{}", redisTemplate.opsForSet().remove("setKey3","hhhhhhh"));
        log.info("=========move=================={}", redisTemplate.opsForSet().move("setKey1","eeee","setKey2"));
        log.info("=========members4=============={}", redisTemplate.opsForSet().members("setKey2"));
        log.info("=========randomMember=========={}", redisTemplate.opsForSet().randomMember("setKey"));
        log.info("=========members5=============={}", redisTemplate.opsForSet().members("setKey"));
        log.info("=========intersect============={}", redisTemplate.opsForSet().intersect("setKey","setKey3"));
        log.info("=========intersectAndStore====={}", redisTemplate.opsForSet().intersectAndStore("setKey","setKey3","setKey-setKey3-intersect"));
        log.info("=========members6=============={}", redisTemplate.opsForSet().members("setKey-setKey3-intersect"));
    }

命令

redisTemplate

说明

hset redishash name tom

redisTemplate.opsForHash().put(“redishash”,“name”,“tom”)

将哈希表 key 中的字段 field 的值设为 value

hmset hashMap name:lily age:18

redisTemplate.opsForHash().putAll(“hashMap”,map)

同时将多个 field-value (域-值)对设置到哈希表 key 中

hmget hashMap

redisTemplate.opsForHash().entries(“hashMap”)

获取所有给定字段的值

hget redishash name

redisTemplate.opsForHash().get(“redishash”,“name”)

获取存储在哈希表中指定字段的值

hincrby hincrby number 10

redisTemplate.opsForHash().increment(“hincrby”,“number”,10L)

为哈希表 key 中的指定字段的整数值加上增量 increment

hsetnx redishash1 hash absent

redisTemplate.opsForHash().putIfAbsent(“redishash1”,“hash”,“absent”)

只有在字段 field 不存在时,设置哈希表字段的值

hdel redishash name

redisTemplate.opsForHash().delete(“redishash”,“name”)

删除一个或多个哈希表字段

hkeys hashMap

redisTemplate.opsForHash().keys(“hashMap”)

获取所有哈希表中的字段

hlen hashMap

redisTemplate.opsForHash().size(“hashMap”)

获取哈希表中字段的数量

hexists “hincrby” “number”

redisTemplate.opsForHash().hasKey(“hincrby”,“number”)

查看哈希表 key 中,指定的字段是否存在

*说明:有些可能有遗漏,或者没有对应,如有错误之处还望指正。

ZSet操作

public void testRedisZSetCommand(){
        ZSetOperations.TypedTuple<String> objectTypedTuple1 = new DefaultTypedTuple<String>("zset-1",9.1);
        ZSetOperations.TypedTuple<String> objectTypedTuple2 = new DefaultTypedTuple<String>("zset-2",9.2);
        ZSetOperations.TypedTuple<String> objectTypedTuple3 = new DefaultTypedTuple<String>("zset-3",9.3);
        ZSetOperations.TypedTuple<String> objectTypedTuple4 = new DefaultTypedTuple<String>("zset-4",9.4);
        ZSetOperations.TypedTuple<String> objectTypedTuple5 = new DefaultTypedTuple<String>("zset-5",9.5);
        ZSetOperations.TypedTuple<String> objectTypedTuple6 = new DefaultTypedTuple<String>("zset-6",9.6);
        Set<ZSetOperations.TypedTuple<String>> tuples = new HashSet<ZSetOperations.TypedTuple<String>>();
        tuples.add(objectTypedTuple1);
        tuples.add(objectTypedTuple2);
        tuples.add(objectTypedTuple3);
        tuples.add(objectTypedTuple4);
        tuples.add(objectTypedTuple5);
        tuples.add(objectTypedTuple6);

        ZSetOperations.TypedTuple<String> tuples1 = new DefaultTypedTuple<String>("zset-1",8.1);
        ZSetOperations.TypedTuple<String> tuples2 = new DefaultTypedTuple<String>("zset-2",8.2);
        ZSetOperations.TypedTuple<String> tuples3 = new DefaultTypedTuple<String>("zset-3",8.3);
        Set<ZSetOperations.TypedTuple<String>> stuples1 = new HashSet<ZSetOperations.TypedTuple<String>>();
        stuples1.add(objectTypedTuple1);
        stuples1.add(objectTypedTuple2);
        stuples1.add(objectTypedTuple3);

        log.info("======add=zset1============{}",redisTemplate.opsForZSet().add("zset1",tuples));
        log.info("======add=zset2============{}",redisTemplate.opsForZSet().add("zset2",stuples1));
        log.info("======range1==============={}",redisTemplate.opsForZSet().range("zset1",0,-1));
        log.info("======zCard================{}",redisTemplate.opsForZSet().zCard("zset1"));
        log.info("======incrementScore======={}",redisTemplate.opsForZSet().incrementScore("zset1","zset-1",1.1));
        log.info("======intersectAndStore===={}",redisTemplate.opsForZSet().intersectAndStore("zset1","zset2", "zset1-zset2-intersect"));
        log.info("======range21=============={}",redisTemplate.opsForZSet().range("zset1-zset2-intersect",0,-1));
        log.info("======rangeByScore========={}",redisTemplate.opsForZSet().rangeByScore("zset2",8.1,8.3));
        log.info("======score================{}",redisTemplate.opsForZSet().score("zset2","zset-1"));
        log.info("======reverseRange========={}",redisTemplate.opsForZSet().reverseRange("zset1",0,-1));
        log.info("======range2==============={}",redisTemplate.opsForZSet().range("zset1",0,-1));
        log.info("======reverseRangeByScore=={}",redisTemplate.opsForZSet().reverseRangeByScore("zset1",9.4,9.2));
        log.info("======range3==============={}",redisTemplate.opsForZSet().range("zset1",0,-1));
        log.info("======reverseRank=========={}",redisTemplate.opsForZSet().reverseRank("zset1","zset-3"));
        log.info("======range4==============={}",redisTemplate.opsForZSet().range("zset1",0,-1));
        log.info("======count================{}",redisTemplate.opsForZSet().count("zset1",9.2,9.6));
        log.info("======unionAndStore========{}",redisTemplate.opsForZSet().unionAndStore("zset1","zset2","zset1-zset2-union"));
        log.info("======range5==============={}",redisTemplate.opsForZSet().range("zset1-zset2-union",0,-1));
        log.info("======rank================={}",redisTemplate.opsForZSet().rank("zset1","zset-2"));
        log.info("======remove==============={}",redisTemplate.opsForZSet().remove("zset1","zset-2"));
        log.info("======range6==============={}",redisTemplate.opsForZSet().range("zset1",0,-1));
        log.info("======removeRangeByScore==={}",redisTemplate.opsForZSet().removeRangeByScore("zset1-zset2-union",9.1,9.4));
        log.info("======range7==============={}",redisTemplate.opsForZSet().range("zset1",0,-1));
        log.info("======size================={}",redisTemplate.opsForZSet().size("zset1"));

命令

redisTemplate

说明

zadd zset1 9.1 zset-1 9.2 zset-2 9.3 zset-3 9.4 zset-4 9.5 zset-6

redisTemplate.opsForZSet().add(“zset2”,stuples1)

向有序集合添加一个或多个成员,或者更新已存在成员的分数

zrange zset1 0 -1

redisTemplate.opsForZSet().range(“zset1”,0,-1)

通过索引区间返回有序集合成指定区间内的成员

zcard zset1

redisTemplate.opsForZSet().zCard(“zset1”)

获取有序集合的成员数

zincrby zset-1 1.1 zset-1

redisTemplate.opsForZSet().incrementScore(“zset1”,“zset-1”,1.1)

有序集合中对指定成员的分数加上增量 increment

zinterstore zset1-zset2-intersect 2 zset1 zset2

redisTemplate.opsForZSet().intersectAndStore(“zset1”,“zset2”, “zset1-zset2-intersect”)

计算给定的一个或多个有序集的交集并将结果集存储在新的有序集合 key 中

zrangebyscore

redisTemplate.opsForZSet().rangeByScore(“zset2”,8.1,8.3)

通过分数返回有序集合指定区间内的成员

zscore zset1 zset-2

redisTemplate.opsForZSet().score(“zset1”,“zset-2”)

返回有序集中,成员的分数值

zrevrange zset1 0 -1

redisTemplate.opsForZSet().reverseRange(“zset1”,0,-1)

返回有序集中指定区间内的成员,通过索引,分数从高到底

zrevrangebyscore zset1 9.4 9.2

redisTemplate.opsForZSet().reverseRangeByScore(“zset1”,9.4,9.2)

返回有序集中指定分数区间内的成员,分数从高到低排序

zrevrank zset1 zset-3

redisTemplate.opsForZSet().reverseRank(“zset1”,“zset-3”)

返回有序集合中指定成员的排名,有序集成员按分数值递减(从大到小)排序

zcount zset1 9.2 9.6

redisTemplate.opsForZSet().count(“zset1”,9.2,9.6)

计算在有序集合中指定区间分数的成员数

zunionstore zset1-zset2-union 2 zset1 zset2

redisTemplate.opsForZSet().unionAndStore(“zset1”,“zset2”,“zset1-zset2-union”)

计算给定的一个或多个有序集的并集,并存储在新的 key 中

zrank zset1 zset-2

redisTemplate.opsForZSet().rank(“zset1”,“zset-2”)

返回有序集合中指定成员的索引

zrem zset1 zset-2

redisTemplate.opsForZSet().remove(“zset1”,“zset-2”)

移除有序集合中的一个或多个成员

zremrangebyscore zset1-zset2-union 9.1 9.4

redisTemplate.opsForZSet().removeRangeByScore(“zset1-zset2-union”,9.1,9.4)

移除有序集合中给定的分数区间的所有成员

zcard zset1

redisTemplate.opsForZSet().size(“zset1”)

在有序集合中计算指定字典区间内成员数量

*说明:有些可能有遗漏,或者没有对应,如有错误之处还望指正。

断断续续从命令到代码,都是自己一个字一个字的打出来的,其中必然少不了出错,聪明的你看到错误千万不要吝啬你的指正,我在这里等你。。。。。

------------------------与君共勉--------------------------