• jedisPoolUtil 单例


       JedisPoolUtil

    public class JedisPoolUtil {
        private static volatile JedisPool jedisPool = null;// 被volatile修饰的变量不会被本地线程缓存,对该变量的读写都是直接操作共享内存。
        private JedisPoolUtil() {
        }
        public static JedisPool getJedisPoolInstance() {
            if (null == jedisPool) {
                synchronized (JedisPoolUtil.class) {
                    if (null == jedisPool) {
                        JedisPoolConfig poolConfig = new JedisPoolConfig();
                        poolConfig.setMaxTotal(1000);
                        poolConfig.setMaxIdle(32);
                        poolConfig.setMaxWaitMillis(100 * 1000);
                        poolConfig.setTestOnBorrow(true);
                        jedisPool = new JedisPool(poolConfig, "192.168.120.129", 6379);
                    }
                }
            }
            return jedisPool;
        }
        public static void release(JedisPool jedisPool, Jedis jedis) {
            if (null != jedis) {
                Jedis jedis2 = null;
                try {
                    jedis2 = jedisPool.getResource();
                } finally {
                    jedis2.close();
                }
            }
        }
    }

    使用

    public class TestJedisPool {
        public static void main(String[] args) {
            JedisPool jedisPool = JedisPoolUtil.getJedisPoolInstance();
            Jedis jedis = null;
            try {
                jedis = jedisPool.getResource();
                jedis.set("k18", "v183");
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                JedisPoolUtil.release(jedisPool, jedis);
            }
        }
    }
  • 相关阅读:
    分享5个viewport相关的jQuery插件
    超棒的响应式jQuery网格布局插件 grida licious
    6款不容错过的超棒倒计时jQuery插件
    分享45套2011年和2012年的高质量免费网站模板
    分享11个使用方便的免费智能手机UI套件
    推荐30款超精致的体育类型的网站设计
    HDOJ1001
    HDOJ1003
    HDOJ1000
    HDOJ1002
  • 原文地址:https://www.cnblogs.com/sdgtxuyong/p/14708650.html
Copyright © 2020-2023  润新知