查詢Redis緩衝的簡單用法

來源:互聯網
上載者:User

1,介面

/**
     * 優先從緩衝中查詢會員資訊
     * @param custNum
     * @return
     */
    public Member findMemberByCustNumFristByRedis(String custNum);


2.實作類別

 @Override
    public Member findMemberByCustNumFristByRedis(String custNum) {
        if (StringUtil.isEmpty(custNum)) {// 當傳入為空白時直接返回為空白,會員編號必須存在
            return null;
        }
        Member member = (Member)ShardedJedisUtil.getObj(RedisConst.QUERY_MEMBER_BY_CUSTNUM + custNum);
        if (member == null) {
            Map<String, Object> parameter = new HashMap<String, Object>();
            parameter.put("custNum", custNum);
            List<Member> members = this.findList(parameter, null);
            if (null != members && !members.isEmpty()) {
                ShardedJedisUtil.setObjEx(RedisConst.QUERY_MEMBER_BY_CUSTNUM + custNum, RedisConst.ONE_HOUR_SECONDS, members.get(0));
                return members.get(0);
            }
        }
        return member;
    }



3.redis操作的工具類

public class ShardedJedisUtil {


final static Logger                     logger             = LoggerFactory.getLogger(ShardedJedisUtil.class);
    protected static ShardedJedisClientImpl shardedJedisClient = null;
    static {
        // 載入SCM平台的redis.conf配置資訊,建立用戶端對象
        logger.info("----------------載入SCM平台的redis.conf配置資訊,建立ShardedJedisClientImpl用戶端對象--------------");
        try {
            shardedJedisClient = new ShardedJedisClientImpl("redis.conf");
            logger.info("----------------建立ShardedJedisClientImpl用戶端對象結束--------------");
        } catch (Exception e) {
            logger.error("--------建立shardedJedisClient異常,請確認配置是否正確!--------------------", e);
            shardedJedisClient = null;
        }
    }



    /**
     * 設定字串類型的鍵對應的對象到redis中,使用者儲存對象
     *
     * @param key 鍵
     * @param seconds 秒 ;在redis中緩衝的時間
     * @param value 值
     * @return
     */
    public static Object setObjEx(final String key, final int seconds, final Object value) {
        if (null == shardedJedisClient) {
            return null;
        }
        return shardedJedisClient.execute(new ShardedJedisAction<Object>() {

            @Override
            public String doAction(ShardedJedis shardedJedis) {
                byte[] keyBytes = ObjectCopyUtil.objectToByte(key);
                byte[] valueBytes = ObjectCopyUtil.objectToByte(value);
                return shardedJedis.setex(keyBytes, seconds, valueBytes);
            }
        });
    }


/**
     * 根據key擷取Redis中儲存的對象
     *
     * @param key 鍵
     * @return
     */
    public static Object getObj(final String key) {
        if (null == shardedJedisClient) {
            return null;
        }
        return shardedJedisClient.execute(new ShardedJedisAction<Object>() {

            @Override
            public Object doAction(ShardedJedis shardedJedis) {
                byte[] keyBytes = ObjectCopyUtil.objectToByte(key);
                byte[] valueBytes = shardedJedis.get(keyBytes);
                return ObjectCopyUtil.byteToObject(valueBytes);
            }
        });
    }





4.相關的常量類

public class RedisConst {


/** 一分鐘的秒數 */
    public static final int     ONE_MINUTE_SECONDS                 = 60;
    
    /** 一小時的秒數 */
    public static final int     ONE_HOUR_SECONDS                 = 60 * 60;
    
    /** 四小時的秒數 */
    public static final int     FOUR_HOUR_SECONDS                 = 60 * 60 * 4;
    
    /** 一天的秒數 */
    public static final int     ONE_DAY_SECONDS                 = 60 * 60 * 24;
    
    /** 一個月的秒數 */
    public static final int     ONE_MONTH_SECONDS                 = 60 * 60 * 24 * 30;


/*** 通過會員編碼查詢會員資訊 */
    public static final String    QUERY_MEMBER_BY_CUSTNUM            = "queryMemberByCustNum";



相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.