springBoot整合Redis(一、Jedis操作Redis)

news2025/7/19 8:33:27

在springboot环境下连接redis的方法有很多,首先最简单的就是直接通过jedis类来连接,jedis类就相当于是redis的客户端表示。

但是因为现在比较常用的是:StringRedisTemplate和RedisTemplate,所以jedis只做简单的介绍。

一、Jedis连接方式

        1) Jedis直接链接Redis服务器

        第一步:先引入Jedis的依赖:

        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>3.2.0</version>
        </dependency>

        第二步  直接连接:

    public static void main(String[] args) {
        
        //连接本地的 Redis 服务
        Jedis jedis = new Jedis("localhost",6379);
        // 如果 Redis 服务设置了密码,需要用下面这行代码输入密码
         jedis.auth("123456");
        System.out.println("连接成功");
        //查看服务是否运行
        System.out.println("服务正在运行: "+jedis.ping());
        
    }

显示已经连接成功!

 2)通过连接池的方式获取 Jedis

        第一步仍然是引入依赖。

        第二步代码实现:

    public static void main(String[] args) {

        // 创建Jedis连接池配置对象
        JedisPoolConfig config = new JedisPoolConfig();

        // 最大连接数
        config.setMaxTotal(30);
        // 最大空闲连接数
        config.setMaxIdle(10);
        // 最小空闲连接数
        config.setMinIdle(5);
        // 最长等待毫秒数
        config.setMaxWaitMillis(3000);

//        连接到 redis 服务器上
        JedisPool jedisPool = new JedisPool(new JedisPoolConfig(),
                "localhost", 6379, 10000, "123456");

        for (int i = 1; i <=31; i++) {
            Jedis jedis = jedisPool.getResource();
            System.out.println(jedis);
//            jedis.close();          // 将连接归还到连接池
        }

        jedisPool.close();          // 释放资源

    }

    需要先创建JedisPoolConfig 连接池配置对象来进行配置,然后再使用JedisPool 进行连接。

        3)使用工厂模式连接Redis 

        一般我们不会这个频繁的创建Redis连接,正常情况下会使用工厂模式进行创建,这样会方便很多。

        1)首先创建配置文件,将Jedis的各种配置信息,全都放到配置文件中设置        

redis:
  host: localhost
  port: 6379
  database: 1
  #连接超时时间
  timeout: 10
  password: 123456
  #最大连接数
  poolMaxTotal=: 10
  #最大空闲连接数
  poolMaxIdle: 5
  #最大等待连接数
  poolMaxWait: 3

         2)创建配置类:

@Component
@ConfigurationProperties(prefix = "redis")
@Data
public class RedisConfig {

    private String host;
    private int port;
    private int timeout;//秒
    private String password;
    private int poolMaxTotal;
    private int poolMaxIdle;
    private int  poolMaxWait;//秒
    private int database;
}
@Configuration
public class JedisPoolFactory {

    @Autowired
    RedisConfig redisConfig;

    @Bean
    public JedisPool JedisPoolFactory(){

        System.out.println(redisConfig.toString());
        JedisPoolConfig poolConfig=new JedisPoolConfig();
        poolConfig.setMaxIdle(redisConfig.getPoolMaxIdle());
        poolConfig.setMaxTotal(redisConfig.getPoolMaxTotal());
        poolConfig.setMaxWaitMillis(redisConfig.getPoolMaxWait()*1000);
        JedisPool jp=new JedisPool(poolConfig,redisConfig.getHost(),redisConfig.getPort()
                ,redisConfig.getTimeout()*1000,redisConfig.getPassword(),redisConfig.getDatabase());
        return jp;
    }
}

        3)像上面创建完配置类之后就可以直接使用Jedis了,示例如下:

@RestController
@RequestMapping("/redis")
public class RedisController {


    @Autowired
    JedisPool jedisPool;

    @GetMapping("/demo")
    public void name(@RequestParam("name") String name){
        
        Jedis jedis = jedisPool.getResource();
        jedis.set("name", name);
        jedis.close();
    }
}

         这是最简单的使用示例。

        4) 哨兵模式连接Redis

        1.首先还是要创建配置文件跟配置类,只不过属性要稍微改一下

redis:
  sentinelHost: localhost1:26379,localhost2:localhost:26379
  masterName: mymaster
#  host: host
#  port: 6379
  database: 1
  #连接超时时间
  timeout: 30
  password: 123456
  #最大连接数
  poolMaxTotal=: 10
  #最大空闲连接数
  poolMaxIdle: 5
  #最大等待连接数
  poolMaxWait: 3

        配置类:

@Component
@ConfigurationProperties(prefix = "redis")
@Data
public class RedisConfig {

    private String  sentinelHost;
    private String masterName;
//    private int port;
    private int timeout;//秒
    private String password;
    private int poolMaxTotal;
    private int poolMaxIdle;
    private int  poolMaxWait;//秒
    private int database;
    
}

        2.创建连接池:

@Configuration
public class JedisPoolFactory {

    @Autowired
    RedisConfig redisConfig;

    @Bean
    public JedisSentinelPool JedisPoolFactory(){

        System.out.println(redisConfig.toString());
        JedisPoolConfig poolConfig=new JedisPoolConfig();
        poolConfig.setMaxIdle(redisConfig.getPoolMaxIdle());
        poolConfig.setMaxTotal(redisConfig.getPoolMaxTotal());
        poolConfig.setMaxWaitMillis(redisConfig.getPoolMaxWait()*1000);
        // 哨兵信息,注意填写哨兵的地址
        String[] host = redisConfig.getSentinelHost().split(",");
        Set<String> sentinels = new HashSet<>(Arrays.asList(host));
        // 创建连接池
        JedisSentinelPool jp = new JedisSentinelPool(redisConfig.getMasterName(),
                sentinels,poolConfig,2000, redisConfig.getPassword(),redisConfig.getDatabase());
        
        return jp;
    }
}

          4) 集群模式连接:

                都差不多,只简单说一下如何创建连接池:

public static void testCluster() {
    // 配置参数,根据服务器情况设置连接参数
    JedisPoolConfig config = new JedisPoolConfig();
    config.setMaxIdle(100);
    // 添加集群master地址
    Set<HostAndPort> jedisClusterSet = new HashSet<>();
    jedisClusterSet.add(new HostAndPort("192.168.242.128", 8201));
    jedisClusterSet.add(new HostAndPort("192.168.242.128", 8202));
    jedisClusterSet.add(new HostAndPort("192.168.242.128", 8203));
    // 连接集群
    JedisCluster jedisCluster = new JedisCluster(jedisClusterSet, config);
    // r如果带密码
    //JedisCluster jedisCluster  = new JedisCluster(jedisClusterSet,1000, 1000, 5, password, config);
    // 设置key
    jedisCluster.set("okayjam", "www.okayjam.com");
    // 读取key
    System.out.println(jedisCluster.get("okayjam"));
}

