CacheEnum.java 2.4 KB
package com.yohoufo.user.constants;

import org.apache.commons.lang3.StringUtils;

import com.yoho.core.config.ConfigReaderUtil;
import com.yoho.core.redis.cluster.operations.serializer.RedisKeyBuilder;

/**
 * Created by shengguo.cai on 2018/9/18.
 */
public enum CacheEnum {
    /**
     * 消息盒子-某类型下未读消息总数,缓存5分钟
     */
    USERS_INBOX_TYPE_UNREADCOUNT("yh:users:inbox_type_unreadcount", 5 * 60, "users.cachetime.inbox_type_unreadcount"),

    /**
     * 消息盒子-消息,缓存5分钟
     */
//    USERS_INBOX_LIST("yh:users:inbox_list", 5 * 60, "users.cachetime.inbox_list"),
    
    /**
     * 消息盒子-消息,缓存5分钟
     */
    USERS_INBOX_VO_LIST("yh:users:inbox_vo_list", 5 * 60, "users.cachetime.inbox_list"),

    /**
     * 消息盒子-消息总数,缓存5分钟
     */
    USERS_INBOX_LIST_TOTAL("yh:users:inbox_list_total", 5 * 60, "users.cachetime.inbox_list_total"),
    /*
     * end
     */;

    private static final long DEFAULT_BACKUP_CACHE_TIME = 12 * 60 * 60;

    private final String keyPrefix;
    private final long cacheTime;
    private String cacheTimeKey;

    CacheEnum(String keyPrefix, long cacheTime) {
        this.keyPrefix = keyPrefix;
        this.cacheTime = cacheTime;
    }

    CacheEnum(String keyPrefix, long cacheTime, String cacheTimeKey) {
        this.keyPrefix = keyPrefix;
        this.cacheTime = cacheTime;
        this.cacheTimeKey = cacheTimeKey;
    }

    public RedisKeyBuilder generateKey(Object... params) {
        if (isEmptyArray(params)) {
            return new RedisKeyBuilder().appendFixed(keyPrefix);
        } else {
            return new RedisKeyBuilder().appendFixed(keyPrefix).appendFixed(':').appendVar(StringUtils.join(params, '-'));
        }
    }

    public RedisKeyBuilder generateKeyLowerCase(Object... params) {
        if (isEmptyArray(params)) {
            return new RedisKeyBuilder().appendFixed(keyPrefix);
        } else {
            return new RedisKeyBuilder().appendFixed(keyPrefix).appendFixed(':').appendVar(StringUtils.join(params, '-').toLowerCase());
        }
    }


    private boolean isEmptyArray(Object... params) {
        return params == null || params.length == 0;
    }

    public long getCacheTime() {
        if (StringUtils.isEmpty(cacheTimeKey)) {
            return cacheTime;
        } else {
            return ConfigReaderUtil.getLong(cacheTimeKey, cacheTime);
        }
    }
}