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. WPF不同方式快捷键判断

    private void Window_PreviewKeyDown(object sender, KeyEventArgs e) { //单个按键e.Key方式判断 if (e.Key == Key ...

  2. flask/app.py-add_url_rule源码分析

    之前分析route方法的时候,可以看到中间会调用add_url_rule方法,add_url_rule方法和route方法一样属于Flask这个类的. add_url_rule方法主要用来连接url规 ...

  3. stm32 内部flash

    嵌入式闪存 闪存存储器有主存储块和信息块组成 大容量产品主存储块最大为64K×64位,每个存储块划分为256个2K字节的页 编程和擦除闪存 闪存编程一次可以写入16位(半字) 闪存擦除操作可以按页面擦 ...

  4. Android studio如何连接三星手机

    打开Android studio,选择Trouble Shoot device connections: 这个菜单项会自动扫描本地已经用USB线连接的设备: 已经成功扫描出了我的Galaxy C7 P ...

  5. Python-共享引用

    A会改变么? 下面三小段代码,A的值都会改变么? >>> A = "spam" >>> B = A >>> B = " ...

  6. 使用Arduino开发板控制步进电机

    目前为止,我还没有真正深入了解过电机,特别是步进电机. 最近我在计划一个项目,需要相对精确的电机控制,所以可能会使用到步进电机,但很快就意识到我首先应该更多地了解这些. 本篇文章主要介绍我到目前为止学 ...

  7. 线性查找与二分查找(python)

    # -*- coding: utf-8 -*- number_list = [0, 1, 2, 3, 4, 5, 6, 7] def linear_search(value, iterable): f ...

  8. <<回想>>

    算是一个简单的回忆录,文笔很差,愧对语文老师 突然发现上一篇回忆录,没错就是那个流水账,是去年今天写的...   这是2019年7月的一天,NOI2019刚刚落下帷幕,而小F,则百无聊赖地在高铁站等车 ...

  9. redis 与 序列化

    概念 序列化:把对象转化为可传输的字节序列过程称为序列化. 反序列化:把字节序列还原为对象的过程称为反序列化. 为什么需要序列化 序列化最终的目的是为了对象可以跨平台存储,和进行网络传输.而我们进行跨 ...

  10. python_面向对象——封装

    1.私有属性 class Person(object): def __init__(self,name,age): self.name = name self.age = age #实例属性 self ...