簡直騷操作,ThreadLocal還能當緩存用


背景說明

有朋友問我一個關於接口優化的問題,他的優化點很清晰,由於接口中調用了內部很多的 service 去組成了一個完成的業務功能。每個 service 中的邏輯都是獨立的,這樣就導致了很多查詢是重復的,看下圖你就明白了。

上層查詢傳遞下去

對於這種場景最好的就是在上層將需要的數據查詢出來,然后傳遞到下層去消費。這樣就不用重復查詢了。

如果開始寫代碼的時候是這樣做的沒問題,但很多時候,之前寫的時候都是獨立的,或者復用的老邏輯,里面就是有獨立的查詢。

如果要做優化就只能將老的方法重載一個,將需要的信息直接傳遞過去。

public void xxx(int goodsId) {
    Goods goods = goodsService.get(goodsId);
    .....
}
public void xxx(Goods goods) {
    .....
}

加緩存

如果你的業務場景允許數據有一定延遲,那么重復調用你可以直接通過加緩存來解決。這樣的好處在於不會重復查詢數據庫,而是直接從緩存中取數據。

更大的好處在於對於優化類的影響最小,原有的代碼邏輯都不用改變,只需要在查詢的方法上加注解進行緩存即可。

public void xxx(int goodsId) {
    Goods goods = goodsService.get(goodsId);
    .....
}
public void xxx(Goods goods) {
    Goods goods = goodsService.get(goodsId);
    .....
}
class GoodsService {
    @Cached(expire = 10, timeUnit = TimeUnit.SECONDS)
    public Goods get(int goodsId) {
        return dao.findById(goodsId);
    }
}

如果你的業務場景不允許有緩存的話,上面這個方法就不能用了。那么是不是還得改代碼,將需要的信息一層層往下傳遞呢?

自定義線程內的緩存

我們總結下目前的問題:

  1. 同一次請求內,多次相同的查詢獲取 RPC 等的調用。
  2. 數據實時性要求高,不適合加緩存,主要是加緩存也不好設置過期時間,除非采用數據變更主動更新緩存的方式。
  3. 只需要在這一次請求里緩存即可,不影響其他地方。
  4. 不想改動已有代碼。

總結后發現這個場景適合用 ThreadLocal 來傳遞數據,對已有代碼改動量最小,而且也只對當前線程生效,不會影響其他線程。

public void xxx(int goodsId) {
    Goods goods = ThreadLocal.get();
    if (goods == null) {
        goods = goodsService.get(goodsId);
    }
    .....
}

上面代碼就是使用了 ThreadLocal 來獲取數據,如果有的話就直接使用,不用去重新查詢,沒有的話就去查詢,不影響老邏輯。

雖然能實現效果,但是不太好,不夠優雅。也不夠通用,如果一次請求內要緩存多種類型的數據怎么處理? ThreadLocal 就不能存儲固定的類型。還有就是老的邏輯還是得改,加了個判斷。

下面介紹一種比較優雅的方式:

  1. 自定義緩存注解,加在查詢的方法上。
  2. 定義切面切到加了緩存注解的方法上,第一次獲取返回值存入 ThreadLocal。第二次直接從 ThreadLocal 中取值返回。
  3. ThreadLocal 中存儲 Map,Key 為某方法的某一標識,這樣可以緩存多種類型的結果。
  4. 在 Filter 中將 ThreadLocal 進行 remove 操作,因為線程是復用的,使用完需要清空。

注意:ThreadLocal 不能跨線程,如果有跨線程需求,請使用阿里的 ttl 來裝飾。

注解定義

