1、配置文件

classpath路径下,新建redis.properties配置文件

配置文件内容

# Redis settings
redis.host=127.0.0.1
redis.port=6379
redis.timeout=10000
redis.maxIdle=300
redis.maxTotal=600
# 毫秒
redis.maxWaitMillis=1000
redis.testOnBorrow=false

新建属性文件工具类,用来读取redis.properties配置文件

/**
* <p>属性文件工具类
*
* @author xupeng
* @date 2019/10/28 10:39
*/
public class PropertyUtil { //加载property文件到io流里面
public static Properties loadProperties(String fileName) {
Properties properties = new Properties();
try {
InputStream is = PropertyUtil.class.getClassLoader().getResourceAsStream(fileName);
properties.load(is);
} catch (IOException e) {
e.printStackTrace();
}
return properties;
} /**
* 根据key值取得对应的value值
*
* @param key
* @return
*/
public static String getValue(String fileName, String key) {
Properties properties = loadProperties(fileName);
return properties.getProperty(key);
} }

新建Jedis工具类,封装常用方法

/**
* <p>Jedis工具类
*
* @author xupeng
* @date 2019/10/28 11:02
*/
public class JedisUtil { private Logger logger = LoggerFactory.getLogger(JedisUtil.class); private static JedisPool jedisPool = null; private JedisUtil(){} static {
Properties properties = PropertyUtil.loadProperties("redis.properties");
String host = properties.getProperty("redis.host");
String port = properties.getProperty("redis.port");
String timeout = properties.getProperty("redis.timeout");
String maxIdle = properties.getProperty("redis.maxIdle");
String maxTotal = properties.getProperty("redis.maxTotal");
String maxWaitMillis = properties.getProperty("redis.maxWaitMillis");
String testOnBorrow = properties.getProperty("redis.testOnBorrow"); JedisPoolConfig config = new JedisPoolConfig();
config.setMaxIdle(Integer.parseInt(maxIdle));
config.setMaxTotal(Integer.parseInt(maxTotal));
config.setMaxWaitMillis(Long.parseLong(maxWaitMillis));
config.setTestOnBorrow(Boolean.valueOf(testOnBorrow));
jedisPool = new JedisPool(config, host, Integer.parseInt(port), Integer.parseInt(timeout));
} /**
* <p>从jedis连接池中获取获取jedis对象
*/
private Jedis getJedis() {
return jedisPool.getResource();
} private static final JedisUtil jedisUtil = new JedisUtil(); /**
* <p>获取JedisUtil实例
*/
public static JedisUtil getInstance() {
return jedisUtil;
} /**
* <p>设置值
* @param key 键
* @param value 值
*/
public String set(String key,String value){
String status = null;
Jedis jedis = null;
try {
jedis = getJedis();
if (null != jedis){
status = jedis.set(key, value);
}
}catch (Exception e){
logger.info("Unable to get connection from connection pool");
}finally {
if (null != jedis){
jedis.close();
}
}
return status;
} /**
* <p>根据键名称获取值
* @param key 键名称
* @return 值
*/
public String get(String key){
Jedis jedis = null;
String value = null;
try {
jedis = getJedis();
if (null != jedis){
value = jedis.get(key); }
}catch (Exception e){
logger.info("Unable to get connection from connection pool");
}finally {
if (null != jedis){
jedis.close();
}
}
return value;
} /**
* <p>如果存在,不设置,返回0;不存在,进行设置,返回1。
* @param key 键
* @param value 值
*/
public Long setnx(String key,String value){
Long setnx = null;
Jedis jedis = null;
try {
jedis = getJedis();
if (null != jedis){
setnx = jedis.setnx(key, value);
}
}catch (Exception e){
logger.info("Unable to get connection from connection pool");
}finally {
if (null != jedis){
jedis.close();
}
}
return setnx;
} /**
* <p>设置key的有效时间
* @param key 键
* @param value 值
*/
public String setex(String key,int seconds,String value){
String setex = null;
Jedis jedis = null;
try {
jedis = getJedis();
if (null != jedis){
setex = jedis.setex(key, seconds, value);
}
}catch (Exception e){
logger.info("Unable to get connection from connection pool");
}finally {
if (null != jedis){
jedis.close();
}
}
return setex;
} /**
* <p>对某个值进行递增
* @param key 键
*/
public Long incr(String key){
Long incr = null;
Jedis jedis = null;
try {
jedis = getJedis();
if (null != jedis){
incr = jedis.incr(key);
}
}catch (Exception e){
logger.info("Unable to get connection from connection pool");
}finally {
if (null != jedis){
jedis.close();
}
}
return incr;
} /**
* <p>对某个值进行递减
* @param key 键
*/
public Long decr(String key){
Long incr = null;
Jedis jedis = null;
try {
jedis = getJedis();
if (null != jedis){
incr = jedis.decr(key);
}
}catch (Exception e){
logger.info("Unable to get connection from connection pool");
}finally {
if (null != jedis){
jedis.close();
}
}
return incr;
} /**
* <p>设置一个map类型的值
*
* @author zhuangxupeng
* @date 2019/10/31 10:30
*/
public String hmset(String key, Map<String,String> map){
Jedis jedis = null;
String hmset = null;
try {
jedis = getJedis();
if (null != jedis){
hmset = jedis.hmset(key, map);
}
}catch (Exception e){
logger.info("Unable to get connection from connection pool");
}finally {
if (null != jedis){
jedis.close();
}
}
return hmset;
} /**
* <p>获取一个map类型的值
*
* @author zhuangxupeng
* @date 2019/10/31 10:30
*/
public Map<String, String> hgetAll(String key){
Jedis jedis = null;
Map<String, String> map = null;
try {
jedis = getJedis();
if (null != jedis){
map = jedis.hgetAll(key);
}
}catch (Exception e){
logger.info("Unable to get connection from connection pool");
}finally {
if (null != jedis){
jedis.close();
}
}
return map;
} /**
* <p>获取key对应map的大小
*
* @author zhuangxupeng
* @date 2019/10/31 10:30
*/
public Long hlen(String key){
Jedis jedis = null;
Long hlen = null;
try {
jedis = getJedis();
if (null != jedis){
hlen = jedis.hlen(key);
}
}catch (Exception e){
logger.info("Unable to get connection from connection pool");
}finally {
if (null != jedis){
jedis.close();
}
}
return hlen;
} /**
* <p>获取key对应map的所有的键的集合
*
* @author zhuangxupeng
* @date 2019/10/31 10:30
*/
public Set<String> hkeys(String key){
Jedis jedis = null;
Set<String> sets = null;
try {
jedis = getJedis();
if (null != jedis){
sets = jedis.hkeys(key);
}
}catch (Exception e){
logger.info("Unable to get connection from connection pool");
}finally {
if (null != jedis){
jedis.close();
}
}
return sets;
} /**
* <p>获取key对应map的所有的值的集合
*
* @author zhuangxupeng
* @date 2019/10/31 10:30
*/
public List<String> hvals(String key){
Jedis jedis = null;
List<String> list = null;
try {
jedis = getJedis();
if (null != jedis){
list = jedis.hvals(key); }
}catch (Exception e){
logger.info("Unable to get connection from connection pool");
}finally {
if (null != jedis){
jedis.close();
}
}
return list;
} /**
* <p>删除
*
* @param key 键名称
* @return del 删除成功返回1,失败返回0
*/
public long del(String key) {
Jedis jedis = null;
Long del = null;
try {
jedis = getJedis();
if (null != jedis){
del = jedis.del(key);
}
}catch (Exception e){
logger.info("Unable to get connection from connection pool");
}finally {
if (null != jedis){
jedis.close();
}
}
return del;
} /**
* <p>是否存在KEY
* @param key 键
*/
public boolean exists(String key) {
Jedis jedis = null;
Boolean exists = null;
try {
jedis = getJedis();
if (null != jedis){
exists = jedis.exists(key);
}
}catch (Exception e){
logger.info("Unable to get connection from connection pool");
}finally {
if (null != jedis){
jedis.close();
}
}
return exists;
} /**
* <p>设置失效时间
* @param key 键名称
* @param seconds 秒数
*/
public Long expire(String key, int seconds) {
Jedis jedis = null;
Long aLong = null;
try {
jedis = getJedis();
if (null != jedis){
aLong = jedis.expire(key, seconds);
}
}catch (Exception e){
logger.info("Unable to get connection from connection pool");
}finally {
if (null != jedis){
jedis.close();
}
}
return aLong;
} /**
* 删除失效时间
* @param key
*/
public Long persist(String key) {
Jedis jedis = null;
Long persist = null;
try {
jedis = getJedis();
if (null != jedis){
persist = jedis.persist(key);
}
}catch (Exception e){
logger.info("Unable to get connection from connection pool");
}finally {
if (null != jedis){
jedis.close();
}
}
return persist;
} /**
* <p>添加sorted set
* <p>已经存在的值,再次添加会失败。
*
* @param key 键名称
* @param score 分数值
* @param value 实际值
* @return zadd 成功返回1,失败返回0。
*/
public Long zadd(String key,double score,String value) {
Jedis jedis = null;
Long zadd = null;
try {
jedis = getJedis();
zadd = jedis.zadd(key, score, value);
}catch (Exception e){
logger.info("Unable to get connection from connection pool");
}finally {
if (null != jedis){
jedis.close();
}
}
return zadd;
} /**
* 返回指定下标的集合元素
* @param key
* @param start 0为第一个
* @param end -1为最后一个
* @return
*/
public Set<String> zrange(String key, int start, int end) {
Jedis jedis = null;
Set<String> sets = null;
try {
jedis = getJedis();
sets = jedis.zrange(key, start, end);
}catch (Exception e){
logger.info("Unable to get connection from connection pool");
}finally {
if (null != jedis){
jedis.close();
}
}
return sets;
} }