二、Jedis的操作:

       1)Jedis对key的操作
            jedis.flushDB();// 清空数据
            jedis.echo("hello");

            // 判断key否存在
            jedis.exists("foo");


            // 如果数据库没有任何key,返回nil,否则返回数据库中一个随机的key。
            String randomKey = jedis.randomKey();
            System.out.println("randomKey: " + randomKey);

            // 设置60秒后该key过期
            jedis.expire("key", 60);

            // key有效毫秒数
            System.out.println(jedis.pttl("key"));

            // 移除key的过期时间
            jedis.persist("key");

            // 获取key的类型, "string", "list", "set". "none" none表示key不存在
            System.out.println("type: " + jedis.type("key"));

            // 导出key的值
            byte[] bytes = jedis.dump("key");
            System.out.println(new String(bytes));

            // 将key重命名
            jedis.renamenx("key", "keytest");
            System.out.println("key是否存在: " + jedis.exists("key"));// 判断是否存在
            System.out.println("keytest是否存在: " + jedis.exists("keytest"));// 判断是否存在

            // 查询匹配的key
            // KEYS       * 匹配数据库中所有 key 。
            // KEYS       h?llo 匹配 hello , hallo 和 hxllo 等。
            // KEYS       h*llo 匹配 hllo 和 heeeeello 等。
            // KEYS       h[ae]llo 匹配 hello 和 hallo ,但不匹配 hillo 。
            // 特殊符号用 \ 隔开。
            Set<String> set = jedis.keys("k*");
            System.out.println(set);

            // 删除key
            jedis.del("key");
            System.out.println(jedis.exists("key"));
        2) Jedis对字符串(String)的相关操作
        jedis.set("hello", "hello");

        
        // 使用append 向字符串后面添加
        jedis.append("hello", " world");
        System.out.println(jedis.get("hello"));
        
        // set覆盖字符串
        jedis.set("hello", "123");
        System.out.println(jedis.get("hello"));
        
        // 设置过期时间
        jedis.setex("hello2", 2, "world2");
        System.out.println(jedis.get("hello2"));
        
        // 一次添加多个key-value对
        jedis.mset("a", "1", "b", "2");
        // 获取a和b的value
        List<String> valus = jedis.mget("a", "b");
        System.out.println(valus);
        
        // 批量删除
        jedis.del("a", "b");
        System.out.println(jedis.exists("a"));
        System.out.println(jedis.exists("b"));
        

        3)Jedis对链表(Lists)的操作
            String key = "mylist";
            jedis.del(key);
            
            // 队列添加元素
            jedis.rpush(key, "aaaa");
            jedis.rpush(key, "aaaa");
            jedis.rpush(key, "bbbb");
            jedis.rpush(key, "cccc");
            jedis.rpush(key, "cccc");
            
            // 队列长度
            System.out.println("lenth: " + jedis.llen(key));
            
            // 打印队列,从索引0开始,到倒数第1个(全部元素)
            System.out.println("all elements: " + jedis.lrange(key, 0, -1));
            
            // 索引为1的元素
            System.out.println("index of 1: " + jedis.lindex(key, 1));
            
            // 设置队列里面一个元素的值,当index超出范围时会返回一个error。
            jedis.lset(key, 1, "aa22");
            System.out.println("index of 1: " + jedis.lindex(key, 1));
            
            // 从队列的右边入队一个元素
            jedis.rpush(key, "-2", "-1");// 先-2,后-1入队列
            System.out.println("all elements: " + jedis.lrange(key, 0, -1));
            
            // 从队列的左边入队一个或多个元素
            jedis.lpush(key, "second element", "first element");// 先second
            // element,后first
            // elementF入队列
            System.out.println("all elements: " + jedis.lrange(key, 0, -1));
            
            // 从队列的右边出队一个元素
            System.out.println(jedis.rpop(key));
            // 从队列的左边出队一个元素
            System.out.println(jedis.lpop(key));
            System.out.println("all elements: " + jedis.lrange(key, 0, -1));
            
            // count > 0: 从头往尾移除值为 value 的元素,count为移除的个数。
            // count < 0: 从尾往头移除值为 value 的元素,count为移除的个数。
            // count = 0: 移除所有值为 value 的元素。
            jedis.lrem(key, 1, "cccc");
            System.out.println("all elements: " + jedis.lrange(key, 0, -1));
            
            // 即最右边的那个元素也会被包含在内。 如果start比list的尾部下标大的时候,会返回一个空列表。
            // 如果stop比list的实际尾部大的时候,Redis会当它是最后一个元素的下标。
            System.out.println(jedis.lrange(key, 0, 2));
            System.out.println("all elements: " + jedis.lrange(key, 0, -1));
            
            // 删除区间以外的元素
            System.out.println(jedis.ltrim(key, 0, 2));
            System.out.println("all elements: " + jedis.lrange(key, 0, -1));
        4)Jedis对集合(Sets)的操作
            // 清空数据
            System.out.println(jedis.flushDB());
            String key = "myset";
            String key2 = "myset2";
            
            // 集合添加元素
            jedis.sadd(key, "aaa", "bbb", "ccc");
            jedis.sadd(key2, "bbb", "ccc", "ddd");
            
            // 获取集合里面的元素数量
            System.out.println(jedis.scard(key));
            
            // 获得两个集合的交集,并存储在一个关键的结果集
            jedis.sinterstore("destination", key, key2);
            System.out.println(jedis.smembers("destination"));
            
            // 获得两个集合的并集,并存储在一个关键的结果集
            jedis.sunionstore("destination", key, key2);
            System.out.println(jedis.smembers("destination"));
            
            // key集合中,key2集合没有的元素,并存储在一个关键的结果集
            jedis.sdiffstore("destination", key, key2);
            System.out.println(jedis.smembers("destination"));
            
            // 确定某个元素是一个集合的成员
            System.out.println(jedis.sismember(key, "aaa"));
            
            // 从key集合里面随机获取一个元素
            System.out.println(jedis.srandmember(key));
            
            // aaa从key移动到key2集合
            jedis.smove(key, key2, "aaa");
            System.out.println(jedis.smembers(key));
            System.out.println(jedis.smembers(key2));
            
            // 删除并获取一个集合里面的元素
            System.out.println(jedis.spop(key));
            
            // 从集合里删除一个或多个元素
            jedis.srem(key2, "ccc", "ddd");
            System.out.println(jedis.smembers(key2));
        5)Jedis对有序集合(Sorted Sets)的操作 
            // 清空数据
            System.out.println(jedis.flushDB());
            String key = "mysortset";
            
            Map<String, Double> scoreMembers = new HashMap<String, Double>();
            scoreMembers.put("aaa", 1001.0);
            scoreMembers.put("bbb", 1002.0);
            scoreMembers.put("ccc", 1003.0);
            
            // 添加数据
            jedis.zadd(key, 1004.0, "ddd");
            jedis.zadd(key, scoreMembers);
            
            // 获取一个排序的集合中的成员数量
            System.out.println(jedis.zcard(key));
            
            // 返回的成员在指定范围内的有序集合,以0表示有序集第一个成员,以1表示有序集第二个成员,以此类推。
            // 负数下标,以-1表示最后一个成员,-2表示倒数第二个成员
            Set<String> coll = jedis.zrange(key, 0, -1);
            System.out.println(coll);
            
            // 返回的成员在指定范围内的逆序集合
            coll = jedis.zrevrange(key, 0, -1);
            System.out.println(coll);
            
            // 元素下标
            System.out.println(jedis.zscore(key, "bbb"));
            
            // 删除元素
            System.out.println(jedis.zrem(key, "aaa"));
            System.out.println(jedis.zrange(key, 0, -1));
            
            // 给定值范围内的成员数
            System.out.println(jedis.zcount(key, 1002.0, 1003.0));
        6)Jedis对哈希(Hashs)的操作
            // 清空数据
            System.out.println(jedis.flushDB());
            String key = "myhash";
            Map<String, String> hash = new HashMap<String, String>();
            hash.put("aaa", "11");
            hash.put("bbb", "22");
            hash.put("ccc", "33");
            
            // 添加数据
            jedis.hmset(key, hash);
            jedis.hset(key, "ddd", "44");
            
            // 获取hash的所有元素(key值)
            System.out.println(jedis.hkeys(key));
            
            // 获取hash中所有的key对应的value值
            System.out.println(jedis.hvals(key));
            
            // 获取hash里所有元素的数量
            System.out.println(jedis.hlen(key));
            
            // 获取hash中全部的域和值,以Map<String, String> 的形式返回
            Map<String, String> elements = jedis.hgetAll(key);
            System.out.println(elements);
            
            // 判断给定key值是否存在于哈希集中
            System.out.println(jedis.hexists(key, "bbb"));
            
            // 获取hash里面指定字段对应的值
            System.out.println(jedis.hmget(key, "aaa", "bbb"));
            
            // 获取指定的值
            System.out.println(jedis.hget(key, "aaa"));
            
            // 删除指定的值
            System.out.println(jedis.hdel(key, "aaa"));
            System.out.println(jedis.hgetAll(key));
            
            // 为key中的域 field 的值加上增量 increment
            System.out.println(jedis.hincrBy(key, "bbb", 100));
            System.out.println(jedis.hgetAll(key));

三、Jedis操作封装类

        一般不直接从Jedis连接池中获取实例然后进行操作,一般会写好一个封装类,然后使用封装类进行操作,简单提供几个示例:
        

        1) 示例一

        例如平时常用的redis操作的get、set、exists、incr、decr方法,序列化的beanToString和stringToBean方法,用到的是import com.alibaba.fastjson.JSON;

@Service
public class RedisService {
 
    @Autowired
    JedisPool jedisPool;
 
    /**
     * 获取单个对象
     * @param prefix
     * @param key
     * @param clazz
     * @param <T>
     * @return
     */
    public <T> T get(KeyPrefix prefix,String key,Class<T> clazz){
        Jedis jedis =null;
        try {
            jedis = jedisPool.getResource();
            //生成真正的key
            String realKey =prefix.getPrefix()+key;
            String str = jedis.get(realKey);
            T t=stringToBean(str,clazz);
            return t;
 
        }finally {
            returnToPool(jedis);
        }
 
    }
 
