Jedis的使用

有两种集成方式

  • 将服务器引入到项目中,让它在应用程序的上下文中运行,通常被称为嵌入式(embedded)服务器。这意味着它们与您的应用程序共享同一个 Java 虚拟机(JVM)进程。这种方式更适合小型项目或希望简化部署和维护的情况。
  • 如果选择远程连接,服务与应用程序分别在不同的进程中运行,这样可以更好地实现解耦和横向扩展,但可能需要更多的配置和管理。
    redis和kafka都没有嵌入式的服务器版本,都只能通过连接redis进程,kafka进程的服务器。

redis键的命名规则
命名空间
项目名:表名:字段名:key
key=oauth:user:userName:haidong value=0227

Jedis: Jedis 类是主要的操作类,用于与 Redis 建立连接并执行各种操作,如设置值、获取值、执行命令等。

单例模式

public class JedisFactory {

    private static JedisPool pool;
    private static final JedisFactory instance=new JedisFactory();

    static {
        Properties properties=new Properties();
        String host;
        int port ;
        int maxTotal ;
        int maxWaitMillis;
        Duration maxWaitMillisDuration;
        int maxIdle ;
        int minIdle;
        InputStream inputStream= JedisFactory.class.getClassLoader().getResourceAsStream("jedis-config.properties");
        try {
            properties.load(inputStream);
            host = properties.getProperty("redis.host", "localhost");
            port = Integer.parseInt(properties.getProperty("redis.port", "6379"));
            maxTotal = Integer.parseInt(properties.getProperty("redis.maxTotal", "10"));
            maxIdle = Integer.parseInt(properties.getProperty("redis.maxIdle", "5"));
            minIdle=Integer.parseInt(properties.getProperty("redis.minIdle","1"));
            maxWaitMillis = Integer.parseInt(properties.getProperty("redis.maxWaitMillis", "10000"));
            maxWaitMillisDuration=Duration.ofMillis(maxWaitMillis);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
       JedisPoolConfig poolConfig=new JedisPoolConfig();
        poolConfig.setMinIdle(minIdle);
        poolConfig.setMaxIdle(maxIdle);
        poolConfig.setMaxTotal(maxTotal);
        poolConfig.setMaxWait(maxWaitMillisDuration);
        pool=new JedisPool(poolConfig,host,port);
    }
    private JedisFactory(){}
    public static JedisFactory getInstance(){
        return instance;
    }

    public Jedis getJedis(){
        Jedis jedis=pool.getResource();
        return jedis;
    }
}

相关推荐

  1. Jedis使用

    2024-02-18 09:54:02       54 阅读
  2. <span style='color:red;'>Jedis</span>

    Jedis

    2024-02-18 09:54:02      55 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-02-18 09:54:02       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-02-18 09:54:02       106 阅读
  3. 在Django里面运行非项目文件

    2024-02-18 09:54:02       87 阅读
  4. Python语言-面向对象

    2024-02-18 09:54:02       96 阅读

热门阅读

  1. 13.5. 多尺度目标检测

    2024-02-18 09:54:02       40 阅读
  2. 算法刷题day14

    2024-02-18 09:54:02       55 阅读
  3. 卷积神经网络吴恩达coursera

    2024-02-18 09:54:02       45 阅读
  4. Hot100-hash表-两数之和

    2024-02-18 09:54:02       53 阅读
  5. 困于环中的机器人

    2024-02-18 09:54:02       57 阅读
  6. 带你了解软件系统架构的演变

    2024-02-18 09:54:02       48 阅读
  7. 软件系统支持联营模式:实现共赢共利的关键

    2024-02-18 09:54:02       49 阅读
  8. 浅谈Websocket

    2024-02-18 09:54:02       54 阅读