• (二)Redis之Jedis概念和HelloWorld实现以及JedisPool的使用


    一、Jedis概念

    实际开发中,我们需要用Redis的连接工具连接Redis然后操作Redis,

    对于主流语言,Redis都提供了对应的客户端;

    官网:https://redis.io/clients

    二、HelloWorld程序

      2.1  引入maven的Jedis依赖

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

      2.2  测试

    package myRedis01;
    
    import redis.clients.jedis.Jedis;
    
    public class JedisTest {
         
        public static void main(String[] args) {
            Jedis jedis=new Jedis("127.0.0.1",6379); // 创建客户端 设置IP和端口
            jedis.set("name", "helloWorld"); // 设置值
            String value=jedis.get("name"); // 获取值
            System.out.println(value);
            jedis.close(); // 释放连接资源
        }
    }

     三、JedisPool的使用

    package myRedis01;
    
    import redis.clients.jedis.Jedis;
    import redis.clients.jedis.JedisPool;
    import redis.clients.jedis.JedisPoolConfig;
    
    public class JedisPoolTest {
    
        public static void main(String[] args) {
            JedisPoolConfig config = new JedisPoolConfig();
            config.setMaxTotal(100); // 设置最大连接数
            config.setMaxIdle(10);// 设置最大空闲连接数
    
            Jedis jedis = null;
            JedisPool jedisPool = new JedisPool(config, "127.0.0.1", 6379);
            try {
    
                jedis = jedisPool.getResource();
                jedis.set("name", "helloWorld");
                String value=jedis.get("name");
                System.out.println(value);
    
            } catch (Exception e) {
                e.printStackTrace();
    
            } finally {
                if (jedis != null) {
                    jedis.close();
                }
                if (jedisPool != null) {
                    jedisPool.close();
                }
    
            }
    
        }
    }

  • 相关阅读:
    代码搭建记事本框架(一)
    代码搭建记事本框架(二)
    ios中图片拉伸用法
    ios启动载入启动图片
    Top-k test
    leetcode : jump game
    leetcode : Top k frequent elements
    一个月没有更新了
    leetcode : Reverse Linked List II [two pointers]
    leetcode : reverse linked list [基本功,闭着眼也要写出来]
  • 原文地址:https://www.cnblogs.com/shyroke/p/8006922.html
Copyright © 2020-2023  润新知