写一个main方法,来进行简单测试

/**
* <p>Jedis客户端操作redis的string数据类型
* @author xupeng
* @date 2019年10月28日
*/
public class JedisStringDemo {
public static void main(String[] args) { JedisUtil instance = JedisUtil.getInstance();
instance.set("name", "zhuang"); String getNameVal = instance.get("name");
System.out.println("get name value:" + getNameVal); }
}

Jedis API操作redis数据库的更多相关文章

  1. Redis学习(5)-Jedis(Java操作redis数据库技术)

    Java连接redis 一,导入jar包 Redis有什么命令,Jedis就有什么方法 设置防火墙 在Linux上面运行如下代码: 单实例:Jedis实例: package com.jedis.dem ...

  2. Jedis操作Redis数据库

    添加Maven依赖: <dependencies> <!-- 单元测试 --> <dependency> <groupId>junit</grou ...

  3. Linux+Redis实战教程_day02_3、redis数据类型_4、String命令_5、hash命令_6、java操作redis数据库技术

    3. redis数据类型[重点] redis 使用的是键值对保存数据.(map) key:全部都是字符串 value:有五种数据类型 Key名:自定义,key名不要过长,否则影响使用效率 Key名不要 ...

  4. 转 用C API 操作MySQL数据库

    用C API 操作MySQL数据库 参考MYSQL的帮助文档整理 这里归纳了C API可使用的函数,并在下一节详细介绍了它们.请参见25.2.3节,“C API函数描述”. 函数 描述 mysql_a ...

  5. 操作redis数据库 & 操作Excel & 开发接口

    操作redis数据库: string类型 1. 增 set,传俩个参数 key value(只要是字符串就行)2. 删 delete 传一个参数 key3. 修改 set 在目标key重新传参 key ...

  6. Python之操作redis数据库

    使用redis模块 一.操作redis 1.添加信息 (1)直接建key-value信息: 右键-Add New Key,手动添加key和value 右键-Console,打开控制台,写入命令 (2) ...

  7. redis python 操作 Python操作Redis数据库

    原文章于此:https://www.cnblogs.com/cnkai/p/7642787.html 有个人修改与改正 Python操作Redis数据库   连接数据库 StrictRedisfrom ...

  8. Jedis(java操作redis数据库技术)

    Redis有什么命令,Jedis就有什么方法. 客户端无法连接时,需要考虑防火墙配置,比如6379端口是否开放,也可以直接关闭防火墙. Jedis连接池: import org.junit.Test; ...

  9. 使用Jedis操作Redis数据库

    Redis不仅是使用命令来操作,现在基本上主流的语言都有客户端支持,比如java.C.C#.C++.php.Node.js.Go等. 在官方网站里列一些Java的客户端,有Jedis.Redisson ...

