Java Redis 串連池 Jedis 工具類

來源:互聯網
上載者:User

標籤:oid   pos   hal   new   建立   jedis   hash表   class   bsp   

import org.slf4j.Logger;import org.slf4j.LoggerFactory;import redis.clients.jedis.Jedis;import redis.clients.jedis.JedisPool;import redis.clients.jedis.JedisPoolConfig;import java.io.IOException;import java.io.InputStream;import java.util.Properties;public class MyJedisPool {    private final static Logger logger = LoggerFactory.getLogger(MyJedisPool.class);    private static JedisPool readPool = null;    private static JedisPool writePool = null;    //靜態代碼初始化池配置    static {        try{            Properties props = new Properties();            InputStream in = MyJedisPool.class.getResourceAsStream("/redis.properties");            props.load(in);            //建立jedis池配置執行個體            JedisPoolConfig config = new JedisPoolConfig();            //設定池配置項值            config.setMaxTotal(Integer.valueOf(props.getProperty("jedis.pool.maxActive")));            config.setMaxIdle(Integer.valueOf(props.getProperty("jedis.pool.maxIdle")));            config.setMaxWaitMillis(Long.valueOf(props.getProperty("jedis.pool.maxWait")));            config.setTestOnBorrow(Boolean.valueOf(props.getProperty("jedis.pool.testOnBorrow")));            config.setTestOnReturn(Boolean.valueOf(props.getProperty("jedis.pool.testOnReturn")));            //根據配置執行個體化jedis池            readPool = new JedisPool(config, props.getProperty("redisReadURL"), Integer.valueOf(props.getProperty("redisReadPort")));            writePool = new JedisPool(config, props.getProperty("redisWriteURL"), Integer.valueOf(props.getProperty("redisWritePort")));        }catch (IOException e) {            logger.info("redis串連池異常",e);        }    }    /**獲得jedis對象*/    public static Jedis getReadJedisObject(){        return readPool.getResource();    }    /**獲得jedis對象*/    public static Jedis getWriteJedisObject(){        return writePool.getResource();    }    /**歸還jedis對象*/    public static void returnJedisOjbect(Jedis jedis){        if (jedis != null) {            jedis.close();        }    }}

  

import redis.clients.jedis.Jedis;import java.util.Set;public class RedisUtils {    /**     * 擷取hash表中所有key     * @param name     * @return     */    public static Set<String> getHashAllKey(String name){        Jedis jedis = null;        try {            jedis = MyJedisPool.getReadJedisObject();            return jedis.hkeys(name);        }catch (Exception e){            e.printStackTrace();        }finally {            MyJedisPool.returnJedisOjbect(jedis);        }        return null;    }    /**     * 從redis hash表中擷取     * @param hashName     * @param key     * @return     */    public static String getHashKV(String hashName,String key){        Jedis jedis = null;        try {            jedis = MyJedisPool.getReadJedisObject();            return jedis.hget(hashName, key);        }catch (Exception e){            e.printStackTrace();        }finally {            MyJedisPool.returnJedisOjbect(jedis);        }        return null;    }    /**     * 刪除hash表的索引值對     * @param hashName     * @param key     */    public static Long delHashKV(String hashName,String key){        Jedis jedis = null;        try {            jedis = MyJedisPool.getWriteJedisObject();            return jedis.hdel(hashName,key);        }catch (Exception e){            e.printStackTrace();        }finally {            MyJedisPool.returnJedisOjbect(jedis);        }        return null;    }    /**     * 存放hash表索引值對     * @param hashName     * @param key     * @param value     */    public static Long setHashKV(String hashName,String key,String value){        Jedis jedis = null;        try {            jedis = MyJedisPool.getWriteJedisObject();            return jedis.hset(hashName,key,value);        }catch (Exception e){            e.printStackTrace();        }finally {            MyJedisPool.returnJedisOjbect(jedis);        }        return null;    }    /**     * 刪除索引值對     * @param k     * @return     */    public static Long delKV(String k){        Jedis jedis = null;        try {            jedis = MyJedisPool.getWriteJedisObject();            return jedis.del(k);        }catch (Exception e){            e.printStackTrace();        }finally {            MyJedisPool.returnJedisOjbect(jedis);        }        return null;    }    /**     * 放索引值對     * 永久     * @param k     * @param v     */    public static String setKV(String k, String v)    {        Jedis jedis = null;        try {            jedis = MyJedisPool.getWriteJedisObject();            return jedis.set(k, v);        }catch (Exception e){            e.printStackTrace();        }finally {            MyJedisPool.returnJedisOjbect(jedis);        }        return null;    }    /**     * 放索引值對     *     * @param k     * @param v     */    public static String setKV(String k,int second, String v)    {        Jedis jedis = null;        try {            jedis = MyJedisPool.getWriteJedisObject();            return jedis.setex(k,second, v);        }catch (Exception e){            e.printStackTrace();        }finally {            MyJedisPool.returnJedisOjbect(jedis);        }        return null;    }    /**     * 根據key取value     *     * @param k     * @return     */    public static String getKV(String k)    {        Jedis jedis = null;        try {            jedis = MyJedisPool.getReadJedisObject();            return jedis.get(k);        }catch (Exception e){            e.printStackTrace();        }finally {            MyJedisPool.returnJedisOjbect(jedis);        }        return null;    }}

  

 

Java Redis 串連池 Jedis 工具類

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.