Redis工具类

shiro.properties:

#shiro
password.algorithmName=md5
password.hashIterations=2

redis.host.ip=163.1.9.37
redis.host.port=6379
redis.pool.maxActive=10
redis.pool.maxIdle=2
redis.pool.maxWaitTime=100
redis.pool.testOnBorrow=true






package com.ebj.util;


import java.io.IOException;
import java.util.Properties;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
/**
 * redis工具类
 * @author
 *
 */
public class RedisUtil {
    
    private static JedisPool pool = null;
    
    /**
     * 构建redis连接池
     *  
     * @param ip
     * @param port
     * @return JedisPool
     * @throws IOException
     */  
    public static JedisPool getPool() throws IOException {  
        if (pool == null) {  
            JedisPoolConfig config = new JedisPoolConfig();  
            //控制一个pool可分配多少个jedis实例,通过pool.getResource()来获取;  
            //如果赋值为-1,则表示不限制;如果pool已经分配了maxActive个jedis实例,则此时pool的状态为exhausted(耗尽)。  
            // config.setMaxActive(500);  
            config.setMaxTotal(500);
            //控制一个pool最多有多少个状态为idle(空闲的)的jedis实例。  
            config.setMaxIdle(5);  
            //表示当borrow(引入)一个jedis实例时,最大的等待时间,如果超过等待时间,则直接抛出JedisConnectionException;  
            //config.setMaxWait(1000 * 100);  
            config.setMaxWaitMillis(1000 * 100);  
            //在borrow一个jedis实例时,是否提前进行validate操作;如果为true,则得到的jedis实例均是可用的;  
            config.setTestOnBorrow(true);  
            
            FileUtil fileUtil = new FileUtil();
            
            Properties properties = fileUtil.loadProperty("/config/shiro.properties");
            
            String redisIp = properties.getProperty("redis.host.ip");
            
            String redisPort = properties.getProperty("redis.host.port");
            
            System.out.println("redisIp=================" + redisIp + ",redisPort===============" + redisPort);
            
            pool = new JedisPool(config, redisIp, Integer.parseInt(redisPort));  
        }  
        return pool;  
    }
    
    /**
     * 返还到连接池
     *  
     * @param pool  
     * @param redis
     */  
    public static void returnResource(JedisPool pool, Jedis redis) {  
        if (redis != null) {  
            pool.returnResource(redis);  
        }  
    }
    
    /**
     * 获取数据
     *  
     * @param key
     * @return
     */  
    public static String get(String key){  
        String value = null;  
          
        JedisPool pool = null;  
        Jedis jedis = null;  
        try {  
            pool = getPool();  
            jedis = pool.getResource();  
            value = jedis.get(key);  
        } catch (Exception e) {  
            //释放redis对象  
            pool.returnBrokenResource(jedis);  
            e.printStackTrace();  
        } finally {  
            //返还到连接池  
            returnResource(pool, jedis);  
        }  
          
        return value;  
    }  
    /**
     * 删除数据
     *  
     * @param key
     * @return
     */  
    public static long del(String key){  
        long value = 0;  
          
        JedisPool pool = null;  
        Jedis jedis = null;  
        try {  
            pool = getPool();  
            jedis = pool.getResource();  
            value = jedis.del(key);  
        } catch (Exception e) {  
            //释放redis对象  
            pool.returnBrokenResource(jedis);  
            e.printStackTrace();  
        } finally {  
            //返还到连接池  
            returnResource(pool, jedis);  
        }  
          
        return value;  
    }  
    /**
     * 删除数据
     *  
     * @param key
     * @return
     */  
    public static boolean exists(String key){  
        boolean value = false;  
          
        JedisPool pool = null;  
        Jedis jedis = null;  
        try {  
            pool = getPool();  
            jedis = pool.getResource();  
            value = jedis.exists(key);  
        } catch (Exception e) {  
            //释放redis对象  
            pool.returnBrokenResource(jedis);  
            e.printStackTrace();  
        } finally {  
            //返还到连接池  
            returnResource(pool, jedis);  
        }  
          
        return value;  
    }  

}



import com.ebj.util.RedisUtil;

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

public class test{


    @RequestMapping("/list")
    public String customerList(HttpServletRequest request,Model model)  {

              JedisPool jedisPool=null;
                Jedis jedis=null;
                try{
                     jedisPool = RedisUtil.getPool();
                
                     jedis = jedisPool.getResource();
            
                    //给userCardController用
                    jedis.set("callerNo_"+userSeq, caller);
                    
                }catch(Exception e){
                    logger.info("缓存=="+e);
                    request.setAttribute("errorMsg","你所访问的页面不存在!");
                    return "common/404";
                }finally{

                      //返还到连接池 

                       RedisUtil.returnResource(jedisPool, jedis);
                }

     }

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值