当前位置: 首页 > news >正文

Java Spring整合Redis工具类

直接上工具类

package com.jy.common.util;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * redis工具类
 * 
 * @author jingyuannet
 */
public final class RedisUtil {

    private static Logger logger = LogManager.getLogger(RedisUtil.class);

    private static String ADDR = "";

    private static int PORT = 6379;

    private static String AUTH = null;

    private static int MAX_ACTIVE = 300;

    private static int MAX_IDLE = 200;

    private static int MAX_WAIT = 10000;

    private static int TIMEOUT = 10000;

    private static boolean TEST_ON_BORROW = true;

    private static JedisPool jedisPool = null;

    private static Jedis jedis = null;

    /**
     * 初始化Redis连接池
     */
    static {
        try {
            init();
        } catch (Exception e) {
            logger.error("初始化Redis出错," + e);
        }
    }

    /**
     * 初始化连接池
     * 
     * @see [类、类#方法、类#成员]
     */
    private synchronized static void init() {
        ADDR = PropertyUtils.getValue("redis.addr");
        PORT = Integer.valueOf(PropertyUtils.getValue("redis.port"));
        AUTH = PropertyUtils.getValue("redis.auth");
        MAX_ACTIVE = Integer.valueOf(PropertyUtils
                .getValue("redis.pool.max_active"));
        MAX_IDLE = Integer.valueOf(PropertyUtils
                .getValue("redis.pool.max_idle"));
        MAX_WAIT = Integer.valueOf(PropertyUtils
                .getValue("redis.pool.max_wait"));
        TIMEOUT = Integer.valueOf(PropertyUtils.getValue("redis.pool.timeout"));

        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxIdle(MAX_IDLE);
        config.setMaxWaitMillis(MAX_WAIT);
        config.setTestOnBorrow(TEST_ON_BORROW);
        config.setMaxTotal(MAX_ACTIVE);
        jedisPool = new JedisPool(config, ADDR, PORT, TIMEOUT, AUTH);
    }

    /**
     * 获取Jedis实例
     * 
     * @return
     */
    private static Jedis getJedis() {
        try {
            if (jedisPool != null) {
                jedis = jedisPool.getResource();
            } else {
                init();
                jedis = jedisPool.getResource();
            }
        } catch (Exception e) {
            logger.error("获取Redis实例出错," + e);
        }
        return jedis;
    }

    /**
     * 设置单个值
     * 
     * @param key
     * @param value
     * @return
     */
    public static String set(String key, String value) {
        return set(key, value, null);
    }

    /**
     * 设置单个值,并设置超时时间
     * 
     * @param key
     *            键
     * @param value
     *            值
     * @param timeout
     *            超时时间(秒)
     * @return
     * @see [类、类#方法、类#成员]
     */
    public static String set(String key, String value, Integer timeout) {
        String result = null;

        Jedis jedis = RedisUtil.getJedis();
        if (jedis == null) {
            return result;
        }
        try {
            result = jedis.set(key, value);
            if (null != timeout) {
                jedis.expire(key, timeout);
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        } finally {
            if (null != jedis) {
                jedis.close();
            }
        }
        return result;
    }

    /**
     * 获取单个值
     * 
     * @param key
     * @return
     */
    public static String get(String key) {
        String result = null;
        Jedis jedis = RedisUtil.getJedis();
        if (jedis == null) {
            return result;
        }
        try {
            result = jedis.get(key);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        } finally {
            if (null != jedis) {
                jedis.close();
            }
        }
        return result;
    }

    /**
     * 删除redis中数据
     * 
     * @param key
     * @return
     * @see [类、类#方法、类#成员]
     */
    public static boolean del(String key) {
        Boolean result = Boolean.FALSE;
        Jedis jedis = RedisUtil.getJedis();
        if (null == jedis) {
            return Boolean.FALSE;
        }
        try {
            jedis.del(key);
        } catch (Exception e) {
            logger.error("删除redis数据出错," + e);
        } finally {
            if (null != jedis) {
                jedis.close();
            }
        }
        return result;
    }

    /**
     * 追加
     * 
     * @param key
     * @param value
     * @return
     * @see [类、类#方法、类#成员]
     */
    public static Long append(String key, String value) {
        Long result = Long.valueOf(0);
        Jedis jedis = RedisUtil.getJedis();
        if (null == jedis) {
            return result;
        }
        try {
            result = jedis.append(key, value);
        } catch (Exception e) {
            logger.error("追加redis数据出错," + e);
        } finally {
            if (null != jedis) {
                jedis.close();
            }
        }
        return result;
    }

    /**
     * 检测是否存在
     * 
     * @param key
     * @return
     * @see [类、类#方法、类#成员]
     */
    public static Boolean exists(String key) {
        Boolean result = Boolean.FALSE;
        Jedis jedis = RedisUtil.getJedis();
        if (null == jedis) {
            return result;
        }
        try {
            result = jedis.exists(key);
        } catch (Exception e) {
            logger.error("检查是否存在出错:," + e);
        } finally {
            if (null != jedis) {
                jedis.close();
            }
        }
        return result;
    }
}

相关文章:

