All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.zcj.ext.redis.RedisKeysBuilder Maven / Gradle / Ivy

package com.zcj.ext.redis;

import com.zcj.util.UtilString;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class RedisKeysBuilder {

    private static final Logger logger = LoggerFactory.getLogger(RedisKeysBuilder.class);

    private String keyPrefix;// 前缀
    private String keyMethodName;// 类名_方法名
    private List keyParams;// 方法参数
    private Set keyChangeKeys;// 会引起缓存数据变化的关键字

    private RedisKeysBuilder() {
    }

    public static RedisKeysBuilder initKeyCreator(final String keyPrefix) {
        String className = Thread.currentThread().getStackTrace()[2].getClassName().replaceAll("\\.", "_");
        String methodName = Thread.currentThread().getStackTrace()[2].getMethodName();
        RedisKeysBuilder result = new RedisKeysBuilder();
        result.keyPrefix = keyPrefix;
        result.keyMethodName = className + "_" + methodName;
        return result;
    }

    public RedisKeysBuilder addParams(List params) {
        if (this.keyParams == null) {
            this.keyParams = params;
        } else {
            this.keyParams.addAll(params);
        }
        return this;
    }

    public RedisKeysBuilder addParams(Object... params) {
        if (this.keyParams == null) {
            this.keyParams = new ArrayList<>();
        }
        if (params != null && params.length > 0) {
            for (Object p : params) {
                this.keyParams.add(p);
            }
        }
        return this;
    }

    public RedisKeysBuilder addParam(Object param) {
        if (this.keyParams == null) {
            this.keyParams = new ArrayList<>();
        }
        this.keyParams.add(param);
        return this;
    }

    public RedisKeysBuilder addChangeKey(String changeKey) {
        if (this.keyChangeKeys == null) {
            this.keyChangeKeys = new HashSet<>();
        }
        if (UtilString.isNotBlank(changeKey)) {
            this.keyChangeKeys.add(changeKey);
        }
        return this;
    }

    public RedisKeysBuilder addChangeKeys(Set changeKeys) {
        if (this.keyChangeKeys == null) {
            this.keyChangeKeys = new HashSet<>();
        }
        if (changeKeys != null && !changeKeys.isEmpty()) {
            for (String k : changeKeys) {
                if (UtilString.isNotBlank(k)) {
                    this.keyChangeKeys.add(k);
                }
            }
        }
        return this;
    }

    public RedisKeysBuilder addChangeKeys(String... changeKeys) {
        if (this.keyChangeKeys == null) {
            this.keyChangeKeys = new HashSet<>();
        }
        if (changeKeys != null && changeKeys.length > 0) {
            for (String k : changeKeys) {
                if (UtilString.isNotBlank(k)) {
                    this.keyChangeKeys.add(k);
                }
            }
        }
        return this;
    }

    public String create() {
        if (UtilString.isBlank(keyPrefix) || UtilString.isBlank(keyMethodName)) {
            return null;
        }
        StringBuilder sb = new StringBuilder();
        sb.append(keyPrefix + ":");
        sb.append(keyMethodName + ":");
        if (keyParams != null && !keyParams.isEmpty()) {
            for (Object p : keyParams) {
                sb.append(p + ":");
            }
        }
        if (keyChangeKeys != null && !keyChangeKeys.isEmpty()) {
            for (String k : keyChangeKeys) {
                sb.append(k + ":");
            }
        }
        // score:com_thanone_score_service_base_SchoolServiceImpl_findSchoolById:376929142031360:@base_school@:
        logger.debug(sb.toString());
        return sb.toString();
    }

    public static RedisKeysBuilder initKeyMatcher(final String keyPrefix) {
        RedisKeysBuilder result = new RedisKeysBuilder();
        result.keyPrefix = keyPrefix;
        return result;
    }

    public Set patterns() {
        Set result = new HashSet<>();
        if (UtilString.isNotBlank(keyPrefix) && keyChangeKeys != null && !keyChangeKeys.isEmpty()) {
            for (String k : keyChangeKeys) {
                if (UtilString.isNotBlank(k)) {
                    StringBuilder sb = new StringBuilder();
                    sb.append(keyPrefix + ":");
                    sb.append("*:" + k + ":*");
                    logger.debug(sb.toString());
                    result.add(sb.toString());
                }
            }
        }
        return result;
    }

}