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. PHP即时实时输出内容

    一般情况下,PHP都是将整个页面全部执行完成后,才会把要输出的内容发送回客户端. for ($i = 0; $i < 10; $i++) { echo $i; sleep(1); } 这段代码会 ...

  2. 转:common.js 常用js公共函数库

    转自其他博主,自己开发备用 var h = {}; h.get = function (url, data, ok, error) { $.ajax({ url: url, data: data, d ...

  3. POJ3255(Roadblocks)--次短路径

    点这里看题目 3228K 485MS G++ 2453B 根据题意和测试用例知道这是一个求次短路径的题目.次短路径,就是比最短路径长那么一丢丢的路径,而题中又是要求从一点到指定点的次短路径,果断Dij ...

  4. 安卓开发之cache 的使用(图片查看器案例)

    package com.lidaochen.test; import android.graphics.Bitmap; import android.graphics.BitmapFactory; i ...

  5. java.sql.Date和java.sql.Timestamp转换

    转自:https://www.xuebuyuan.com/1479399.html 在开发web应用中,针对不同的数据库日期类型,我们需要在我们的程序中对日期类型做各种不同的转换.若对应数据库数据是o ...

  6. iOS Touch Id 开发

    Touch Id Touch Id是iPhone5S后加入的一项新的功能,也就是大家熟知的指纹识别技术.大家用得最多的可能是手机的解屏操作,不用在和以前一样输入手机的四位数密码进行验证.一方面不用担心 ...

  7. Vue使用ref 属性来获取DOM

    注意,在父组件中可以使用this.$refs.属性名  获取任何元素的属性和方法,子组件不可以获取父组件中的 <!DOCTYPE html> <html lang="en& ...

  8. C++——virtual function

    无论是pure virtual还是impure virtual,都允许子类override他.但是真两种方式还是有一点差别,如果是pure virtual,那么父类是十分强烈希望子类override他 ...

  9. 001——搭建OpenCV实验环境

    开发环境 VS 2017 15.7.6 OpenCV 3.4.1 搭建环境 设置环境变量 创建Win32 空项目 配置属性管理器 测试代码 #include<opencv2/opencv.hpp ...

  10. tomcat部署(一)

    Tomcat部署最佳实践 标签: linux 笔者Q:972581034 交流群:605799367.有任何疑问可与笔者或加群交流 tomcat是玩web软件必会技能之一,今天我给大家介绍一下tomc ...