一、简述

  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; /*
Redis辅助插入类。
* */
public RedisUtils(String host, int post, int timeout, String password, boolean ssl, int maxTotal, int maxIdel, int dbIndex) {
this.dbIndex = dbIndex;
JedisPoolConfig config = new JedisPoolConfig();
config.setTestOnBorrow(true);
config.setMaxWaitMillis(120000);
config.setMaxIdle(maxIdel);
config.setMaxTotal(maxTotal);
jedisPool = new JedisPool(config, host, post, timeout, password, ssl);
} public boolean checkConnection() {
try {
Jedis jedis = jedisPool.getResource();
if (jedis != null) {
jedis.close();
return true;
}
} catch (Exception ignored) {
log.warn("[checkConnection] check redis connection failed. ", ignored);
}
return false;
} private synchronized Jedis getJedis(int maxRetry) {
Jedis jedis = null;
Exception lastEx = new Exception("no error.");
for (int i = 0; i < maxRetry; i++) {
if (jedisPool == null) break;
try {
jedis = jedisPool.getResource();
if (jedis == null) {
Thread.sleep(1000);
} else {
jedis.select(dbIndex); //临时使用
break;
}
} catch (Exception e) {
jedis = null;
lastEx = e;
}
}
if (jedis == null) {
log.error("[get a jedis] get a jedis from pools failed, has been retry [" + maxRetry + "] times. please check connection. ", lastEx);
}
return jedis;
} public synchronized boolean add(List<KvItem> item) {
if (item == null || item.isEmpty()) return true;
try {
Jedis jedis = getJedis(300);
if (jedis == null) {
log.error("[add to redis] add to [" + item.size() + "] items to redis, but get a jedis failed. please check");
return false;
}
Pipeline p = jedis.pipelined();
for (KvItem kv : item) {
p.set(kv.getKey(), kv.getValue());
}
p.sync();
try {
p.close();
} catch (IOException e) {
log.warn("[add to redis] close jedis Pipeline failed.", e);
}
jedis.close();
return true;
} catch (Exception ex) {
log.warn("[add to redis] occur a error.", ex);
return false;
}
}
}

最新文章

  1. tensorflow中的lstm的state
  2. SElinux对一些服务关系的影响
  3. 算法系列:FFT 003
  4. Java经典实例:使用DateFormatter来格式化日期时间
  5. Leetcode 190 Reverse Bits 位运算
  6. asp.net mvc 4 json大数据异常 提示JSON字符长度超出限制的异常
  7. 定制你的Unity编辑器
  8. [Reprint]C++友元函数与拷贝构造函数详解
  9. iOS7.1 编译报错 解决方案 体会
  10. HTML5 Canvas Cheat Sheet
  11. less可以做什么?less详解!(less嵌套选择器实现纯CSS二级导航)
  12. Python中安装模块的方法
  13. 网络流 P3358 最长k可重区间集问题
  14. centos7下安装docker(25docker swarm---replicated mode&amp;global mode)
  15. 【mysql】不可不知的Metadata Lock
  16. u-boot移植(十三)---代码修改---裁剪及环境变量 一
  17. C++经典面试题(最全,面中率最高)
  18. matplotlib使用GridSpec调整子图位置大小 (非对称的子图)
  19. OK335xS Ubuntu 12.04.1 版本 Android 开发环境搭建
  20. html select options &amp; vue h render

热门文章

  1. day24 面向对象,交互,组合,命名空间,初始继承
  2. 页面滚动到指定class样式位置
  3. 函数模拟sort快排
  4. SQL语句中单引号、双引号和反引号的区分
  5. Django之setting文件
  6. datatables出现横向滚动条
  7. typescript精简版1:用ts表示常见数据类型
  8. RFC2616-HTTP1.1-Header Field Definitions(头字段规定部分—单词注释版)
  9. 牛客练习赛35-函数的魔法-floyd
  10. vs2008单元测试