Java语言连接Redis服务的工具:

  • Jedis
  • SpringData Redis
  • Lettuce

Jedis操作Redis

  1. 引入对应的依赖
    1. <dependency>
    2. <groupId>redis.clients</groupId>
    3. <artifactId>jedis</artifactId>
    4. <version>3.3.0</version>
    5. </dependency>
  1. 编写程序

    1. @Test
    2. public void testJedis() {
    3. // 1.连接reids
    4. Jedis jedis = new Jedis("127.0.0.1", 6379);
    5. // 2.操作redis
    6. // jedis的方法名和reids对应的命令名完全一样
    7. jedis.set("name", "zhangsan");
    8. String name = jedis.get("name");
    9. System.out.println(name);
    10. // 3.关闭连接
    11. jedis.close();
    12. }

使用Jedis连接池

  1. private JedisPool jp = null;
  2. static {
  3. JedisPoolConfig jpc = new JedisPoolConfig();
  4. jpc.setMaxTotal(30);
  5. jpc.setMaxIdle(10);
  6. String host = "127.0.0.1";
  7. int port = 6379;
  8. jp = new JedisPool(jpc, host, port);
  9. }
  10. public static Jedis getJedis() {
  11. return jp.getResource();
  12. }