• JRedis的基本操作,基本数据类型操作


    Redis的基本数据类型:

    • string
    • hash
    • list
    • set
    • zset
    {
        public static void main(String[] args) {
            Jedis jedis = new Jedis("127.0.0.1", 6379);
            // string
            jedis.set("hello", "word");
            String hello = jedis.get("hello");
            System.out.println(hello);
            // hash
            jedis.hset("hello:world", "version", "111");
            jedis.hset("hello:world", "name", "jedis-redis");
            Map<String, String> stringStringMap = jedis.hgetAll("hello:world");
            System.out.println(stringStringMap);
            // list
            jedis.rpush("namelist", "11", "22");
            List<String> namelist = jedis.lrange("namelist", 0, 1);
            System.out.println(namelist);
            // set
            jedis.sadd("subject", "math", "Math", "abc", "abc");
            Set<String> subject = jedis.smembers("subject");
            System.out.println(subject);
            // zset
            jedis.zadd("nums", 1, "ming");
            jedis.zadd("nums", 4, "zhang");
            jedis.zadd("nums", 2, "wang");
            Set<String> nums = jedis.zrange("nums", 0, 4);
            System.out.println(nums);
    
            jedis.close();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    在这里插入图片描述

    Jedis线程池比Jedis稳定,且易于管理。

    {
        public static void main(String[] args) {
            JedisPoolConfig poolConfig = new JedisPoolConfig();
            JedisPool jedisPool = new JedisPool(poolConfig, "127.0.0.1", 6379, 20000);
            Jedis jedis = null;
            try{
                jedis = jedisPool.getResource();
                jedis.set("pool", "jeditpool");
                String result = jedis.get("pool");
                System.out.println(result);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                assert jedis != null;
                jedis.close();
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    在这里插入图片描述

    JedisPool的Jedis对象最多默认为8个

    package com.codeiteasy.service;
    
    import org.springframework.stereotype.Service;
    import redis.clients.jedis.Jedis;
    import redis.clients.jedis.JedisPool;
    import redis.clients.jedis.JedisPoolConfig;
    
    import java.time.Duration;
    import java.util.concurrent.atomic.AtomicInteger;
    
    @Service
    public class JedisPoolService {
        public static void main(String[] args) throws InterruptedException {
            JedisPoolConfig poolConfig = new JedisPoolConfig();
            poolConfig.setMaxWait(Duration.ofSeconds(2));
            JedisPool jedisPool = new JedisPool(poolConfig, "127.0.0.1", 6379, 20000);
    
    
            AtomicInteger atom = new AtomicInteger(0);
            for (int i = 0; i < 15; i++) {
                Thread t1 = new Thread(() -> {
                    try{
                        Jedis jedis = null;
                        jedis = jedisPool.getResource();
                        jedis.set("pool", "jeditpool");
                        String result = jedis.get("pool");
                        int x = atom.incrementAndGet();
                        System.out.println(result+x);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                });
                t1.start();
                t1.join();
            }
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38

    在这里插入图片描述
    更改用后关闭

    package com.codeiteasy.service;
    
    import org.springframework.stereotype.Service;
    import redis.clients.jedis.Jedis;
    import redis.clients.jedis.JedisPool;
    import redis.clients.jedis.JedisPoolConfig;
    
    import java.time.Duration;
    import java.util.concurrent.atomic.AtomicInteger;
    
    @Service
    public class JedisPoolService {
        public static void main(String[] args) throws InterruptedException {
            JedisPoolConfig poolConfig = new JedisPoolConfig();
            poolConfig.setMaxWait(Duration.ofSeconds(2));
            JedisPool jedisPool = new JedisPool(poolConfig, "127.0.0.1", 6379, 20000);
    
    
            AtomicInteger atom = new AtomicInteger(0);
            for (int i = 0; i < 15; i++) {
                Thread t1 = new Thread(() -> {
                    Jedis jedis = null;
                    try{
    
                        jedis = jedisPool.getResource();
                        jedis.set("pool", "jeditpool");
                        String result = jedis.get("pool");
                        int x = atom.incrementAndGet();
                        System.out.println(result+x);
                    } catch (Exception e) {
                        e.printStackTrace();
                    } finally {
                        jedis.close();
                    }
                });
                t1.start();
                t1.join();
            }
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41

    在这里插入图片描述

  • 相关阅读:
    不小心删除文件夹怎么恢复,怎么恢复误删文件?
    Oracle12c新特性大全 存储资源隔离+flex+diskgroup
    QAnything安装纪要
    Spring Boot中的依赖注入和自动注入
    SSM整合ActiveMQ
    Linux安装Hadoop超详细教程
    【北京迅为】《i.MX8MM嵌入式Linux开发指南》-第一篇 嵌入式Linux入门篇-第二十八章 借助U盘或TF卡拷贝程序到开发板上
    怎样去优化JAVA中的实现代码
    VR机器人教你如何正确打乒乓球
    什么是无人机全自动飞行系统?概念、构成、作用深度解析
  • 原文地址:https://blog.csdn.net/weixin_43792401/article/details/132884345