Redis的util类,求指导

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

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

public class RedisUtils {
    
    private static final Log log = LogFactory.getLog(RedisUtils.class);
    
    private static JedisPool rPool = null;
    
    private static JedisPool wPool = null;
    
    static{
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxTotal(BaseConstants.REDIS_MAX_TOTAL);
        config.setMaxIdle(BaseConstants.REDIS_MAX_IDLE);
        config.setMaxWaitMillis(BaseConstants.REDIS_TIME_OUT);
        config.setTestOnBorrow(BaseConstants.REDIS_TESTONBORROW);
        rPool = new JedisPool(config, Config.INSTANCE.getConfigValue("redis.r.host"),
                Integer.parseInt(Config.INSTANCE.getConfigValue("redis.r.port")));
        wPool = new JedisPool(config, Config.INSTANCE.getConfigValue("redis.w.host"),
                Integer.parseInt(Config.INSTANCE.getConfigValue("redis.w.port")));
    }
    
    /**
     * Get A New Redis Connection From Read Redis Pool To Read Data!
     * @return
     */
    public static Jedis getReadJedis(){
        Jedis jedis = rPool.getResource();
        if(StringUtils.notEmpty(Config.INSTANCE.getConfigValue("redis.r.pwd"))){
            jedis.auth(Config.INSTANCE.getConfigValue("redis.r.pwd"));
        }
        log.info("Get A New Redis Connection From Read Redis Pool To Read Data!");
        return jedis;
    }
    
    /**
     * Get A New Redis Connection From Write Redis Pool To Output Data!
     * @return
     */
    public static Jedis getWriteJedis(){
        Jedis jedis = wPool.getResource();
        if(StringUtils.notEmpty(Config.INSTANCE.getConfigValue("redis.w.pwd"))){
            jedis.auth(Config.INSTANCE.getConfigValue("redis.w.pwd"));
        }
        log.info("Get A New Redis Connection From Write Redis Pool To Output Data!");
        return jedis;
    }
    
    /**
     * Get A New Redis Connection appointing the db index From Write Redis Pool To Output Data!
     * @param Index
     * @return
     */
    public static Jedis getWriteJedis(int Index){
        Jedis jedis = getWriteJedis();
        jedis.select(Index);
        return jedis;
    }  
    
    /**
     * Realeas Read Redis Connection!
     * @param jedis the redis to be realeas
     */
    public static void releasReadJedis(Jedis rJedis){
        try {
            if(rJedis!=null){
                rJedis.disconnect();   
                rPool.returnResource(rJedis);
                log.info("Realeas Read Redis Connection!");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
   
    /**
     * Realeas Write Redis Connection!
     * @param jedis the redis to be realeas
     */
    public static void releasWriteJedis(Jedis wJedis){
        try {
            if(wJedis!=null){
                wJedis.disconnect();   
                wPool.returnResource(wJedis);
                log.info("Realeas Write Redis Connection!");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
   
    /**
     * destory read redis pool
     */
    public static void destoryReadPool(){
        if(rPool != null){
            rPool.destroy();
            log.info("Destory Read Redis Pool!");
        }
    }
    
    /**
     * destory write redis pool
     */
    public static void destoryWritePool(){
        if(wPool != null){
            wPool.destroy();
            log.info("Destory Write Redis Pool!");
        }
    }
   
    /**
     * destory all redis pool
     */
    public static void destoryAllPool(){
        destoryReadPool();
        destoryWritePool();
    }
}

时间: 2025-01-07 13:42:45

Redis的util类,求指导的相关文章

spring boot 结合Redis 实现工具类

自己整理了 spring boot 结合 Redis 的工具类引入依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId></dependency>加入配置 # Redis数据库索引(默认为0)spring.redis.database=0# Redis服务器地址

《数据结构编程实验》 2.4.4Calendar个人见解,求指导

题目大意: 制作一个日历系统,输入年份,一些周年纪念日,及服务要求日期,根据要求日期输出,输出重要程度小于发生日期的周年纪念日. 题目地址: UVA  145 个人见解: 纯模拟,在闰年,输出顺序及输出范围可能跨年上有坑.解决了这些后,依旧困了我十多天,一直没过,求指导. 我的程序里先读入周年纪念日,l代表周年纪念日的数量,每读入一个服务日期,调用f()函数,f()函数里先计算每日期相对于当年月号的日期,然后Is()函数判断是否要输出并将要输出移至数组前方,k代表要输出的周年纪念日的数量,最后按

求指导

文件 ip.txt 192.168.0.1 zhangxc192.168.0.1 zhangxc1192.168.0.1 zhangxc3192.168.0.2 zhangc192.168.0.2 zhangc1192.168.0.3 zhangc192.168.0.3 zhangc192.168.0.3 zhangxc192.168.0.3 zhangxc192.168.0.0 zhang192.168.0.5 zhang192.168.0.0 zhang192.168.0.0 zhang19

Redis 在 SNS 类应用中的最佳实践有哪些?

1. 消息队列(通知类.延迟更新类)2. 热点数据的实时缓存(比如feed,数据库.缓存同时写)3. 热点列表数据缓存(首页.热门话题等)4. counter(计数器,大多是用缓存实现的)5. 记日志最好不要用redis,用mongodb比较适合.Redis 在 SNS 类应用中的最佳实践有哪些?,码迷,mamicode.com

android content provider 中的URL解析总是出问题?求指导!!!

java.lang.IllegalArgumentException: Unknown URL content:// 不管是自己写或者用别的的代码在我的eclipse中都是报这个错误 很怪,我的URL地址绝对没有写错,是不是和使用版本有关系?我的google提供的eclipse,sdk用的2.3.3 android content provider 中的URL解析总是出问题?求指导!!!

封装php redis缓存操作类

封装php redis缓存操作类,集成了连接redis并判断连接是否成功,redis数据库选择,检测redis键是否存在,获取值,写入值,设置生存时间和删除清空操作. php redis类代码: <?php/*** redisdrive.class.php* php redis 操作类**/class redisdrive{ //键名 public $key; //值 public $value; //默认生存时间 public $expire = 86400; /*60*60*24*/ //连

Redis 操作帮助类

首先从Nuget中添加StackExchange.Redis包 1.Redis连接对象管理帮助类 using Mvc.Base; using Mvc.Base.Log; using StackExchange.Redis; using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Linq; using System.Text; namespace Redis

php redis数据库操作类

<?php namespace iphp\db; use iphp\App; /** * redis操作类 * 说明,任何为false的串,存在redis中都是空串. * 只有在key不存在时,才会返回false. * 这点可用于防止缓存穿透 * @author xuen * */ class Redis { private $redis; //当前数据库ID号 protected $dbId=0; //当前权限认证码 protected $auth; /** * 实例化的对象,单例模式. *

一个redis使用工具类

package com.cheng.common.util.cache; import java.util.ArrayList; import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; import java.util.concurrent.TimeUnit; import javax.annotation.Pos