随机推荐

  1. javascript 之 命名空间

    注意点: 1/IIFE是现代js框架最主要的基础设施,保证变量不被污染. 2/基本上我们把命名空间等同于框架的名字. 内容 一.简单的命名空间 <input type="button& ...

  2. MySQL中You can't specify target table '表名'('sn_app_label') for update in FROM clause错误解决办法

    在有些时候有级联关系的数据放在了同一张表中,在写sql语句的时候可能会遇到这样的场景:我要插入两条数据,第一条是父节点,第二条是子节点,关联关系是父节点的自增长id:在写这样的sql语句时有可能就会出 ...

  3. robot framework 如何处理循环条件下面的变量自增

    下面举了一个基础栗子,可以运行的.${num}就是我需要的自增变量.有人也许会问为什么不用${i},不是我不想用,而是我${i}有其他用处,必须另外定义一个变量,需要注意的是定义变量的时候,应该在循环 ...

  4. 两通道实信号使用一个FFT同时计算算法

    前言 在工程的实际应用场景中,往往是需要最省资源量.而DSP资源和BRAM资源对FPGA来说弥足珍贵. 对于同时存在多个通道的实信号需要做FFT而言,常规做法是每个通道用一个FFT IP,FFT IP ...

  5. 开发一个简单的工具,导出github仓库所有issue列表

    Jerry有一个github仓库,专门用来存放自己的知识管理,通过一条条的issue来记录具体的知识点: https://github.com/i042416/KnowlegeRepository/i ...

  6. array_map 去除数组参数里面左右两端空格

    <?php class Test{ public function trimArray($params){ if (!is_array($params)) return trim($params ...

  7. c++混合使用不同标准编译潜在的问题

    最近项目使用的C++的版本到C++11了,但是由于有些静态库(.a)没有源码,因此链接时还在使用非C++11版本的库文件.目前跑了几天,似乎是没出什么问题,但是我还是想说一下这样做有哪些潜在的风险. ...

  8. 在Linux中安装适用于arm64位的nodejs

    # 安装适用于arm64位的nodejs runtime v10.16.3 mkdir /runtimes cd /runtimes wget https://nodejs.org/dist/v10. ...

  9. 网上的JAVA语言的某个测试框架

    https://github.com/wenchengyao/testLJTT.git 使用maven打包,mvn clean install 在运行的时候,java -jar testLJTT.ja ...

  10. PL/SQL嵌入SQL语句

    一.PL/SQL块中只能直接嵌入SELECT.DML(INSERT,UPDATE,DELETE)以及事务控制语句(COMMIT,ROLLBACK,SAVEPOINT), 而不能直接嵌入DDL语句(CR ...