package com.elab.cache.spring.generator;

import com.alibaba.fastjson.JSON;
import org.springframework.cache.interceptor.KeyGenerator;

import java.lang.reflect.Method;

/**
 * 默认的缓存key生成策略
 *
 * @author Liukx
 * @create 2017-11-06 13:38
 * @email liukx@elab-plus.com
 **/
public class DefaultCacheKeyGenerator implements KeyGenerator {
    // 标识前缀
    private String prefix;
    // 分隔符
    private String separator = "_";

    @Override
    public Object generate(Object target, Method method, Object... params) {
        String className = target.getClass().getSimpleName();
        String name = method.getName();
        String jsonParams = JSON.toJSONString(params);
        String cachekey = prefix + separator + className + separator + name + separator + jsonParams;
        return cachekey;
    }

    public String getSeparator() {
        return separator;
    }

    public void setSeparator(String separator) {
        this.separator = separator;
    }

    public String getPrefix() {
        return prefix;
    }

    public void setPrefix(String prefix) {
        this.prefix = prefix;
    }
}