    /**
     * 设置对象
     * @param prefix
     * @param key
     * @param value
     * @param <T>
     * @return
     */
    public <T> boolean set(KeyPrefix prefix,String key,T value){
        Jedis jedis =null;
        try {
            jedis = jedisPool.getResource();
            String str=beanToString(value);
            if (str==null||str.length()<=0)
                return false;
            //生成真正的key
            String realKey =prefix.getPrefix()+key;
            int seconds=prefix.expireSeconds();
            if (seconds<=0){
                jedis.set(realKey,str);
            } else {
                jedis.setex(realKey,seconds,str);
            }
            jedis.set(realKey, str);
            return true;
 
        }finally {
            returnToPool(jedis);
        }
 
    }
 
    /**
     * 判断是否存在
     * @param prefix
     * @param key
     * @param <T>
     * @return
     */
    public <T> boolean exists(KeyPrefix prefix,String key){
        Jedis jedis =null;
        try {
            jedis = jedisPool.getResource();
            //生成真正的key
            String realKey =prefix.getPrefix()+key;
            return jedis.exists(realKey);
 
        }finally {
            returnToPool(jedis);
        }
 
    }
 
    /**
     * 增加值
     * @param prefix
     * @param key
     * @param <T>
     * @return
     */
    public <T> Long incr(KeyPrefix prefix,String key){
        Jedis jedis =null;
        try {
            jedis = jedisPool.getResource();
            //生成真正的key
            String realKey =prefix.getPrefix()+key;
            return jedis.incr(realKey);
 
        }finally {
            returnToPool(jedis);
        }
 
    }
 
    /**
     * 减少值
     * @param prefix
     * @param key
     * @param <T>
     * @return
     */
    public <T> Long decr(KeyPrefix prefix,String key){
        Jedis jedis =null;
        try {
            jedis = jedisPool.getResource();
            //生成真正的key
            String realKey =prefix.getPrefix()+key;
            return jedis.decr(realKey);
 
        }finally {
            returnToPool(jedis);
        }
 
    }
 
 
 
    private <T> String beanToString(T value) {
        if (value==null)
            return null;
        Class<?> aClass = value.getClass();
        if (aClass==int.class||aClass==Integer.class){
            return ""+value;
        }else if (aClass==String.class){
            return (String) value;
        }else if (aClass==long.class||aClass==Long.class){
            return ""+value;
        }else {
            return JSON.toJSONString(value);
        }
    }
 
    private <T> T stringToBean(String str,Class<T> aClass) {
        if (str==null||str.length()<=0||aClass==null)
            return null;
 
 
        if (aClass==int.class||aClass==Integer.class){
            return (T)Integer.valueOf(str);
        }else if (aClass==String.class){
            return (T)str;
        }else if (aClass==long.class||aClass==Long.class){
            return (T)Long.valueOf(str);
        }else {
            return JSON.toJavaObject(JSON.parseObject(str),aClass);
        }
    }
 
    private void returnToPool(Jedis jedis) {
        if (jedis!=null){
            jedis.close();
        }
    }
 
 
 
}

        

        2) 示例二:
/**
 * 基于jedis的redis操作工具类
 * @author jyf
 * @time 2019/8/3 22:30
 */
public final class RedisUtils {
 
   /*
    除了该工具类提供的方法外,还可以在外面调用getJedis()方法,获取到jedis实例后,调用它原生的api来操作
     */
 
    /**
     * 获取jedis对象,并选择redis库。jedis默认是0号库,可传入1-16之间的数选择库存放数据
     * 原则上使用一个redis库存放数据,通过特定的key的命令规则来区分不同的数据就行了。
     *
     * @param index redis库号。使用可变参数的目的就是该参数可传可不传。
     * @return 返回jedis对象
     */
    public static Jedis getJedis(int... index) {
        Jedis jedis = MyJedisPoolConfig.getJedisPool().getResource();
        if (index != null && index.length > 0) {
            if (index[0] > 0 && index[0] <= 16){
                jedis.select(index[0]);
            }
        }
        return jedis;
    }
 
    /*########################  key的操作  ################################*/
 
    /**
     * 根据pattern返回当前库中的key
     *
     * @param pattern
     * @return
     */
    public static Set<String> keys(String pattern) {
        Jedis jedis = null;
        Set<String> keys = null;
        try {
            jedis = getJedis();
            keys = jedis.keys(pattern);
        }finally {
            if (jedis != null){
                jedis.close();
            }
        }
        return keys;
    }
 
    /**
     * 删除一个或多个key
     *
     * @param key 一个或多个key
     */
    public static Long del(String... key) {
        Jedis jedis = null;
        Long delNum = 0L;
        try {
            jedis = getJedis();
            delNum =jedis.del(key);
        }finally {
            if (jedis != null){
                jedis.close();
            }
        }
        return delNum;
    }
 
    /**
     * 批量删除
     * @param keyList 要删除的key的集合
     */
    public static void mdel(List<String> keyList){
        Jedis jedis = getJedis();
        //获取pipeline
        Pipeline pipeline = jedis.pipelined();
        for (String key : keyList) {
            pipeline.del(key);
        }
        //执行结果同步,这样才能保证结果的正确性。实际上不执行该方法也执行了上面的命令,但是结果确不一定完全正确。
        //注意
        pipeline.sync();
        //关闭连接
        jedis.close();
    }
 
    /**
     * 判断某个key是否还存在
     *
     * @param key key
     * @return
     */
    public static Boolean exists(String key) {
        Jedis jedis = null;
        Boolean flag = false;
        try {
            jedis = getJedis();
            flag = jedis.exists(key);
        }finally {
            if (jedis != null){
                jedis.close();
            }
        }
        return flag;
    }
 
    /**
     * 设置某个key的过期时间,单位秒
     *
     * @param key key
     * @param seconds 过期时间秒
     */
    public static void expire(String key, int seconds) {
        Jedis jedis = null;
        try {
            jedis = getJedis();
            jedis.expire(key, seconds);
        }finally {
            if (jedis != null){
                jedis.close();
            }
        }
    }
 
    /**
     * 查看某个key还有几秒过期,-1表示永不过期 ,-2表示已过期
     *
     * @param key key
     * @return
     */
    public static Long timeToLive(String key) {
        Jedis jedis = null;
        Long ttl;
        try {
            jedis = getJedis();
            ttl = jedis.ttl(key);
        }finally {
            if (jedis != null){
                jedis.close();
            }
        }
        return ttl;
    }
 
    /**
     * 查看某个key对应的value的类型
     *
     * @param key
     * @return
     */
    public static String type(String key) {
        Jedis jedis = null;
        String type = null;
        try {
            jedis = getJedis();
            type = jedis.type(key);
        }finally {
            if (jedis != null){
                jedis.close();
            }
        }
        return type;
    }
 
 
    /*########################  string(字符串)的操作  ####################*/
 
    /**
     * 获取某个key的value,类型要对,只能value是string的才能获取
     *
     * @param key
     * @return
     */
    public static String get(String key) {
        Jedis jedis = null;
        String value = null;
        try {
            jedis = getJedis();
            value = jedis.get(key);
        }finally {
            if (jedis != null){
                jedis.close();
            }
        }
        return value;
    }
 
    /**
     * 设置某个key的value
     *
     * @param key
     * @param value
     */
    public static void set(String key, String value) {
        Jedis jedis = null;
        try {
            jedis = getJedis();
            jedis.set(key, value);
        }finally {
            if (jedis != null){
                jedis.close();
            }
        }
    }
 
    /**
     * 字符串后追加内容
     *
     * @param key key
     * @param appendContent 要追加的内容
     */
    public static void append(String key, String appendContent) {
        Jedis jedis = null;
        try {
            jedis = getJedis();
            jedis.append(key, appendContent);
        }finally {
            if (jedis != null){
                jedis.close();
            }
        }
    }
 
    /**
     * 返回key的value的长度
     *
     * @param key
     * @return
     */
    public static Long strlen(String key) {
        Jedis jedis = null;
        Long strLen = 0L;
        try {
            jedis = getJedis();
            strLen = jedis.strlen(key);
        }finally {
            if (jedis != null){
                jedis.close();
            }
        }
        return strLen;
    }
 
    /**
     * value 加1 必
     * 须是字符型数字
     * @param key
     * @return 增加后的值
     */
    public static Long incr(String key) {
        Jedis jedis = null;
        Long incrResult = 0L;
        try {
            jedis = getJedis();
            incrResult = jedis.incr(key);
        }finally {
            if (jedis != null){
                jedis.close();
            }
        }
        return incrResult;
    }
 
    /**
     * value 减1   必须是字符型数字
     *
     * @param key
     * @return
     */
    public static Long decr(String key) {
        Jedis jedis = null;
        Long decrResult = 0L;
        try {
            jedis = getJedis();
            decrResult = jedis.decr(key);
        }finally {
            if (jedis != null){
                jedis.close();
            }
        }
        return decrResult;
    }
 
    /**
     * value 加increment
     *
     * @param key key
     * @param increment 加几
     * @return
     */
    public static Long incrby(String key, int increment) {
        Jedis jedis = null;
        Long incrByResult = 0L;
        try {
            jedis = getJedis();
            incrByResult = jedis.incrBy(key, increment);
        }finally {
            if (jedis != null){
                jedis.close();
            }
        }
        return incrByResult;
    }
 
    /**
     * value 减increment
     *
     * @param key
     * @param increment
     * @return
     */
    public static Long decrby(String key, int increment) {
        Jedis jedis = null;
        Long decrByResult = 0L;
        try {
            jedis = getJedis();
            decrByResult = jedis.decrBy(key, increment);
        }finally {
            if (jedis != null){
                jedis.close();
            }
        }
        return decrByResult;
    }
 
    /**
     * 给某个key设置过期时间和value,成功返回OK
     *
     * @param key key
     * @param seconds 过期时间秒
     * @param value 设置的值
     * @return
     */
    public static String setex(String key, int seconds, String value) {
        Jedis jedis = null;
        String result = null;
        try {
            jedis = getJedis();
            result = jedis.setex(key, seconds, value);
        }finally {
            if (jedis != null){
                jedis.close();
            }
        }
        return result;
    }
 
 
    /*########################  list(列表)的操作  #######################*/
    //lpush rpush lpop rpop lrange lindex llen lset
 
    /**
     * 从左边向列表中添加值
     *
     * @param key key
     * @param str 要添加的值
     */
    public static void lpush(String key, String str) {
        Jedis jedis = null;
        try {
            jedis = getJedis();
            jedis.lpush(key, str);
        }finally {
            if (jedis != null){
                jedis.close();
            }
        }
    }
 
    /**
     * 从右边向列表中添加值
     *
     * @param key key
     * @param str 要添加的值
     */
    public static void rpush(String key, String str) {
        Jedis jedis = null;
        try {
            jedis = getJedis();
            jedis.rpush(key, str);
        }finally {
            if (jedis != null){
                jedis.close();
            }
        }
    }
 
    /**
     * 从左边取出一个列表中的值
     *
     * @param key
     * @return
     */
    public static String lpop(String key) {
        Jedis jedis = null;
        String lpop = null;
        try {
            jedis = getJedis();
            lpop = jedis.lpop(key);
        }finally {
            if (jedis != null){
                jedis.close();
            }
        }
        return lpop;
    }
 
    /**
     * 从右边取出一个列表中的值
     *
     * @param key
     * @return
     */
    public static String rpop(String key) {
        Jedis jedis = null;
        String rpop = null;
        try {
            jedis = getJedis();
            rpop = jedis.rpop(key);
        }finally {
            if (jedis != null){
                jedis.close();
            }
        }
        return rpop;
    }
 
    /**
     * 取出列表中指定范围内的值,0 到 -1 表示全部
     *
     * @param key
     * @param startIndex
     * @param endIndex
     * @return
     */
    public static List<String> lrange(String key, int startIndex, int endIndex) {
        Jedis jedis = null;
        List<String> result = null;
        try {
            jedis = getJedis();
            result = jedis.lrange(key, startIndex, endIndex);
        }finally {
            if (jedis != null){
                jedis.close();
            }
        }
        return result;
    }
 
    /**
     * 返回某列表指定索引位置的值
     *
     * @param key 列表key
     * @param index 索引位置
     * @return
     */
    public static String lindex(String key, int index) {
        Jedis jedis = null;
        String lindex = null;
        try {
            jedis = getJedis();
            lindex = jedis.lindex(key, index);
        }finally {
            if (jedis != null){
                jedis.close();
            }
        }
        return lindex;
    }
 
    /**
     * 返回某列表的长度
     *
     * @param key
     * @return
     */
    public static Long llen(String key) {
        Jedis jedis = null;
        Long llen = 0L;
        try {
            jedis = getJedis();
            llen = jedis.llen(key);
        }finally {
            if (jedis != null){
                jedis.close();
            }
        }
        return llen;
    }
 
    /**
     * 给某列表指定位置设置为指定的值
     *
     * @param key
     * @param index
     * @param str
     */
    public static void lset(String key, Long index, String str) {
        Jedis jedis = null;
        try {
            jedis = getJedis();
            jedis.lset(key, index, str);
        }finally {
            if (jedis != null){
                jedis.close();
            }
        }
    }
 
    /**
     * 对列表进行剪裁,保留指定闭区间的元素(索引位置也会重排)
     * @param key 列表key
     * @param startIndex 开始索引位置
     * @param endIndex 结束索引位置
     */
    public static void ltrim(String key,Integer startIndex,Integer endIndex){
        Jedis jedis = null;
        try {
            jedis = getJedis();
            jedis.ltrim(key, startIndex, endIndex);
        }finally {
            if (jedis != null){
                jedis.close();
            }
        }
    }
 
    /**
     * 从列表的左边阻塞弹出一个元素
     * @param key 列表的key
     * @param timeout 阻塞超时时间,0表示若没有元素就永久阻塞
     * @return
     */
    public static List<String> blpop(String key,Integer timeout){
        Jedis jedis = null;
        List<String> valueList = null;
        try {
            jedis = getJedis();
            valueList = jedis.blpop(timeout, key);
        }finally {
            if (jedis != null){
                jedis.close();
            }
        }
        return valueList;
    }
 
    /**
     * 从列表的右边阻塞弹出一个元素
     * @param key 列表的key
     * @param timeout 阻塞超时时间,0表示若没有元素就永久阻塞
     * @return
     */
    public static List<String> brpop(String key,Integer timeout){
        Jedis jedis = null;
        List<String> valueList = null;
        try {
            jedis = getJedis();
            valueList = jedis.brpop(timeout, key);
        }finally {
            if (jedis != null){
                jedis.close();
            }
        }
        return valueList;
    }
 
 
    /*########################  hash(哈希表)的操作  #######################*/
    //hset hget hmset hmget hgetall hdel hkeys hvals hexists hincrby
 
    /**
     * 给某个hash表设置一个键值对
     *
     * @param key
     * @param field
     * @param value
     */
    public static void hset(String key, String field, String value) {
        Jedis jedis = null;
        try {
            jedis = getJedis();
            jedis.hset(key, field, value);
        }finally {
            if (jedis != null){
                jedis.close();
            }
        }
    }
 
    /**
     * 取出某个hash表中某个field对应的value
     *
     * @param key key
     * @param field field
     * @return
     */
    public static String hget(String key, String field) {
        Jedis jedis = null;
        String hget = null;
        try {
            jedis = getJedis();
            hget = jedis.hget(key, field);
        }finally {
            if (jedis != null){
                jedis.close();
            }
        }
        return hget;
    }
 
    /**
     * 某个hash表设置一个或多个键值对
     *
     * @param key
     * @param kvMap
     */
    public static void hmset(String key, Map<String, String> kvMap) {
        Jedis jedis = null;
        try {
            jedis = getJedis();
            jedis.hmset(key, kvMap);
        }finally {
            if (jedis != null){
                jedis.close();
            }
        }
    }
 
    /**
     * 取出某个hash表中任意多个key对应的value的集合
     *
     * @param key
     * @param fields
     * @return
     */
    public static List<String> hmget(String key, String... fields) {
        Jedis jedis = null;
        List<String> hmget = null;
        try {
            jedis = getJedis();
            hmget = jedis.hmget(key, fields);
        }finally {
            if (jedis != null){
                jedis.close();
            }
        }
        return hmget;
    }
 
    /**
     * 取出某个hash表中所有的键值对
     *
     * @param key
     * @return
     */
    public static Map<String, String> hgetall(String key) {
        Jedis jedis = null;
        Map<String, String> kvMap = null;
        try {
            jedis = getJedis();
            kvMap = jedis.hgetAll(key);
        }finally {
            if (jedis != null){
                jedis.close();
            }
        }
        return kvMap;
    }
 
    /**
     * 判断某个hash表中的某个key是否存在
     *
     * @param key
     * @param field
     * @return
     */
    public static Boolean hexists(String key, String field) {
        Jedis jedis = null;
        Boolean exists = null;
        try {
            jedis = getJedis();
            exists = jedis.hexists(key, field);
        }finally {
            if (jedis != null){
                jedis.close();
            }
        }
        return exists;
    }
 
    /**
     * 返回某个hash表中所有的key
     *
     * @param key
     * @return
     */
    public static Set<String> hkeys(String key) {
        Jedis jedis = null;
        Set<String> keys = null;
        try {
            jedis = getJedis();
            keys = jedis.hkeys(key);
        }finally {
            if (jedis != null){
                jedis.close();
            }
        }
        return keys;
    }
 