@Target({ ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
public @interface ThreadLocalCache {
    /**
     * 緩存key,支持SPEL表達式
     * @return
     */
    String key() default "";
}

存儲定義

/**
 * 線程內緩存管理
 *
 * @作者 尹吉歡
 * @時間 2020-07-12 10:47
 */
public class ThreadLocalCacheManager {
    private static ThreadLocal<Map> threadLocalCache = new ThreadLocal<>();
    public static void setCache(Map value) {
        threadLocalCache.set(value);
    }
    public static Map getCache() {
        return threadLocalCache.get();
    }
    public static void removeCache() {
        threadLocalCache.remove();
    }
    public static void removeCache(String key) {
        Map cache = threadLocalCache.get();
        if (cache != null) {
            cache.remove(key);
        }
    }
}

切面定義

/**
 * 線程內緩存
 *
 * @作者 尹吉歡
 * @時間 2020-07-12 10:48
 */
@Aspect
public class ThreadLocalCacheAspect {
    @Around(value = "@annotation(localCache)")
    public Object aroundAdvice(ProceedingJoinPoint joinpoint, ThreadLocalCache localCache) throws Throwable {
        Object[] args = joinpoint.getArgs();
        Method method = ((MethodSignature) joinpoint.getSignature()).getMethod();
        String className = joinpoint.getTarget().getClass().getName();
        String methodName = method.getName();
        String key = parseKey(localCache.key(), method, args, getDefaultKey(className, methodName, args));
        Map cache = ThreadLocalCacheManager.getCache();
        if (cache == null) {
            cache = new HashMap();
        }
        Map finalCache = cache;
        Map<String, Object> data = new HashMap<>();
        data.put("methodName", className + "." + methodName);
        Object cacheResult =  CatTransactionManager.newTransaction(() -> {
            if (finalCache.containsKey(key)) {
                return finalCache.get(key);
            }
            return null;
        }, "ThreadLocalCache", "CacheGet", data);
        if (cacheResult != null) {
            return cacheResult;
        }
        return CatTransactionManager.newTransaction(() -> {
            Object result = null;
            try {
                result = joinpoint.proceed();
            } catch (Throwable throwable) {
                throw new RuntimeException(throwable);
            }
            finalCache.put(key, result);
            ThreadLocalCacheManager.setCache(finalCache);
            return result;
        }, "ThreadLocalCache", "CachePut", data);
    }
    private String getDefaultKey(String className, String methodName, Object[] args) {
        String defaultKey = className + "." + methodName;
        if (args != null) {
            defaultKey = defaultKey + "." + JsonUtils.toJson(args);
        }
        return defaultKey;
    }
    private String parseKey(String key, Method method, Object[] args, String defaultKey){
        if (!StringUtils.hasText(key)) {
            return defaultKey;
        }
        LocalVariableTableParameterNameDiscoverer nameDiscoverer = new LocalVariableTableParameterNameDiscoverer();
        String[] paraNameArr = nameDiscoverer.getParameterNames(method);
        ExpressionParser parser = new SpelExpressionParser();
        StandardEvaluationContext context = new StandardEvaluationContext();
        for(int i = 0;i < paraNameArr.length; i++){
            context.setVariable(paraNameArr[i], args[i]);
        }
        try {
            return parser.parseExpression(key).getValue(context, String.class);
        } catch (SpelEvaluationException e) {
            // 解析不出SPEL默認為類名+方法名+參數
            return defaultKey;
        }
    }
}

過濾器定義

/**
 * 線程緩存過濾器
 *
 * @作者 尹吉歡
 * @個人微信 jihuan900
 * @微信公眾號 猿天地
 * @GitHub https://github.com/yinjihuan
 * @作者介紹 http://cxytiandi.com/about
 * @時間 2020-07-12 19:46
 */
@Slf4j
public class ThreadLocalCacheFilter implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        filterChain.doFilter(servletRequest, servletResponse);
        // 執行完后清除緩存
        ThreadLocalCacheManager.removeCache();
    }
}

自動配置類

@Configuration
public class ThreadLocalCacheAutoConfiguration {
    @Bean
    public FilterRegistrationBean idempotentParamtFilter() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        ThreadLocalCacheFilter filter = new ThreadLocalCacheFilter();
        registration.setFilter(filter);
        registration.addUrlPatterns("/*");
        registration.setName("thread-local-cache-filter");
        registration.setOrder(1);
        return registration;
    }
    @Bean
    public ThreadLocalCacheAspect threadLocalCacheAspect() {
        return new ThreadLocalCacheAspect();
    }
}

使用案例

@Service
public class TestService {
    /**
     * ThreadLocalCache 會緩存,只對當前線程有效
     * @return
     */
    @ThreadLocalCache
    public String getName() {
        System.out.println("開始查詢了");
        return "yinjihaun";
    }
    /**
     * 支持SPEL表達式
     * @param id
     * @return
     */
    @ThreadLocalCache(key = "#id")
    public String getName(String id) {
        System.out.println("開始查詢了");
        return "yinjihaun" + id;
    }
}

功能代碼: https://github.com/yinjihuan/kitty

案例代碼: https://github.com/yinjihuan/kitty-samples

關於作者 :尹吉歡,簡單的技術愛好者,《Spring Cloud 微服務-全棧技術與案例解析》, 《Spring Cloud 微服務 入門 實戰與進階》作者, 公眾號 猿天地 發起人。個人微信 jihuan900 ,歡迎勾搭。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM