1. Jedis 简介

Jedis:一款 Java 操作 redis 数据库的工具.

2. Jedis步骤

2.1使用步骤:

  1. 1. 下载`jedis``jar`包<br /> 2. 使用
  1. //1. 获取连接
  2. Jedis jedis = new Jedis("localhost",6379);
  3. //2. 操作
  4. jedis.set("username","zhangsan");
  5. //3. 关闭连接
  6. jedis.close();

2.2 Jedis 操作各种 redis 中的数据结构

1) 字符串类型 string [ set、get ]

  1. //1. 获取连接
  2. Jedis jedis = new Jedis();//如果使用空参构造,默认值 "localhost",6379端口
  3. //2. 操作
  4. //存储
  5. jedis.set("username","zhangsan");
  6. //获取
  7. String username = jedis.get("username");
  8. System.out.println(username);
  9. //可以使用setex()方法存储可以指定过期时间的 key value
  10. jedis.setex("activecode",20,"hehe");//将activecode:hehe键值对存入redis,并且20秒后自动删除键值对
  11. //3. 关闭连接
  12. jedis.close();
  1. 2) 哈希类型 `hash ` **map**格式 **[** **hset

、hget 、hgetAll ]**

  1. //1. 获取连接
  2. Jedis jedis = new Jedis();//如果使用空参构造,默认值 "localhost",6379端口
  3. //2. 操作
  4. // 存储hash
  5. jedis.hset("user","name","lisi");
  6. jedis.hset("user","age","23");
  7. jedis.hset("user","gender","female");
  8. // 获取hash
  9. String name = jedis.hget("user", "name");
  10. System.out.println(name);
  11. // 获取hash的所有map中的数据
  12. Map<String, String> user = jedis.hgetAll("user");
  13. // keyset
  14. Set<String> keySet = user.keySet();
  15. for (String key : keySet) {
  16. //获取value
  17. String value = user.get(key);
  18. System.out.println(key + ":" + value);
  19. }
  20. //3. 关闭连接
  21. jedis.close();
  1. 3) 列表类型 `list ` **linkedlist**格式。<br />


支持重复元素

[ lpush / rpush 、lpop / rpop、lrange start end : 范围获取 ]

  1. //1. 获取连接
  2. Jedis jedis = new Jedis();//如果使用空参构造,默认值 "localhost",6379端口
  3. //2. 操作
  4. // list 存储
  5. jedis.lpush("mylist","a","b","c");//从左边存
  6. jedis.rpush("mylist","a","b","c");//从右边存
  7. // list 范围获取
  8. List<String> mylist = jedis.lrange("mylist", 0, -1);
  9. System.out.println(mylist);
  10. // list 弹出
  11. String element1 = jedis.lpop("mylist");//c
  12. System.out.println(element1);
  13. String element2 = jedis.rpop("mylist");//c
  14. System.out.println(element2);
  15. // list 范围获取
  16. List<String> mylist2 = jedis.lrange("mylist", 0, -1);
  17. System.out.println(mylist2);
  18. //3. 关闭连接
  19. jedis.close();
  1. 4) 集合类型 `set` <br />不允许重复元素

[ sadd 、smembers : 获取所有元素 ]

  1. //1. 获取连接
  2. Jedis jedis = new Jedis();//如果使用空参构造,默认值 "localhost",6379端口
  3. //2. 操作
  4. // set 存储
  5. jedis.sadd("myset","java","php","c++");
  6. // set 获取
  7. Set<String> myset = jedis.smembers("myset");
  8. System.out.println(myset);
  9. //3. 关闭连接
  10. jedis.close();
  1. 5) 有序集合类型 `sortedset`:<br />不允许重复元素,且元素有顺序

[ zadd、zrange ]

  1. //1. 获取连接
  2. Jedis jedis = new Jedis();//如果使用空参构造,默认值 "localhost",6379端口
  3. //2. 操作
  4. // sortedset 存储
  5. jedis.zadd("mysortedset",3,"亚瑟");
  6. jedis.zadd("mysortedset",30,"后裔");
  7. jedis.zadd("mysortedset",55,"孙悟空");
  8. // sortedset 获取
  9. Set<String> mysortedset = jedis.zrange("mysortedset", 0, -1);
  10. System.out.println(mysortedset);
  11. //3. 关闭连接
  12. jedis.close();

3. jedis连接池: JedisPool

JedisPool使用:

  1. 创建 JedisPool 连接池对象

    1. <br />2. 调用方法 `getResource()` 方法获取 `Jedis` 连接
    1. //0.创建一个配置对象
    2. JedisPoolConfig config = new JedisPoolConfig();
    3. config.setMaxTotal(50);
    4. config.setMaxIdle(10);
    5. //1.创建Jedis连接池对象
    6. JedisPool jedisPool = new JedisPool(config,"localhost",6379);
    7. //2.获取连接
    8. Jedis jedis = jedisPool.getResource();
    9. //3. 使用
    10. jedis.set("hehe","heihei");
    11. //4. 关闭 归还到连接池中
    12. jedis.close();

连接池工具类

自己建立一个 JedisPoolUtils.Java 文件

  1. public class JedisPoolUtils {
  2. private static JedisPool jedisPool;
  3. static{
  4. //读取配置文件
  5. InputStream is =
  6. JedisPoolUtils.class.getClassLoader().getResourceAsStream("jedis.properties");
  7. //创建Properties对象
  8. Properties pro = new Properties();
  9. //关联文件
  10. try {
  11. pro.load(is);
  12. } catch (IOException e) {
  13. e.printStackTrace();
  14. }
  15. //获取数据,设置到JedisPoolConfig中
  16. JedisPoolConfig config = new JedisPoolConfig();
  17. config.setMaxTotal(Integer.parseInt(pro.getProperty("maxTotal")));
  18. config.setMaxIdle(Integer.parseInt(pro.getProperty("maxIdle")));
  19. //初始化JedisPool
  20. jedisPool = new JedisPool(
  21. config,pro.getProperty("host"),Integer.parseInt(pro.getProperty("port")));
  22. }
  23. /**
  24. * 获取连接方法
  25. */
  26. public static Jedis getJedis(){
  27. return jedisPool.getResource();
  28. }
  29. }