Detailed explanation of Jedis configuration

Jedis

Jedis is a Redis client library for the Java language, which provides interfaces and functions for interacting with the Redis database. By using Jedis, you can easily communicate with Redis in your Java applications.

Jedis provides a concise API, with the redis command as the method name, which is easy to learn and use.

Here are some common uses of Jedis:

  1. Connect to Redis:

    Jedis jedis = new Jedis("localhost", 6379);
    
  2. Execute Redis command:

    jedis.set("key", "value");
    String value = jedis.get("key");
    
  3. Key value operations:

    jedis.set("name", "John");
    String name = jedis.get("name");
    
    jedis.del("key"); // 删除键
    
    Boolean exists = jedis.exists("key"); // 检查键是否存在
    
  4. List operations:

    jedis.lpush("list", "item1", "item2"); // 在列表左侧插入元素
    List<String> list = jedis.lrange("list", 0, -1); // 获取列表所有元素
    
    Long len = jedis.llen("list"); // 获取列表长度
    String item = jedis.lpop("list"); // 弹出列表左侧元素
    
  5. Hash operation:

    jedis.hset("hash", "field1", "value1");
    String value = jedis.hget("hash", "field1");
    
    Map<String, String> map = jedis.hgetAll("hash"); // 获取哈希表所有字段和值
    
  6. Collection operations:

    jedis.sadd("set", "member1", "member2"); // 向集合添加成员
    Set<String> set = jedis.smembers("set"); // 获取集合所有成员
    
    Boolean isMember = jedis.sismember("set", "member1"); // 检查成员是否存在于集合中
    
  7. Ordered set operations:

    jedis.zadd("sortedset", 1.0, "member1");
    Double score = jedis.zscore("sortedset", "member1");
    
    Set<Tuple> set = jedis.zrangeWithScores("sortedset", 0, -1); // 获取有序集合的成员和分数
    

Jedis connection pool

Non-thread-safe: Jedis instances are not thread-safe, and multiple threads sharing an instance may cause concurrent access conflicts. In order to ensure thread safety, a connection pool needs to be used to manage multiple instances.

Configuring the Jedis connection pool involves setting some parameters. The following are the basic configuration steps:

  1. Import Jedis and connection pool-related dependency packages:

    <dependency>
      <groupId>redis.clients</groupId>
      <artifactId>jedis</artifactId>
      <version>版本号</version>
    </dependency>
    
  2. Create a JedisPool object and configure it in code:

    import redis.clients.jedis.JedisPool;
    import redis.clients.jedis.JedisPoolConfig;
    
    // 创建连接池配置对象
    JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
    
    // 设置最大连接数
    jedisPoolConfig.setMaxTotal(100);
    
    // 设置最大空闲连接数
    jedisPoolConfig.setMaxIdle(50);
    
    // 设置最小空闲连接数
    jedisPoolConfig.setMinIdle(10);
    
    // 设置连接超时时间(毫秒)
    jedisPoolConfig.setConnectTimeout(5000);
    
    // 创建 JedisPool 对象并进行配置
    JedisPool jedisPool = new JedisPool(jedisPoolConfig, "localhost", 6379);
    

    When configuring parameters, you can adjust them according to actual needs. Here we take the maximum number of connections, the maximum number of idle connections, the minimum number of idle connections and the connection timeout as examples.

  3. Use the connection pool to obtain a Jedis instance for operation:

    import redis.clients.jedis.Jedis;
    
    try (Jedis jedis = jedisPool.getResource()) {
          
          
        // 使用 jedis 进行 Redis 操作
        jedis.set("key", "value");
        String result = jedis.get("key");
        System.out.println(result);
    }
    

    When using the Jedis connection pool, jedisPool.getResource()obtain the Jedis instance from the connection pool through the method, and use the try-with-resources structure to automatically close the connection after use.

Guess you like

Origin blog.csdn.net/drhnb/article/details/132186713