JAVA中通过Jedis操作Redis连接与插入简单库

一、简述

  JAVA 中通过 Jedis 操作 Redis 连接与插入简单库

二、依赖

        <!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.9.0</version>
        </dependency>

 

三、代码

package com.test.utils.redis;

import lombok.extern.log4j.Log4j2;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.Pipeline;
import com.test.utils.redis.items.KvItem;

import java.io.IOException;
import java.util.List;

@Log4j2
public class RedisUtils {
private final JedisPool jedisPool;
private int dbIndex;

</span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">
Redis辅助插入类。
* </span><span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 0, 255, 1)">public</span> RedisUtils(String host, <span style="color: rgba(0, 0, 255, 1)">int</span> post, <span style="color: rgba(0, 0, 255, 1)">int</span> timeout, String password, <span style="color: rgba(0, 0, 255, 1)">boolean</span> ssl, <span style="color: rgba(0, 0, 255, 1)">int</span> maxTotal, <span style="color: rgba(0, 0, 255, 1)">int</span> maxIdel, <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> dbIndex) {
    </span><span style="color: rgba(0, 0, 255, 1)">this</span>.dbIndex =<span style="color: rgba(0, 0, 0, 1)"> dbIndex;
    JedisPoolConfig config </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> JedisPoolConfig();
    config.setTestOnBorrow(</span><span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">);
    config.setMaxWaitMillis(</span>120000<span style="color: rgba(0, 0, 0, 1)">);
    config.setMaxIdle(maxIdel);
    config.setMaxTotal(maxTotal);
    jedisPool </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> JedisPool(config, host, post, timeout, password, ssl);
}

</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">boolean</span><span style="color: rgba(0, 0, 0, 1)"> checkConnection() {
    </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> {
        Jedis jedis </span>=<span style="color: rgba(0, 0, 0, 1)"> jedisPool.getResource();
        </span><span style="color: rgba(0, 0, 255, 1)">if</span> (jedis != <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">) {
            jedis.close();
            </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">;
        }
    } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (Exception ignored) {
        log.warn(</span>"[checkConnection] check redis connection failed. "<span style="color: rgba(0, 0, 0, 1)">, ignored);
    }
    </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">;
}

</span><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">synchronized</span> Jedis getJedis(<span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> maxRetry) {
    Jedis jedis </span>= <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">;
    Exception lastEx </span>= <span style="color: rgba(0, 0, 255, 1)">new</span> Exception("no error."<span style="color: rgba(0, 0, 0, 1)">);
    </span><span style="color: rgba(0, 0, 255, 1)">for</span> (<span style="color: rgba(0, 0, 255, 1)">int</span> i = 0; i &lt; maxRetry; i++<span style="color: rgba(0, 0, 0, 1)">) {
        </span><span style="color: rgba(0, 0, 255, 1)">if</span> (jedisPool == <span style="color: rgba(0, 0, 255, 1)">null</span>) <span style="color: rgba(0, 0, 255, 1)">break</span><span style="color: rgba(0, 0, 0, 1)">;
        </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> {
            jedis </span>=<span style="color: rgba(0, 0, 0, 1)"> jedisPool.getResource();
            </span><span style="color: rgba(0, 0, 255, 1)">if</span> (jedis == <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">) {
                Thread.sleep(</span>1000<span style="color: rgba(0, 0, 0, 1)">);
            } </span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)"> {
                jedis.select(dbIndex); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">临时使用</span>
                <span style="color: rgba(0, 0, 255, 1)">break</span><span style="color: rgba(0, 0, 0, 1)">;
            }
        } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (Exception e) {
            jedis </span>= <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">;
            lastEx </span>=<span style="color: rgba(0, 0, 0, 1)"> e;
        }
    }
    </span><span style="color: rgba(0, 0, 255, 1)">if</span> (jedis == <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">) {
        log.error(</span>"[get a jedis] get a jedis from pools failed, has been retry [" + maxRetry + "] times. please check connection. "<span style="color: rgba(0, 0, 0, 1)">, lastEx);
    }
    </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> jedis;
}

</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">synchronized</span> <span style="color: rgba(0, 0, 255, 1)">boolean</span> add(List&lt;KvItem&gt;<span style="color: rgba(0, 0, 0, 1)"> item) {
    </span><span style="color: rgba(0, 0, 255, 1)">if</span> (item == <span style="color: rgba(0, 0, 255, 1)">null</span> || item.isEmpty()) <span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">;
    </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> {
        Jedis jedis </span>= getJedis(300<span style="color: rgba(0, 0, 0, 1)">);
        </span><span style="color: rgba(0, 0, 255, 1)">if</span> (jedis == <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">) {
            log.error(</span>"[add to redis] add to [" + item.size() + "] items to redis, but get a jedis failed. please check"<span style="color: rgba(0, 0, 0, 1)">);
            </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">;
        }
        Pipeline p </span>=<span style="color: rgba(0, 0, 0, 1)"> jedis.pipelined();
        </span><span style="color: rgba(0, 0, 255, 1)">for</span><span style="color: rgba(0, 0, 0, 1)"> (KvItem kv : item) {
            p.set(kv.getKey(), kv.getValue());
        }
        p.sync();
        </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> {
            p.close();
        } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (IOException e) {
            log.warn(</span>"[add to redis] close jedis Pipeline failed."<span style="color: rgba(0, 0, 0, 1)">, e);
        }
        jedis.close();
        </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">;
    } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (Exception ex) {
        log.warn(</span>"[add to redis] occur a error."<span style="color: rgba(0, 0, 0, 1)">, ex);
        </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">;
    }
}

}