  • 深入理解 Compose Navigation 实现原理
  • springboot小型教育网站的开发与建设毕业设计源码100853
  • js类型检测
  • 微服务网关Gateway实践总结
  • python学生成绩管理系统 毕业设计-附源码061011
  • springboot财务管理系统毕业设计-附源码061533
  • STM32与DS18B20数字温度传感器寄生供电方式的新方案与1-wire总线程序设计
  • python+nodejs+vue大学生心理健康测评管理系统
  • springboot呼伦贝尔旅游网站的设计与实现毕业设计源码091833
  • 基于Springboot超市管理系统毕业设计-附源码231443
  • SSM汽车订票系统毕业设计-附源码061801
  • springboot农村饮用水海量数据存储平台毕业设计-附源码061205
  • 【数学分析笔记03】上确界和下确界
  • HarmonyOS鸿蒙学习笔记(9)Navigator组件实现页面路由跳转
  • php遵义旅游管理系统毕业设计源码091801
  • 【108天】Java——《Head First Java》笔记(第1-4章)
  • 4月23日世界读书日 网络营销论坛推荐《正在爆发的营销革命》
  • CentOS6 编译安装 redis-3.2.3
  • CODING 缺陷管理功能正式开始公测
  • django开发-定时任务的使用
  • HomeBrew常规使用教程
  • Mac转Windows的拯救指南
  • Node项目之评分系统(二)- 数据库设计
  • spring-boot List转Page
  • SpringCloud集成分布式事务LCN (一)
  • unity如何实现一个固定宽度的orthagraphic相机
  • 机器学习学习笔记一
  • 基于Dubbo+ZooKeeper的分布式服务的实现
  • 精益 React 学习指南 (Lean React)- 1.5 React 与 DOM
  • 离散点最小(凸)包围边界查找
  • 理解在java “”i=i++;”所发生的事情
  • 聊聊redis的数据结构的应用
  • 前嗅ForeSpider采集配置界面介绍
  • 网页视频流m3u8/ts视频下载
  • 赢得Docker挑战最佳实践
  • ​Linux·i2c驱动架构​
  • (3)nginx 配置(nginx.conf)
  • (AngularJS)Angular 控制器之间通信初探
  • (分布式缓存)Redis持久化
  • (每日持续更新)jdk api之FileFilter基础、应用、实战
  • (三) diretfbrc详解
  • (译)计算距离、方位和更多经纬度之间的点
  • (转载)hibernate缓存
  • .Net MVC4 上传大文件,并保存表单
  • .NET 设计模式—适配器模式(Adapter Pattern)
  • .NET/C# 编译期能确定的字符串会在字符串暂存池中不会被 GC 垃圾回收掉
  • @ModelAttribute 注解
  • [ 蓝桥杯Web真题 ]-布局切换
  • [⑧ADRV902x]: Digital Pre-Distortion (DPD)学习笔记
  • [AAuto]给百宝箱增加娱乐功能
  • [Android]使用Android打包Unity工程
  • [Bugku]密码???[writeup]
  • [C#]手把手教你打造Socket的TCP通讯连接(一)
  • [codevs 2822] 爱在心中 【tarjan 算法】
  • [C语言][PTA基础C基础题目集] strtok 函数的理解与应用