码迷,mamicode.com
首页 > 其他好文 > 详细

Redis数据操作

时间:2019-05-26 12:32:24      阅读:93      评论:0      收藏:0      [点我收藏+]

标签:static   计数   cat   数据   oid   ash   nta   imp   sdn   

http://www.importnew.com/19321.html

https://blog.csdn.net/u013256816/article/details/51125842

另外纠正原文部分错误:

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

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: feng
 * @Date: 2019/5/26 10:26
 * @Description:
 */
public class RedisUtil {

        private RedisUtil(){}

        private static class RedisUtilHolder{
            private static final RedisUtil instance = new RedisUtil();
        }

        public static RedisUtil getInstance(){
            return RedisUtilHolder.instance;
        }

        private static Map<String,JedisPool> maps = new HashMap<String,JedisPool>();

        private static JedisPool getPool(String ip, int port){
            String key = ip+":"+port;
            JedisPool pool = null;
            if(!maps.containsKey(key))
            {
                JedisPoolConfig config = new JedisPoolConfig();
                config.setMaxTotal(RedisConfig.MAX_ACTIVE);
                config.setMaxIdle(RedisConfig.MAX_IDLE);
                config.setMaxWaitMillis(RedisConfig.MAX_WAIT);
                config.setTestOnBorrow(true);
                config.setTestOnReturn(true);

                pool = new JedisPool(config,ip,port,RedisConfig.TIMEOUT);
                maps.put(key, pool);
            }
            else
            {
                pool = maps.get(key);
            }
            return pool;
        }

        public Jedis getJedis(String ip, int port)
        {
            Jedis jedis = null;
            int count = 0;
            while(jedis == null && count<RedisConfig.RETRY_NUM){
                try
                {
                    jedis = getPool(ip,port).getResource();
                }
                catch (Exception e)
                {
                    e.printStackTrace();
                    //加上计数
                    count++;
                }
            }
            return jedis;
        }

        public void closeJedis(Jedis jedis, String ip, int port){
            if(jedis != null)
            {
                getPool(ip,port).getResource().close();
            }
        }
}

  

Redis数据操作

标签:static   计数   cat   数据   oid   ash   nta   imp   sdn   

原文地址:https://www.cnblogs.com/Andrew520/p/10925550.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!