zoukankan      html  css  js  c++  java
  • Redis在JAVA中的运用(工具类)

    最近项目需要用redis在中间做缓存所以写了一个工具类作为练习用

    redis版本:redis_version:3.0.504

    用到阿里的解析JSON的库:fastjson

    import org.apache.log4j.Logger;
    import org.apache.log4j.chainsaw.Main;
    import com.alibaba.fastjson.JSONObject;
    import redis.clients.jedis.Jedis;
    import redis.clients.jedis.JedisPool;
    import redis.clients.jedis.JedisPoolConfig;
    
    /**
     * @Description:redis工具类
     * @ClassName:
     * @date 2016年10月31日 上午11:25:06
     */
    @SuppressWarnings("unused")
    public class RedisUtils
    {
        private static final String IP = "127.0.0.1"; // ip
        private static final int PORT = 6379;         // 端口
        private static final String AUTH="";          // 密码(原始默认是没有密码)
        private static int   MAX_ACTIVE = 1024;       // 最大连接数
        private static int   MAX_IDLE = 200;          // 设置最大空闲数
        private static int   MAX_WAIT = 10000;        // 最大连接时间
        private static int   TIMEOUT = 10000;         // 超时时间
        private static boolean BORROW = true;         // 在borrow一个事例时是否提前进行validate操作
        private static JedisPool pool = null;
        private static Logger logger = Logger.getLogger(RedisUtils.class);
        /**
         * 初始化线程池
         */
        static
        {
            JedisPoolConfig config = new JedisPoolConfig();
            config.setMaxTotal(MAX_ACTIVE);
            config.setMaxIdle(MAX_IDLE);
            config.setMaxWaitMillis(MAX_WAIT);
            config.setTestOnBorrow(BORROW);
            pool = new JedisPool(config, IP, PORT, TIMEOUT);
        }
        
        
        /**
         * 获取连接
         */
        public static synchronized  Jedis getJedis()
        {
            try
            {
                if(pool != null)
                {
                    return pool.getResource();
                }
                else
                {
                    return null;
                }
            }
            catch (Exception e) {
                logger.info("连接池连接异常");
                return null;
            }
           
        }
        /**
         * @Description:设置失效时间
         * @param @param key
         * @param @param seconds
         * @param @return  
         * @return boolean 返回类型
         */
        public static void disableTime(String key,int seconds)
        {
            Jedis jedis = null;
            try
            {
                jedis = getJedis();
                jedis.expire(key, seconds);
                
            }
            catch (Exception e) {
                logger.debug("设置失效失败.");
            }finally {
                getColse(jedis);
            }
        }
        
        
        /**
         * @Description:插入对象
         * @param @param key
         * @param @param obj
         * @param @return  
         * @return boolean 返回类型
         */
        public static boolean addObject(String key,Object obj)
        {
            
            Jedis jedis = null;
            String value = JSONObject.toJSONString(obj);
            try
            {
                jedis = getJedis();
                String code = jedis.set(key, value);
                if(code.equals("ok"))
                {
                    return true;
                }
            }
            catch (Exception e) {
                logger.debug("插入数据有异常.");
                return false;
            }finally {
                getColse(jedis);
            }
            return false;
        }
        
        
        
        /**
         * @Description:存储key~value
         * @param @param key
         * @param @param value  
         * @return void 返回类型
         */
        
        public static boolean addValue(String key,String value)
        {
            Jedis jedis = null;
            try
            {
                jedis = getJedis();
                String code = jedis.set(key, value);
                if(code.equals("ok"))
                {
                    return true;
                }
            }
            catch (Exception e) {
                logger.debug("插入数据有异常.");
                return false;
            }finally {
                getColse(jedis);
            }
            return false;
        }
        /**
         * @Description:删除key
         * @param @param key
         * @param @return  
         * @return boolean 返回类型
         */
        public static boolean delKey(String key)
        {
            Jedis jedis = null;
            try
            {
                jedis = getJedis();
                Long code = jedis.del(key);
                if(code > 1)
                {
                    return true;
                }
            }
            catch (Exception e) {
                logger.debug("删除key异常.");
                return false;
            }finally {
                getColse(jedis);
            }
            return false;
        }
        
        /**
         * @Description: 关闭连接
         * @param @param jedis  
         * @return void 返回类型
         */
        
        public static void getColse(Jedis jedis)
        {
            if(jedis != null)
            {
                jedis.close();
            }
        }
        
    }
    

      

  • 相关阅读:
    PyQt5基础应用一
    常用SQL
    常用vim命令
    原生Ajax XMLHttpRequest对象
    跨域两种解决方案CORS以及JSONP
    Django生命周期 URL ----> CBV 源码解析-------------- 及rest_framework APIView 源码流程解析
    Django缓存机制--rest_framework中节流源码使用的就是django提供的缓存api
    Django2.0 models中的on_delete参数
    【开发工具IDE】Eclipse 安装 Maven 的 m2eclipse 插件
    【Java】CSVUtils
  • 原文地址:https://www.cnblogs.com/byteworld/p/6015923.html
Copyright © 2011-2022 走看看