在 Java 中使用 redis

redis 支持不少語言的客戶端。在官方網站上有支持的全部的 redis 客戶端列表java

由於平時使用 java 做爲開發語言,因此這裏描述一下如何經過 java 來鏈接和操做 redis 服務器。在官方文檔中, Java 推薦的 redis 客戶端是 Jedis ,這裏咱們也用這個客戶端對 redis 服務器進行操做。git


引入依賴

首先咱們創建一個 maven 工程,在工程的 pom.xml 文件中加入 Jedis 的依賴引用。爲了方便測試,還加入了 Junit 依賴。文件內容以下。github

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.x9710.common</groupId>
<artifactId>redis-util</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
    <dependency>
        <groupId>commons-logging</groupId>
        <artifactId>commons-logging</artifactId>
        <version>1.1.1</version>
    </dependency>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.17</version>
    </dependency>
    <dependency>
        <groupId>redis.clients</groupId>
        <artifactId>jedis</artifactId>
        <version>2.9.0</version>
    </dependency>

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>
</dependencies>
</project>
複製代碼

建立鏈接類

創建 redis 鏈接類 com.x9710.common.redis.RedisConnection 。內容以下 package com.x9710.common.redis;redis

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

public class RedisConnection {
   /**
   * redis 鏈接池配置信息
   */
  private JedisPoolConfig jedisPoolConfig;
   /**
   * redis 服務器地址
   */
  private String ip;

  /**
   * redis 服務器端口
   */
  private Integer port;

  /**
   * redis 服務器密碼
   */
  private String pwd;

   /**
   * redis 服務器鏈接超時時間
   */
  private Integer timeOut;

   /**
   * redis 鏈接客戶端名稱
   */
  private String clientName = null;

  private JedisPool jedisPool;

   public void setJedisPoolConfig(JedisPoolConfig jedisPoolConfig) {
     this.jedisPoolConfig = jedisPoolConfig;
  }

  public void setIp(String ip) {
    this.ip = ip;
  }

  public void setPort(Integer port) {
    this.port = port;
  }

  public void setPwd(String pwd) {
    this.pwd = pwd;
  }

public void setTimeOut(Integer timeOut) {
    this.timeOut = timeOut;
}

public void setClientName(String clientName) {
    this.clientName = clientName;
}

private void buildConnection() {
    if (jedisPool == null) {
        if (jedisPoolConfig == null) {
            jedisPool = new JedisPool(new JedisPoolConfig(), ip, port, timeOut, pwd, 0, clientName);
        } else {
            jedisPool = new JedisPool(jedisPoolConfig, ip, port, timeOut, pwd, 0, clientName);
        }
    }
}

public Jedis getJedis() {
    buildConnection();
    if (jedisPool != null) {
        return jedisPool.getResource();
    }
    return null;
}
複製代碼

}apache


編寫測試

用一個測試類 com.x9710.common.redis.test.RedisConnectionTest 來測試 rdis 鏈接功能.服務器

package com.x9710.common.redis.test;

import com.x9710.common.redis.RedisConnection;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPoolConfig;

public class RedisConnectionTest {
private RedisConnection redisConnection;

@Before
public void before() {
    JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
    //設置 redis 鏈接池最大鏈接數量
    jedisPoolConfig.setMaxTotal(50);
    //設置 redis 鏈接池最大空閒鏈接數量
    jedisPoolConfig.setMaxIdle(10);
    //設置 redis 鏈接池最小空閒鏈接數量
    jedisPoolConfig.setMinIdle(1);
    redisConnection = new RedisConnection();
    redisConnection.setIp("10.110.2.56");
    redisConnection.setPort(52981);
    redisConnection.setPwd("hhSbcpotThgWdnxJNhrzwstSP20DvYOldkjf");
    redisConnection.setClientName(Thread.currentThread().getName());
    redisConnection.setTimeOut(600);
    redisConnection.setJedisPoolConfig(jedisPoolConfig);
}

@Test
public void testPutGet() {
    Jedis jedis = redisConnection.getJedis();
    try {
        jedis.select(1);
        jedis.set("name","grace");
        Assert.assertTrue("grace".equals(jedis.get("name")));
    } finally {
        if (jedis != null) {
            jedis.close();
        }
    }
}
}
複製代碼

在 ide 環境中執行測試用例,結果以下。 maven

測試用例執行結果

如今,咱們就在 Java 中利用 Jedit 客戶端創建和 redis 的鏈接而且能夠執行操做。對應的代碼發佈到了 GitHubide

原文發表在簡書中,原始連接測試

相關文章
相關標籤/搜索