    /**
     * 返回某个hash表中所有的value
     *
     * @param key
     * @return
     */
    public static List<String> hvals(String key) {
        Jedis jedis = null;
        List<String> hvals = null;
        try {
            jedis = getJedis();
            hvals = jedis.hvals(key);
        }finally {
            if (jedis != null){
                jedis.close();
            }
        }
        return hvals;
    }
 
    /**
     * 删除某个hash表中的一个或多个键值对
     *
     * @param key
     * @param fields
     */
    public static void hdel(String key, String... fields) {
        Jedis jedis = null;
        try {
            jedis = getJedis();
            jedis.hdel(key, fields);
        }finally {
            if (jedis != null){
                jedis.close();
            }
        }
    }
 
    /**
     * 给某个hash表中的某个field的value增加多少
     *
     * @param key       hash表的key
     * @param field     表中的某个field
     * @param increment 增加多少
     * @return
     */
    public static Long hincrby(String key, String field, Long increment) {
        Jedis jedis = null;
        Long result = null;
        try {
            jedis = getJedis();
            result = jedis.hincrBy(key, field, increment);
        }finally {
            if (jedis != null){
                jedis.close();
            }
        }
        return result;
    }
 
    /*########################  set(集合)的操作  ###########################*/
 
    /**
     * 往set集合中添加一个或多个元素
     * @param key key
     * @param members 要添加的元素
     * @return 添加成功的元素个数
     */
    public static Long sadd(String key,String... members){
        Jedis jedis = null;
        Long num = 0L;
        try {
            jedis = getJedis();
            num = jedis.sadd(key, members);
        }finally {
            if (jedis != null){
                jedis.close();
            }
        }
        return num;
    }
 
    /**
     * 返回set集合中的所有元素,顺序与加入时的顺序一致
     * @param key key
     * @return
     */
    public static Set<String> smembers(String key){
        Jedis jedis = null;
        Set<String> members = null;
        try {
            jedis = getJedis();
            members = jedis.smembers(key);
        }finally {
            if (jedis != null){
                jedis.close();
            }
        }
        return members;
    }
 
    /**
     * 判断集合中是否存在某个元素
     * @param key key
     * @param member 某个元素
     * @return true存在,false不存在
     */
    public static Boolean sismember(String key,String member){
        Jedis jedis = null;
        Boolean isMember = false;
        try {
            jedis = getJedis();
            isMember = jedis.sismember(key, member);
        }finally {
            if (jedis != null){
                jedis.close();
            }
        }
        return isMember;
    }
 
    /**
     * 返回set集合的长度
     * @param key key
     * @return
     */
    public static Long scard(String key){
        Jedis jedis = null;
        Long len = 0L;
        try {
            jedis = getJedis();
            len = jedis.scard(key);
        }finally {
            if (jedis != null){
                jedis.close();
            }
        }
        return len;
    }
 
    /**
     * 删除set集合中指定的一个或多个元素
     * @param key
     * @param members 要删除的元素
     * @return 删除成功的元素个数
     */
    public static Long srem(String key,String... members){
        Jedis jedis = null;
        Long num = 0L;
        try {
            jedis = getJedis();
            num = jedis.srem(key,members);
        }finally {
            if (jedis != null){
                jedis.close();
            }
        }
        return num;
    }
 
    /**
     * 将key1中的元素key1Member移动到key2中
     * @param key1 来源集合key
     * @param key2 目的地集合key
     * @param key1Member key1中的元素
     * @return 1成功,0失败
     */
    public static Long smove(String key1,String key2,String key1Member){
        Jedis jedis = null;
        Long num = 0L;
        try {
            jedis = getJedis();
            num = jedis.smove(key1,key2,key1Member);
        }finally {
            if (jedis != null){
                jedis.close();
            }
        }
        return num;
    }
 
    /**
     * 随机查询返回集合中的指定个数的元素(若count为负数,返回的元素可能会重复)
     * @param key key
     * @param count 要查询返回的元素个数
     * @return 元素list集合
     */
    public static List<String> srandmember(String key,int count){
        Jedis jedis = null;
        List<String> members = null;
        try {
            jedis = getJedis();
            members = jedis.srandmember(key,count);
        }finally {
            if (jedis != null){
                jedis.close();
            }
        }
        return members;
    }
 
    /**
     * 从set集合中随机弹出指定个数个元素
     * @param key key
     * @param count 要弹出的个数
     * @return 随机弹出的元素
     */
    public static Set<String> spop(String key,int count){
        Jedis jedis = null;
        Set<String> members = null;
        try {
            jedis = getJedis();
            members = jedis.spop(key,count);
        }finally {
            if (jedis != null){
                jedis.close();
            }
        }
        return members;
    }
 
    /**
     * 求交集,返回多个set集合相交的部分
     * @param setKeys 多个set集合的key
     * @return 相交的元素集合
     */
    public static Set<String> sinter(String... setKeys){
        Jedis jedis = null;
        Set<String> members = null;
        try {
            jedis = getJedis();
            members = jedis.sinter(setKeys);
        }finally {
            if (jedis != null){
                jedis.close();
            }
        }
        return members;
    }
 
    /**
     * 求并集,求几个set集合的并集(因为set中不会有重复的元素,合并后的集合也不会有重复的元素)
     * @param setKeys 多个set的key
     * @return 合并后的集合
     */
    public static Set<String> sunion(String... setKeys){
        Jedis jedis = null;
        Set<String> members = null;
        try {
            jedis = getJedis();
            members = jedis.sunion(setKeys);
        }finally {
            if (jedis != null){
                jedis.close();
            }
        }
        return members;
    }
 
    /**
     * 求差集,求几个集合之间的差集
     * @param setKeys 多个set的key
     * @return 差集
     */
    public static Set<String> sdiff(String... setKeys){
        Jedis jedis = null;
        Set<String> members = null;
        try {
            jedis = getJedis();
            members = jedis.sdiff(setKeys);
        }finally {
            if (jedis != null){
                jedis.close();
            }
        }
        return members;
    }
 
 
    /*########################  zset(有序集合)的操作  #######################*/
 
    /**
     * 添加一个元素到zset
     * @param key key
     * @param score 元素的分数
     * @param member 元素
     * @return 成功添加的元素个数
     */
    public static Long zadd(String key,double score, String member){
        Jedis jedis = null;
        Long num = null;
        try {
            jedis = getJedis();
            num = jedis.zadd(key,score,member);
        }finally {
            if (jedis != null){
                jedis.close();
            }
        }
        return num;
    }
}

        简单举两个例子,可以仿照这个自己进行封装。

四、Jedis事务操作

#开启jedis事务

Transaction multi=jedis.multi();

#添加数据到redis中

multi.set("ka", "va");

#jedis事务提交

multi.exec();

#jedis事务回滚

multi.discard();

	Jedis jedis=new Jedis("127.0.0.1",6379);
	Transaction multi=jedis.multi();//开启redis事务
	try {
	//设置数据
		multi.set("ka", "va");
		multi.set("kb", "vb");
		int num=1/0;
		multi.set("kc", "vc");
	} catch (Exception e) {
		//multi.discard();
		e.printStackTrace();
	}
	//redis事务提交
	multi.exec();		
以上代码的结果是:redis成功保存ka,kb,不会保存kc。


Jedis jedis=new Jedis("127.0.0.1",6379);
//开启事务
	Transaction multi=jedis.multi();
	try {
		multi.set("ka", "va");
		multi.set("kb", "vb");
		int num=1/0;	
		multi.set("kc", "vc");
	} catch (Exception e) {
	//redis事务回滚
		multi.discard();
		e.printStackTrace();
	}
	//redis事务回滚
	multi.exec();	
	以上代码的结果是:redis不会保存ka,kb,kc.

五、Jedis 使用发布与订阅

整个过程可以分为以下几个步骤:

  • 创建Redis连接;
  • 创建订阅者;
  • 创建发布者;
  • 发布消息;
  • 接收消息。

1.使用jedis的publish(String channel, String message)方法向通道发布消息;

2.使用jedis的subscribe(JedisPubSub jedisPubSub, String... channels)方法订阅通道;

3.jedisPubSub参数使用自定义类继承JedisPubSub类,重写onMessage(String channel, String message)方法即可处理接收的消息;

4.JedisPubSub类的方法可以按照需求重写:

public abstract class JedisPubSub {
    private int subscribedChannels = 0;
    private volatile Client client;

    public JedisPubSub() {
    }

    public void onMessage(String channel, String message) {
    }

