CacheKeyBuilder.java 1.44 KB
package com.yohoufo.msg.cache;

import com.yoho.core.redis.cluster.operations.serializer.RedisKeyBuilder;
import org.slf4j.helpers.MessageFormatter;

/**
 * Created by chenchao on 2018/10/16.
 */
public class CacheKeyBuilder {
    public enum KeyTemp{
        BASE_NOTICE_CASE("ufo:msg:notice:case:","metaKey:{}"),

        LOWER_PRICE_NOTICE_SELLER("ufo:msg:lowerPrice:seller:","uid:{}:storageId:{}case:{}:method:{}:dtp{}:num");


        private String fix;

        private String placeHolder;

        KeyTemp(String fix, String placeHolder) {
            this.fix = fix;
            this.placeHolder = placeHolder;
        }


        public String fillPlaceHolder(Object[] args){
            if (placeHolder == null){
                return null;
            }
            return MessageFormatter.arrayFormat(placeHolder, args).getMessage();
        }

        public RedisKeyBuilder builderKey(Object[] args){
            String appendPart = fillPlaceHolder(args);
            return new RedisKeyBuilder().appendFixed(fix).appendVar(appendPart);
        }
    }


    public static RedisKeyBuilder lowerPriceNoticeSellerKey(int uid, int storageId, Integer caseId, Short method, String dtp){
        return KeyTemp.LOWER_PRICE_NOTICE_SELLER.builderKey(new Object[]{uid, storageId, caseId, method, dtp});
    }

    public static RedisKeyBuilder baseNoticeCaseKey(String metaKey){
        return KeyTemp.BASE_NOTICE_CASE.builderKey(new Object[]{metaKey});
    }


}