Jedis API操作redis数据库
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数据库的更多相关文章
- Redis学习(5)-Jedis(Java操作redis数据库技术)
Java连接redis 一,导入jar包 Redis有什么命令,Jedis就有什么方法 设置防火墙 在Linux上面运行如下代码: 单实例:Jedis实例: package com.jedis.dem ...
- Jedis操作Redis数据库
添加Maven依赖: <dependencies> <!-- 单元测试 --> <dependency> <groupId>junit</grou ...
- Linux+Redis实战教程_day02_3、redis数据类型_4、String命令_5、hash命令_6、java操作redis数据库技术
3. redis数据类型[重点] redis 使用的是键值对保存数据.(map) key:全部都是字符串 value:有五种数据类型 Key名:自定义,key名不要过长,否则影响使用效率 Key名不要 ...
- 转 用C API 操作MySQL数据库
用C API 操作MySQL数据库 参考MYSQL的帮助文档整理 这里归纳了C API可使用的函数,并在下一节详细介绍了它们.请参见25.2.3节,“C API函数描述”. 函数 描述 mysql_a ...
- 操作redis数据库 & 操作Excel & 开发接口
操作redis数据库: string类型 1. 增 set,传俩个参数 key value(只要是字符串就行)2. 删 delete 传一个参数 key3. 修改 set 在目标key重新传参 key ...
- Python之操作redis数据库
使用redis模块 一.操作redis 1.添加信息 (1)直接建key-value信息: 右键-Add New Key,手动添加key和value 右键-Console,打开控制台,写入命令 (2) ...
- redis python 操作 Python操作Redis数据库
原文章于此:https://www.cnblogs.com/cnkai/p/7642787.html 有个人修改与改正 Python操作Redis数据库 连接数据库 StrictRedisfrom ...
- Jedis(java操作redis数据库技术)
Redis有什么命令,Jedis就有什么方法. 客户端无法连接时,需要考虑防火墙配置,比如6379端口是否开放,也可以直接关闭防火墙. Jedis连接池: import org.junit.Test; ...
- 使用Jedis操作Redis数据库
Redis不仅是使用命令来操作,现在基本上主流的语言都有客户端支持,比如java.C.C#.C++.php.Node.js.Go等. 在官方网站里列一些Java的客户端,有Jedis.Redisson ...
随机推荐
- iview-admin本地测试上线登陆问题和文件路径找不到问题
在项目中vue.config.js下修改上线路径(图中我修改为:根目录路径) 测试本地上线登陆出现问题: 在main.js下if (process.env.NODE_ENV !== 'producti ...
- js实现图片的Blob base64 ArrayBuffer 的各种转换
一.相关基础知识 构造函数 FileReader() 返回一个新构造的FileReader 事件处理 FileReader.onabort 处理abort事件.该事件在读取操作被中断时触发. Fil ...
- VIO的一些随笔
大公司跑在手机的似乎都是滤波MSCKF那种,有优化的但似乎功耗不行.还有就是杂交的前端滤波后面在挂地图,反正国内的似乎就是SVO, VINS, ORBSLAM,MSCKF组合起来. 缺啥补啥,那个太烂 ...
- deploy KBA 2167993
The default trace shows the following error: ****************************************** Unable to cr ...
- IntelliJ IDEA.2017.3.4(win7 64位)的安装使用
下载 1.Idea与Webstorm同为JetBrains公司的产品,因此安装使用方式也极为相似,首先我们打开IDEA的官方下载网站:https://www.jetbrains.com/idea/,点 ...
- mysql limit和offset用法
limit和offset用法 mysql里分页一般用limit来实现 1. select* from article LIMIT 1,3 2.select * from article LIMIT 3 ...
- sql 四舍五入 保留两位小数
一.问题描述 数据库里的 float momey 类型,都会精确到多位小数.但有时候 我们不需要那么精确,例如,只精确到两位有效数字. 二.sqlserver解决方案: 1. 使用 Round() 函 ...
- Vue使用ref 属性来获取DOM
注意,在父组件中可以使用this.$refs.属性名 获取任何元素的属性和方法,子组件不可以获取父组件中的 <!DOCTYPE html> <html lang="en& ...
- 再见 Docker,是时候拥抱下一代容器工具了
本文首发于:微信公众号「运维之美」,公众号 ID:Hi-Linux. 「运维之美」是一个有情怀.有态度,专注于 Linux 运维相关技术文章分享的公众号.公众号致力于为广大运维工作者分享各类技术文章和 ...
- [Jenkins][centos]1 持续集成 之 配置VNC,部署Jenkins
痛点:上一篇的AWS部署的VNC不知为啥挂了,死活连不上,因此改申请京东云做部署Jenkins 预计阅读时间:20分钟 更新软件,安装桌面 yum -y update yum -y groupinst ...