7.jedis工具

jedis可以通过java程序连接到redis

1.pom.xml安装依赖

	<jedis.version>4.2.3</jedis.version>

	<dependency>
          <groupId>redis.clients</groupId>
          <artifactId>jedis</artifactId>
          <version>${jedis.version}</version>
    </dependency>

2.测试

public class JedisTest {
    private static final String HOST ="192.168.199.110";
    private static final int PORT=6379;
    public static void main(String[] args) {
        //set();
        get();
    }
    public static void set(){
        Jedis jedis = new Jedis(HOST, PORT);
        String country = jedis.set("country","中国");
        jedis.close();
    }
    public static void get(){
        Jedis jedis = new Jedis(HOST, PORT);
        String country = jedis.get("country");
        System.out.println("country = " + country);
        jedis.close();
    }
}

3.编写工具类

//utils/JedisUtils.java

public class JedisUtils {
        private static JedisPool jedisPool;

        static {
            InputStream inputStream = JedisUtils.class.getClassLoader().getResourceAsStream("jedis.properties");
            Properties properties = new Properties();
            try {
                properties.load(inputStream);
                int maxTotal = Integer.valueOf(properties.getProperty("jedis.maxTotal"));
                int maxIdle = Integer.valueOf(properties.getProperty("jedis.maxIdle"));
                long maxWaitMillis = Integer.valueOf(properties.getProperty("jedis.maxWaitMillis"));
                String host = properties.getProperty("jedis.host");
                int port = Integer.valueOf(properties.getProperty("jedis.port"));

                //创建Jedis链接池对象
                JedisPoolConfig poolConfig = new JedisPoolConfig();
                poolConfig.setMaxTotal(maxTotal);
                poolConfig.setMaxIdle(maxIdle);
                poolConfig.setMaxWaitMillis(maxWaitMillis);
                jedisPool = new JedisPool(
                        poolConfig,
                        host,
                        port
                );
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

        public static Jedis getJedis() {
            return jedisPool.getResource();
        }

        public static void close(Jedis jedis) {
            if ( null != jedis) {
                jedis.close();
                jedis = null;
            }
        }
}

4.配置jedis.properties

# redis主机地址
jedis.host=192.168.199.110

# redis端口 
jedis.port=6379

# 最大连接数 
jedis.maxTotal=1000

# 控制一个pool最多有多少个状态为空闲的jedis实例 
jedis.maxIdle=100

# 最大的等待毫秒数,如果超过等待时间,则直接抛JedisConnectionException
jedis.MaxWaitMillis=10000

5.工具类的使用

public class JedisTest {
    public static void main(String[] args) {
        //set();
        get();
    }
    public static void set(){
        Jedis jedis = JedisUtils.getJedis();
        String country = jedis.set("country","中国");
        JedisUtils.close(jedis);
    }
    public static void get(){
        Jedis jedis = JedisUtils.getJedis();
        String country = jedis.get("country");
        System.out.println("country = " + country);
        JedisUtils.close(jedis);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小钱要努力

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值