1. 程式人生 > >查詢Redis快取的簡單用法

查詢Redis快取的簡單用法

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";