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. Matlab匿名函数,子函数,私有函数,重载函数,eval和feval函数

    匿名函数,子函数,私有函数等函数类型 匿名函数: 匿名函数没有函数名,也不是.m文件,只包含一个表达式和输入输出参数. Fxy=@(x,y)x.^y+3*x*y x,y为输入输入参数,Fxy为函数名 ...

  2. 炫酷的可视化工具包——cufflinks

    前言 学过Python数据分析的朋友都知道,在可视化的工具中,有很多优秀的三方库,比如matplotlib,seaborn,plotly,Boken,pyecharts等等.这些可视化库都有自己的特点 ...

  3. 我是怎么和SAP结缘的 - Jerry的SAP校园招聘之路

    2006年9月,结束了一年的北京中科院实习后,我回到了电子科技大学,此时已经是研三上学期了.有着"金九银十"之称的秋季校园招聘正式开始了. 准备好了简历后,Jerry也加入了浩浩荡 ...

  4. OpenStack kilo版(6) 启动第一台虚拟机

    创建网络 提供者为external,类型为flat,网络名称为public,: root@controller:~# neutron net-create --shared --provider:ph ...

  5. 解决 google 浏览器记住密码导致输入框样式改变(变成淡黄色背景)

    直接在页面上使用css代码: input:-webkit-autofill , textarea:-webkit-autofill, select:-webkit-autofill { -webkit ...

  6. 【robotframework】robotframework环境搭建

    一.基于python3.6环境 在dos命令输入 pip install robotframework 在线安装robotframework在dos命令输入 pip install Pypubsub= ...

  7. Linux命令——chkconfig

    拓展:如何增加一个系统服务service chkconfig本身用法不复杂,其作用是控制service是否开机启动. 对于CentOS而言,7.X版本已经不再使用SysV,SysV相关的命令基本上没有 ...

  8. python 多分类任务中按照类别分层采样

    在机器学习多分类任务中有时候需要针对类别进行分层采样,比如说类别不均衡的数据,这时候随机采样会造成训练集.验证集.测试集中不同类别的数据比例不一样,这是会在一定程度上影响分类器的性能的,这时候就需要进 ...

  9. String decryption with de4dot

    Introduction de4dot is a wonderful tool for deobfuscating known and unknown .NET protections. Dealin ...

  10. 模拟webpack 实现自己的打包工具

    本框架模拟webpack打包工具 详细代码个步骤请看git地址:https://github.com/jiangzhenfei/easy-webpack 创建package.json { " ...