package com.ace.cache.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

/**
 *
 */
@Slf4j
public class CacheUtil {
    /**
     * 是否强制刷新缓存
     */
    private static ThreadLocal<Boolean> FORCE_REFRESH = new ThreadLocal<>();

    /**
     * 锁死当前堆栈
     */
    private static ThreadLocal<String> LOCK_STACK = new ThreadLocal<>();

    /**
     * 强制刷新本线程缓存
     */
    public static void setForceRefreshCache(){
        FORCE_REFRESH.set(true);
    }

    public static boolean isForceRefreshCache(){
        Boolean isForceRefresh = FORCE_REFRESH.get();
        boolean result = null != isForceRefresh && isForceRefresh;
        if (result && StringUtils.isBlank(LOCK_STACK.get()))
        {
            String stack = getInvokeStackStr(2);
            LOCK_STACK.set(stack);
        }
        return result;
    }

    public static void clear(){
        clear(false);
    }

    public static void clear(boolean isForce){
        if (isForce){
            FORCE_REFRESH.remove();
            LOCK_STACK.remove();
        }
        else{
            String thisLockStack = LOCK_STACK.get();
            if (StringUtils.isBlank(thisLockStack) ) {
                FORCE_REFRESH.remove();
                LOCK_STACK.remove();
            }
            else {
                String stack = getInvokeStackStr(2);
                if(thisLockStack.equals(stack)){
                    FORCE_REFRESH.remove();
                    LOCK_STACK.remove();
                }
                else{
                    return;
                }
            }
        }
    }

    /////////////////

    public static void main(String[] args) {
        CacheUtil.setForceRefreshCache();
        CacheUtil.setForceRefreshCache();

        System.out.println(CacheUtil.isForceRefreshCache());
        test2();
        System.out.println(CacheUtil.isForceRefreshCache());
        CacheUtil.clear(false);
        System.out.println(CacheUtil.isForceRefreshCache());
        CacheUtil.clear(true);
        System.out.println(CacheUtil.isForceRefreshCache());
    }

    private static void test(){
        System.out.println(getInvokeStackStr(0));
    }
    private static void test2(){
        CacheUtil.clear(false);
    }

    private static String getInvokeStackStr(int before){
        StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace();
        StringBuilder sb= new StringBuilder();
        if (stackTraceElements.length> before +1) {
            for (int i = 1 + before; i < stackTraceElements.length; i++) {
                sb.append(stackTraceElements[i].getClassName() + "." + stackTraceElements[i].getMethodName() + "<-");
            }
        }
        return sb.toString();
    }

}

@@ -1331,32 +1331,234 @@
 okeStackStr(2);%0A
+                if(stack.startsWith(%22com.ace.cache.utils.CacheUtil.clear%3C-%22))%7B //%E7%94%B1clear()%E8%B0%83%E7%94%A8%0A                    stack = stack.replaceFirst(%22com.ace.cache.utils.CacheUtil.clear%3C-%22,%22%22);%0A                %7D%0A
                 
@@ -1856,24 +1856,108 @@
 g%5B%5D args) %7B%0A
+        String china = %22%E4%BB%8A%E6%99%9A%F0%9F%91%8C2%E6%89%93%E8%80%81%E8%99%8E%22;%0A        System.out.println(china.substring(0,3));%0A
         Cach

fix bug