Redis常用方法


首先構建非切片連接池jedisPool對象,寫好配置redis連接的方法。

/**
     * 構建redis切片連接池
     * 
     * @param ip
     * @param port
     * @return JedisPool
     */
    public static JedisPool getJedisPool() {
        if (jedisPool == null) {
            synchronized (lock) {
                //redis服務器對應的IP和端口
                String redisIp = PropertiesUtils.getProperties("REDIS_SERVER_IP");
                Integer redisPort = Integer.valueOf(PropertiesUtils.getProperties("REDIS_SERVER_PORT"));
                if (jedisPool == null) {
                    JedisPoolConfig config = new JedisPoolConfig();
                    //設置連接池初始化大小和最大容量
                    
                    // 控制一個pool可分配多少個jedis實例,通過pool.getResource()來獲取;
                    // 如果賦值為-1,則表示不限制;如果pool已經分配了maxActive個jedis實例,則此時pool的狀態為exhausted(耗盡)。
                    config.setMaxTotal(-1);
                    // 控制一個pool最多有多少個狀態為idle(空閑的)的jedis實例。
                    config.setMaxIdle(1000);
                    // 表示當borrow(引入)一個jedis實例時,最大的等待時間,如果超過等待時間,則直接拋出JedisConnectionException;
                    config.setMaxWaitMillis(1000 * 30);
                    // 在borrow一個jedis實例時,是否提前進行validate操作;如果為true,則得到的jedis實例均是可用的;
                    config.setTestOnBorrow(true);
                    //
                    jedisPool = new JedisPool(config, redisIp, redisPort,DEFAULT_TIME_OUT);
                    
                }
            }
        }
        return jedisPool;
    }

我們都知道redis是key,value型就當它是內存數據庫把,雖然一般常用於數據緩存,畢竟你往內存中放幾千萬條數據會弄爆- -(雖然我就是要這么干)  下來,根據key獲取value

 
    /**
     * 獲取數據
     * 
     * @param key
     * @return
     */
    public static String getForString(String key){
        List<String> values = mgetForString(key);
        if(values == null) {
            return null;
        } else {
            return values.get(0);
        }
    }

也可根據key獲取value的集合

 /**
     * 獲取數據
     * 
     * @param key
     * @return
     */
    public static List<String> mgetForString(String... key){
        List<String> value = null;
        JedisPool pool = null;
        Jedis jedis = null;
        try {
            pool = getJedisPool();
            jedis = pool.getResource();
            value = jedis.mget(key);
        } catch (Exception e) {
            log.error(e);
        } finally {
            //返還到連接池
            returnJedisResource(jedis);
        }
        return value;
    }

將數據加載到redis中的方法 一般是用set. 如下列方法,這里指定value是String類型,也是因為我的業務關系把value轉成了json串~

public static void setForString(String key,String value){
           JedisPool pool = null;
        Jedis jedis = null;
       try {
           pool = getJedisPool();
           jedis = pool.getResource();
           jedis.set(key, value);
       } catch (Exception e) {
           log.error(e);
       } finally {
           //返還到連接池
             returnJedisResource(jedis);
       }
       
   }

也可獲取哈希結構的字段和值

/**
     * 設置哈希結構的字段和值
     * @param key
     * @param value
     */
    public static void setForHashObj(String key, Map<String, String> value) {
        JedisPool pool = null;
        Jedis jedis = null;
        try {
            pool = getJedisPool();
            jedis = pool.getResource();
            jedis.hmset(key, value);
        } catch (Exception e) {
            log.error(e);
        } finally {
            // 返還到連接池
            returnJedisResource(jedis);
        }
    }

這里的Map也可以改為List<Map<String, String>> values,其實一樣的~然后再遍歷這個Map即可~


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM