Jedis简述

1、所需要的依赖

 <dependencies>
    <!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
    <dependency>
        <groupId>redis.clients</groupId>
        <artifactId>jedis</artifactId>
        <version>2.9.0</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-pool2 -->
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-pool2</artifactId>
        <version>2.3</version>
    </dependency>
</dependencies>

2、入门程序

public class test1 {

    @Test
    public void test1(){
        Jedis jedis = new Jedis("localhost",6379);
        jedis.set("username","zhangsan");
        jedis.close();
    }
}

执行完查看
在这里插入图片描述

操作字符串
@Test
public void test(){
     Jedis jedis = new Jedis("localhost",6379);
     jedis.set("username","zhangsan");
     System.out.println(jedis.get("username"));
     jedis.del("username");
     System.out.println(jedis.get("username"));
     jedis.close();
}

在这里插入图片描述

  • setex方法,可以指定过期时间的方法。
    将activecode: hehe键值对存入redis, 并且20秒后自动删除该键值对
jedis.setex("activecode",20,"haha");
操作hash
@Test
public void test(){
    Jedis jedis = new Jedis("localhost",6379);
    jedis.hset("user","name","dai");
    jedis.hset("user","age","20");
    String name = jedis.hget("user","name");
    System.out.println(name);

    Map<String,String> map = jedis.hgetAll("user");
    Set<String> keySet = map.keySet();
    for(String key:keySet){
        String value = map.get(key);
        System.out.println(value);
    }
    jedis.close();
}
操作list
@Test
public void test(){
    Jedis jedis = new Jedis("localhost",6379);
    jedis.lpush("list","a","b","c");
    jedis.rpush("list","q","w","e");
    List<String> list = jedis.lrange("list",0,-1);
    System.out.println(list);
    String a = jedis.lpop("list");
    System.out.println(a);
    System.out.println(jedis.lrange("list",0,-1));
    jedis.close();
}

在这里插入图片描述

操作set
@Test
public void test(){
     Jedis jedis = new Jedis("localhost",6379);
     jedis.sadd("mySet","c++","java","php");
     Set<String> myset = jedis.smembers("mySet");
     System.out.println(myset);
     jedis.close();
 }

在这里插入图片描述

操作sortedset
@Test
public void test(){
    Jedis jedis = new Jedis("localhost",6379);
    jedis.zadd("setsorted",300,"haha");
    jedis.zadd("setsorted",30,"后裔");
    jedis.zadd("setsorted",25,"eee");
    System.out.println(jedis.zrange("setsorted",0,-1));
    jedis.close();
}

在这里插入图片描述

3、连接池jedisPool

@Test
public void test(){
    //设置配置类
    JedisPoolConfig config = new JedisPoolConfig();
    config.setMaxTotal(50);
    config.setMaxIdle(10);
    //创建jedis连接池对象
    JedisPool jedisPool = new JedisPool(config,"localhost",6379);
    //获取连接
    Jedis jedis = jedisPool.getResource();
    //使用
    jedis.set("name","aaa");
    //关闭
    jedis.close();
}

在这里插入图片描述

配置类信息
  • 最天活动对象数
    redis. pool . maxTotal= =1000
  • 最大能够保持ide1状态的对象数
    redis. pool . maxIdle= =100
  • 设小能够保持idel状态的对象数
    redis. pool. minIdle=50
  • 当池内没有返回对象时,最大等待时间
    redis. pool . maxWaitMillis=10000
  • 当调用borrowObject方法时,是否进行有效性检查
    redi s . pool. tes tOnBor row=true
  • 当调用return object方法时,是否进行有效性检查
    redi s. pool. testOnReturn: =true
  • “空闲链接"检测线程,检测的周期,毫秒数。如果为负值,表示不运行“检测线程”。默认为-1.
    redis. pool. timeBetweenEvictionRunsMillis= =30000
  • 向调用者输出“链接"对象时,是否检测它的空闲超时:
    redis. pool. testWhileIdle=true
  • 对于“空闲链接"检测线程而言,每次检测的链接资源的个数。默认为3.
    redis. pool . numTestsPerEvictionRun=50
  • redis服务器的IP
    redis. ip=XXXXXX
  • redis服务器的Port
    redis1.port=6379

4、连接池工具类

package com.jedis.test;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * @ClassName JedisUtil
 * @Description
 * @Author 
 * @Date 2019/10/3 19:33
 * @Version 1.0
 **/
public class JedisUtil {

    private static JedisPool jedisPool;

    static{
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxIdle(50);
        config.setMaxIdle(10);

        jedisPool = new JedisPool(config,"localhost",6379);
    }

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

发布了134 篇原创文章 · 获赞 91 · 访问量 16万+

猜你喜欢

转载自blog.csdn.net/weixin_44588495/article/details/101991545