    public void onPMessage(String pattern, String channel, String message) {
    }

    public void onSubscribe(String channel, int subscribedChannels) {
    }

    public void onUnsubscribe(String channel, int subscribedChannels) {
    }

    public void onPUnsubscribe(String pattern, int subscribedChannels) {
    }

    public void onPSubscribe(String pattern, int subscribedChannels) {
    }

    public void onPong(String pattern) {
    }
}

demo:


        JedisPool jedisPool = new JedisPool(new JedisPoolConfig(), "192.168.100.155", 6379
                , 3000, "KFQ2xc3iozsa", 0);


        new Thread(new Runnable() {

            @Override
            public void run() {
                while (true) {
                    Jedis jedis = jedisPool.getResource();
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("发布消息");
                    jedis.publish("ch", "hello");//发布消息
                    jedis.close();
                }
            }
        }).start();

        MyPubSub mypubsub = new MyPubSub();
        new Thread(new Runnable() {
            @Override
            public void run() {
                Jedis jedis = jedisPool.getResource();
                jedis.subscribe(mypubsub, "ch");//订阅消息
                jedis.close();
            }
        }).start();

        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        mypubsub.unsubscribe();// 取消所有订阅
    }


    static class MyPubSub extends JedisPubSub {
        @Override
        public void onSubscribe(String channel, int subscribedChannels) {
            System.out.println("订阅了:" + channel);
            super.onSubscribe(channel, subscribedChannels);
        }

        @Override
        public void onMessage(String channel, String message) {
            System.out.println("收到通道" + channel + "消息:" + message);
            super.onMessage(channel, message);
        }

        @Override
        public void onUnsubscribe(String channel, int subscribedChannels) {
            System.out.println("取消订阅:" + channel);
            super.onUnsubscribe(channel, subscribedChannels);
        }


    }


Redis总结(五)redis发布订阅模式 - 知乎 (zhihu.com)

 Demo2:

首先我们创建两个客户端执行体:

package com.example.redisdemo.service;


import redis.clients.jedis.JedisPubSub;

/**
 * 订阅消息消费体.
 * @author yzlu
 */
public class OneJedisPubSub extends JedisPubSub {

   //接收到消息时执行
    @Override
    public void  onMessage(String channel, String message){
        System.out.println("oneJedisPubSub message is" + message);
    }

    //接收到模式消息时执行
    @Override
    public void onPMessage(String pattern, String channel, String message){
        System.out.println("oneJedisPubSub pattern是"+pattern+"channel是"+channel + "message是" + message);
    }

    //订阅时执行
    @Override
    public void onSubscribe(String channel, int subscribedChannels) {
        System.out.println("oneJedisPubSub订阅成功");
    }

    //取消订阅时执行
    @Override
    public void onUnsubscribe(String channel, int subscribedChannels){
        System.out.println("oneJedisPubSub取消订阅"+channel);
    }


    //取消模式订阅时执行
    @Override
    public void onPUnsubscribe(String pattern, int subscribedChannels) {
        System.out.println("oneJedisPubSub取消多订阅"+pattern);
    }

}
package com.example.redisdemo.service;

import redis.clients.jedis.JedisPubSub;

public class SecondJedisPubSub extends JedisPubSub {

  //接收到消息时执行
    @Override
    public void  onMessage(String channel, String message){
        System.out.println(" SecondJedisPubSub message is" + message);
    }

    //接收到模式消息时执行
    @Override
    public void onPMessage(String pattern, String channel, String message){
        System.out.println("SecondJedisPubSub pattern是"+pattern+"channel是"+channel + "message是" + message);
    }

    //取消订阅时执行
    @Override
    public void onUnsubscribe(String channel, int subscribedChannels){
        System.out.println("SecondJedisPubSub 取消订阅"+channel);
    }

    //订阅时执行
    @Override
    public void onSubscribe(String channel, int subscribedChannels) {
        System.out.println("SecondJedisPubSub订阅成功");
    }

    //取消模式订阅时执行
    @Override
    public void onPUnsubscribe(String pattern, int subscribedChannels) {
        System.out.println("SecondJedisPubSub 取消多订阅"+pattern);
    }

}

然后我们开始给这两个客户端订阅消息

@RestController
@RequestMapping("test")
@Slf4j
public class TestController {
    @Autowired
    private RedisClient redisClient;
    private final OneJedisPubSub oneJedisPubSub = new OneJedisPubSub();

    private final SecondJedisPubSub secondJedisPubSub = new SecondJedisPubSub();

    @PostMapping("subscribe")
    public void  subscribe(@RequestBody QueueTest queueTest){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    if("1".equals(queueTest.getTopic())){
                        redisClient.subscribe(oneJedisPubSub,"topic1","topic2");
                    }
                    if("2".equals(queueTest.getTopic())){
                        redisClient.subscribe(secondJedisPubSub,"topic2");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }


}

请求情况如下图:

请求结果如图:

可以看出我们将两个客户端都订阅了一定channel

此时OneJedisPubSub订阅了topic1和topic2,SecondJedisPubSub订阅了topic2,我们尝试推送消息,demo如下:

@PostMapping("push")
    public void push(@RequestBody QueueTest queueTest){
        log.info("发布一条消息");
        Long publish = redisClient.publish(queueTest.getTopic(), queueTest.getName());
        System.out.println("消费者数量"+publish);
    }

请求如下:

结果如下:

可以看到我们往topic1发布了消息只有OneJedisPubSub接收到了消息,接下来我们往topic2发布消息

请求如下:

结果如下:

可以看到此时两个客户端都接收到了消息

在测试完毕客户端接收消息的能力,我们这时取消SecondJedisPubSub订阅topic2,demo如下:

@PostMapping("unno")
    public void  unno(@RequestBody QueueTest queueTest){
        log.info("取消订阅消息");
        try {
            secondJedisPubSub.unsubscribe(queueTest.getTopic());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

请求如下:

结果如下:

在取消后我们再往topic2推送消息,可以看到只有一个客户端接收消息

如图:

至此我们实验了大部分场景,至于模式订阅由于贴图太麻烦,我就将代码提供出来,大家可以自己实验:

@PostMapping("subscribe")
    public void  subscribe(@RequestBody QueueTest queueTest){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    if("1".equals(queueTest.getTopic())){
                        redisClient.pubsubPattern(oneJedisPubSub,"topic*");
                    }
                    if("2".equals(queueTest.getTopic())){
                        redisClient.subscribe(secondJedisPubSub,"topic2");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
@PostMapping("unno")
    public void  unno(@RequestBody QueueTest queueTest){
        log.info("取消模式订阅消息");
        try {
            secondJedisPubSub.punsubscribe(queueTest.getTopic());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

完整代码:

@Data
public class QueueTest {

    private String topic;

    private String name;
}
@Component("redisClient")
@Slf4j
public class RedisClient {
    @Resource
    private JedisPool jedisPool;

    /**
     * 发布消息
     * @param topic
     * @param message
     */
    public Long publish(String topic,String message){
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            return jedis.publish(topic, message);
        } catch (Exception e) {
            throw e;
        } finally {
            if(jedis != null){
                jedis.close();
            }
        }
    }

    /**
     * 订阅消息
     * @param jedisPubSub
     * @param topics
     */
    public void subscribe(JedisPubSub jedisPubSub, String... topics) throws Exception {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            jedis.subscribe(jedisPubSub,topics);
        } catch (Exception e) {
            throw e;
        } finally {
            if(jedis != null){
                jedis.close();
            }

        }
    }

    /**
     * 模式匹配订阅消息
     * @param topic
     */
    public void pubsubPattern(JedisPubSub jedisPubSub, String topic){
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            jedis.psubscribe(jedisPubSub,topic);
        } catch (Exception e) {
            throw e;
        } finally {
            if(jedis != null){
                jedis.close();
            }
        }
    }
}
@RestController
@RequestMapping("/test")
@Slf4j
public class TestController {
    @Autowired
    private RedisClient redisClient;
    private final OneJedisPubSub oneJedisPubSub = new OneJedisPubSub();

    private final SecondJedisPubSub secondJedisPubSub = new SecondJedisPubSub();

    @PostMapping("/subscribe")
    public void  subscribe(@RequestBody QueueTest queueTest){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    if("1".equals(queueTest.getTopic())){
                        redisClient.subscribe(oneJedisPubSub,"topic1","topic2");
                    }
                    if("2".equals(queueTest.getTopic())){
                        redisClient.subscribe(secondJedisPubSub,"topic2");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }


    @PostMapping("/push")
    public void push(@RequestBody QueueTest queueTest){
        log.info("发布一条消息");
        Long publish = redisClient.publish(queueTest.getTopic(), queueTest.getName());
        System.out.println("消费者数量"+publish);
    }



    @PostMapping("/unno")
    public void  unno(@RequestBody QueueTest queueTest){
        log.info("取消订阅消息");
        try {
            secondJedisPubSub.unsubscribe(queueTest.getTopic());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}

正常Redis中发布与订阅模式应用场景不多,一般需要生产者消费者模式的,又觉得mq重量级的话,可以考虑使用,我还是使用mq比较多

六、Jedis序列化存储对象

        在使用redis存储对象类型时,有好多种方式,着重说以下最常用的Json格式。

        1)fastJson封装类:
public class FastJsonUtils {
    private static final SerializeConfig config;
    static {
        config=new SerializeConfig();
        config.put(java.util.Date.class,new JSONLibDataFormatSerializer());// 使用和json-lib兼容的日期输出格式
        config.put(java.sql.Date.class,new JSONLibDataFormatSerializer());// 使用和json-lib兼容的日期输出格式

    }
    private static final SerializerFeature[] features={SerializerFeature.WriteMapNullValue, // 输出空置字段
            SerializerFeature.WriteNullListAsEmpty, // list字段如果为null,输出为[],而不是null
            SerializerFeature.WriteNullNumberAsZero, // 数值字段如果为null,输出为0,而不是null
            SerializerFeature.WriteNullBooleanAsFalse, // Boolean字段如果为null,输出为false,而不是null
            SerializerFeature.WriteNullStringAsEmpty // 字符类型字段如果为null,输出为"",而不是null

    };
    public  static String convertObjectToJSON(Object object){
        return JSON.toJSONString(object,config,features);
    }
    public static String toJSONNoFeatures(Object object){
        return JSON.toJSONString(object);
    }
    public static String toJSONNoFeaturesNoConfig(Object object){
        return JSON.toJSONString(object);
    }
    public static Object toBean(String text){
        return JSON.parse(text);
    }
    public static <T> T toBean(String text,Class<T> zlass){
        return JSON.parseObject(text,zlass);
    }

    /**
     *  转换为数组
     * @param text
     * @return
     */
    public static <T> Object[] toArray(String text,Class<T> clazz){
        return JSON.parseArray(text,clazz).toArray();
    }
    /**
     * 转换为List
     * @param text
     * @param clazz
     * @return
     */
    public static <T> List<T> toList(String text, Class<T> clazz) {
        return JSON.parseArray(text, clazz);
    }
    /**
     * 将string转化为序列化的json字符串
     * @param text
     * @return
     */
    public static Object textToJson(String text) {
        Object objectJson  = JSON.parse(text);
        return objectJson;
    }
    /**
     * json字符串转化为map
     * @param s
     * @return
     */
    public static <K, V> Map<K, V> stringToCollect(String s) {
        Map<K, V> m = (Map<K, V>) JSONObject.parseObject(s);
        return m;
    }
    /**
     * 转换JSON字符串为对象
     * @param jsonData
     * @param clazz
     * @return
     */
    public static Object convertJsonToObject(String jsonData, Class<?> clazz) {
        return JSONObject.parseObject(jsonData, clazz);
    }
    /**
     * 将map转化为string
     * @param m
     * @return
     */
    public static <K, V> String collectToString(Map<K, V> m) {
        String s = JSONObject.toJSONString(m);
        return s;
    }

}
        2) Jackson封装类:
public class JacksonUtils {
    private final static ObjectMapper obejectmapper=new ObjectMapper();
    private JacksonUtils(){

    }
    private static ObjectMapper getInstance(){
        return obejectmapper;
    }
    /**
     * javaBean、列表数组转换为json字符串
     */
    public static String obj2json(Object obj) throws Exception {
        return obejectmapper.writeValueAsString(obj);
    }
    /**
     * javaBean、列表数组转换为json字符串,忽略空值
     */
    public static String obj2jsonIgnoreNull(Object obj) throws Exception {
        ObjectMapper mapper = new ObjectMapper();
        mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        return mapper.writeValueAsString(obj);
    }
    /**
     * json 转JavaBean
     */
    public static <T> T json2pojo(String jsonString, Class<T> clazz) throws Exception {
        obejectmapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
        return obejectmapper.readValue(jsonString, clazz);
    }
    /**
     * json字符串转换为map
     */
    public static <T> Map<String, Object> json2map(String jsonString) throws Exception {
        ObjectMapper mapper = new ObjectMapper();
        mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        return mapper.readValue(jsonString, Map.class);
    }
    /**
     * json字符串转换为map
     */
    public static <T> Map<String, T> json2map(String jsonString, Class<T> clazz) throws Exception {

        Map<String, Map<String, Object>> map = (Map<String, Map<String, Object>>) obejectmapper.readValue(jsonString, new TypeReference<Map<String, T>>() {
        });
        Map<String, T> result = new HashMap<String, T>();
        for (Map.Entry<String, Map<String, Object>> entry : map.entrySet()) {
            result.put(entry.getKey(), map2pojo(entry.getValue(), clazz));
        }
        return result;
    }
    /**
     * 深度转换json成map
     *
     * @param json
     * @return
     */
    public static Map<String, Object> json2mapDeeply(String json) throws Exception {
        return json2MapRecursion(json, obejectmapper);
    }
    /**
     * 把json解析成list,如果list内部的元素存在jsonString,继续解析
     *
     * @param json
     * @param mapper 解析工具
     * @return
     * @throws Exception
     */
    private static List<Object> json2ListRecursion(String json, ObjectMapper mapper) throws Exception {
        if (json == null) {
            return null;
        }

        List<Object> list = mapper.readValue(json, List.class);

        for (Object obj : list) {
            if (obj != null && obj instanceof String) {
                String str = (String) obj;
                if (str.startsWith("[")) {
                    obj = json2ListRecursion(str, mapper);
                } else if (obj.toString().startsWith("{")) {
                    obj = json2MapRecursion(str, mapper);
                }
            }
        }

        return list;
    }
    /**
     * 把json解析成map,如果map内部的value存在jsonString,继续解析
     *
     * @param json
     * @param mapper
     * @return
     * @throws Exception
     */
    private static Map<String, Object> json2MapRecursion(String json, ObjectMapper mapper) throws Exception {
        if (json == null) {
            return null;
        }

        Map<String, Object> map = mapper.readValue(json, Map.class);

        for (Map.Entry<String, Object> entry : map.entrySet()) {
            Object obj = entry.getValue();
            if (obj != null && obj instanceof String) {
                String str = ((String) obj);

                if (str.startsWith("[")) {
                    List<?> list = json2ListRecursion(str, mapper);
                    map.put(entry.getKey(), list);
                } else if (str.startsWith("{")) {
                    Map<String, Object> mapRecursion = json2MapRecursion(str, mapper);
                    map.put(entry.getKey(), mapRecursion);
                }
            }
        }

        return map;
    }

    /**
     * 与javaBean json数组字符串转换为列表
     */
    public static <T> List<T> json2list(String jsonArrayStr, Class<T> clazz) throws Exception {

        JavaType javaType = getCollectionType(ArrayList.class, clazz);
        List<T> lst = (List<T>) obejectmapper.readValue(jsonArrayStr, javaType);
        return lst;
    }

    /**
     * 获取泛型的Collection Type
     *
     * @param collectionClass 泛型的Collection
     * @param elementClasses  元素类
     * @return JavaType Java类型
     * @since 1.0
     */
    public static JavaType getCollectionType(Class<?> collectionClass, Class<?>... elementClasses) {
        return obejectmapper.getTypeFactory().constructParametricType(collectionClass, elementClasses);
    }


    /**
     * map  转JavaBean
     */
    public static <T> T map2pojo(Map map, Class<T> clazz) {
        return obejectmapper.convertValue(map, clazz);
    }
    /**
     * map 转json
     *
     * @param map
     * @return
     */
    public static String mapToJson(Map map) {
        try {
            return obejectmapper.writeValueAsString(map);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * map  转JavaBean
     */
    public static <T> T obj2pojo(Object obj, Class<T> clazz) {
        return obejectmapper.convertValue(obj, clazz);
    }


}

简单使用案例:

public class TestRedisDemo3 {
     // 存储对象 - 以String形式存储
     @Test
    public void setStringFastJson(){
         //1. 连接Redis
         Jedis jedis = new Jedis("127.0.0.1", 6379);
         //2.1 准备key(String)-value(User)
         String stringKey="fastjsonstringUser";
         User user = new User(2,"好妹妹",new Date());
         //2.2 使用fastJSON将value转化为json字符串
         String stringValue  = FastJsonUtils.toJSONNoFeaturesNoConfig(user);
         //2.3 存储到Redis中
         jedis.set(stringKey,stringValue);
         //3. 释放资源
         jedis.close();


     }
    // 获取对象 - 以String形式获取
    @Test
    public void getStringFastJson(){
        //1. 连接Redism
        Jedis jedis = new Jedis("127.0.0.1", 6379);
        //2.1 准备一个key
        String key="fastjsonstringUser";
        //2.2 去Redis中查询value
        String value = jedis.get(key);
        //2.3 将value反序列化为User
        User user =(User) FastJsonUtils.toBean(value,User.class);
        System.out.println("user"+user);
        //3. 释放资源
        jedis.close();

    }

}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1473958.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

3D生成式AI模型与工具

当谈到技术炒作时&#xff0c;人工智能正在超越虚拟世界&#xff0c;吸引世界各地企业和消费者的注意力。 但人工智能可以进一步增强虚拟世界&#xff0c;至少在某种意义上&#xff1a;资产创造。 AI 有潜力扩大用于虚拟环境的 3D 资产的创建。 AI 3D生成使用人工智能生成3D模…

【pytorch】常用便捷代码

文章目录 条件与概率torch.tensor()torch.rand()torch.randn()torch.randint()torch.multinominal() 逻辑运算torch.argmax()torch.max()torch.sum()torch.tanh()torch.pow() 功能性操作 torch.nn.functionalF.normalize()F.elu()F.relu()F.softmax() 张量计算torch.zeros()tor…

CentOS7 Hive2.3.8安装

CentOS7 Hive2.3.8 安装 建议从头用我的博客&#xff0c;如果用外教的文件到 一、9)步骤了&#xff0c;就用他的弄完&#xff0c;数据库不一样&#xff0c;在9步骤前还能继续看我的 一、 安装MySQL 0.0&#xff09;查询mariadb,有就去0.1&#xff09;&#xff0c;没有就不管…

kuka协作机器人LBR系列 issy15R930导入到ros2_rviz(带外观文件)

kuka协作机器人LBR系列 issy15R930导入到ros2_rviz(带外观文件&#xff09;外观文件未调整好&#xff0c;外观仍需进一步研究&#xff0c;外观文件dae与轮廓&#xff08;碰撞&#xff09;文件STL并未完全对应起来。在blender里面看了一下UR机器人的文件&#xff0c;是对应的&am…

Springboot教程(二)——过滤器、拦截器

过滤器 过滤器可以在调用控制器方法之前进行一些操作&#xff0c;过滤器类一般放在filter包下。 配置类注册 使用过滤器时&#xff0c;要实现Filter接口&#xff0c;并重写doFilter方法&#xff1a; class TestFilter : Filter {override fun doFilter(request: ServletReq…

苹果发布iPhone 16:革命性创新重新定义智能手机体验

&#xff08;苹果总部&#xff0c;加利福尼亚州&#xff0c;2024年2月23日&#xff09;——今天&#xff0c;全球领先的科技公司苹果公司再次震撼世界&#xff0c;宣布推出iPhone 16&#xff0c;这款革命性的智能手机重新定义了人们对于手机的期望和体验。 iPhone 16的发布代表…

数字孪生的技术开发平台

数字孪生的开发平台可以基于各种软件和硬件工具来实现&#xff0c;这些平台提供了丰富的功能和工具&#xff0c;帮助开发人员构建、部署和管理数字孪生系统&#xff0c;根据具体的需求和技术要求&#xff0c;开发人员可以选择合适的平台进行开发工作。以下列举了一些常见的数字…

再探二分法

推荐阅读 智能化校园&#xff1a;深入探讨云端管理系统设计与实现&#xff08;一&#xff09; 智能化校园&#xff1a;深入探讨云端管理系统设计与实现&#xff08;二&#xff09; 文章目录 推荐阅读二分查找题目思路解法左闭右闭式写法左闭右开式写法 二分查找 题目 给定一个…

十六:网络编程

提示&#xff1a;文章写完后&#xff0c;目录可以自动生成&#xff0c;如何生成可参考右边的帮助文档 文章目录 01、网络编程概述02、网络通信要素概述03、通信要素1&#xff1a;IP和端口号3.1、IP的理解与InetAddress类的实例化3.2、端口号的理解 04、通信要素2&#xff1a;网…

主从复制实现Redis集群

主从复制实现Redis集群实验 (一主二从): 实验环境: 使用Docker 搭建 Redis 版本 5.0.5 打开一个终端窗口&#xff0c;在其中运行如下命令创建一个名为redis-master的Redis容器。注意&#xff0c;它的端口是6379 (本地的端口:映射到容器的端口) docker run -itd--name redis-m…

【wails】(6):使用wails做桌面应用开发,使用gin+go-chatglm.cpp进行本地模型运行,在windows上运行成功

1&#xff0c;整体架构说明 主要使用&#xff0c;参考的开源项目是&#xff1a; https://github.com/wailsapp/wails 前端项目&#xff1a; https://github.com/Chanzhaoyu/chatgpt-web 运行模型&#xff1a; https://github.com/Weaxs/go-chatglm.cpp 参考代码&#xff1a; h…

【VSCode】SSH Remote 通过跳板机连开发机提示“bash行1 powershell未找到命令”

需求背景 因为需要&#xff0c;在家我需要挂上公司VPN然后SSH连到跳板机&#xff0c;然后再从跳板机SSH进开发机。 问题背景 跳板机进开发机输入完密码显示 bash行1 powershell未找到命令VSCode SSH Remote跳板机配置请自行搜素其他文章config配置 注意其中ssh.exe地址请根据…

消息中间件篇之RabbitMQ-延时队列

一、延时队列 延迟队列&#xff1a;进入队列的消息会被延迟消费的队列。 场景&#xff1a;超时订单、限时优惠、定时发布。 延迟队列死信交换机TTL&#xff08;生存时间&#xff09;。 二、死信交换机 当一个队列中的消息满足下列情况之一时&#xff0c;可以成为死信&#xf…

React Hooks概述及常用的React Hooks介绍

Hook可以让你在不编写class的情况下使用state以及其他React特性 useState ● useState就是一个Hook ● 通过在函数组件里调用它来给组件添加一些内部state,React会在重复渲染时保留这个state 纯函数组件没有状态&#xff0c;useState()用于设置和使用组件的状态属性。语法如下…

Yolov8有效涨点:YOLOv8-AM,添加多种注意力模块提高检测精度,含代码,超详细

前言 2023 年&#xff0c;Ultralytics 推出了最新版本的 YOLO 模型。注意力机制是提高模型性能最热门的方法之一。 本次介绍的是YOLOv8-AM&#xff0c;它将注意力机制融入到原始的YOLOv8架构中。具体来说&#xff0c;我们分别采用四个注意力模块&#xff1a;卷积块注意力模块…

【嵌入式——QT】SpinBox

QSpinBox用于整数的显示和输入&#xff0c;一般显示十进制数&#xff0c;也可以显示二进制、十六进制数&#xff0c;而且可以在显示框中增加前缀和后缀。QDoubleSpinBox用于浮点数的显示和输入&#xff0c;可以设置显示小数位数&#xff0c;也可以设置显示的前缀和后缀。 样式…

安装淘宝镜像cnpm报错

npm 安装淘宝镜像报错 npm install -g cnpm --registryhttps://registry.npm.taobao.org 安装报 The operation was rejected by your operating system. npm ERR! Its possible that the file was already in use (by a text editor or antivirus), npm ERR! or that you la…

CSS 入门手册(二)

目录 12-Overflow 13-下拉菜单 14-提示框 14.1 显示位置&#xff08;左右&#xff09; 14.2 显示位置(上下) 14.3 添加箭头 14.4 淡入效果 15-图片 16-列表 17-表格 17.1 表格宽度和高度 17.2 文字对齐 17.3 表格颜色 18-计数器 19-导航栏 19.1 导航栏UI优化 …

顺序表的列题(力扣)和旋转数组

文章目录 一.删除有序数组中的重复项&#xff08;取自力扣&#xff09; 二.合并两个有序数组&#xff08;取自力扣&#xff09; 三.旋转数组&#xff08;多解法&#xff09; 前言 见面我们说到了顺序表今天来分享几个有关于顺序表的题目 一.删除有序数组中的重复项&#xff…

【JavaSE】实用类——枚举类型、包装类、数学类

目录 Java API枚举优势代码示例 包装类作用包装类和基本数据类型的对应关系包装类的构造方法包装类的常用方法装箱和拆箱 留一个问题大家猜猜看包装类的特点 Math类Random类代码示例 Java API Java API(Java Application Programming Interface) 即Java应用程序编程接口&#…