From 80b6cbfc9c861469146318d0b3dd5f8b8b525b8a Mon Sep 17 00:00:00 2001 From: xiejun <xiejun@vci-tech.com> Date: 星期五, 01 十一月 2024 15:11:19 +0800 Subject: [PATCH] Revert "集成获取mdm分发通用数据格式接口集成" --- Source/BladeX-Tool/blade-starter-cache/src/main/java/org/springblade/core/cache/utils/CacheUtil.java | 331 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 331 insertions(+), 0 deletions(-) diff --git a/Source/BladeX-Tool/blade-starter-cache/src/main/java/org/springblade/core/cache/utils/CacheUtil.java b/Source/BladeX-Tool/blade-starter-cache/src/main/java/org/springblade/core/cache/utils/CacheUtil.java new file mode 100644 index 0000000..9bd3dbb --- /dev/null +++ b/Source/BladeX-Tool/blade-starter-cache/src/main/java/org/springblade/core/cache/utils/CacheUtil.java @@ -0,0 +1,331 @@ +/* + * Copyright (c) 2018-2028, Chill Zhuang All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * Neither the name of the dreamlu.net developer nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * Author: Chill 搴勯獮 (smallchill@163.com) + */ +package org.springblade.core.cache.utils; + +import org.springblade.core.secure.utils.AuthUtil; +import org.springblade.core.tool.constant.BladeConstant; +import org.springblade.core.tool.utils.*; +import org.springframework.cache.Cache; +import org.springframework.cache.CacheManager; +import org.springframework.lang.Nullable; + +import java.lang.reflect.Field; +import java.util.List; +import java.util.concurrent.Callable; + +/** + * 缂撳瓨宸ュ叿绫� + * + * @author Chill + */ +public class CacheUtil { + + private static CacheManager cacheManager; + + private static final Boolean TENANT_MODE = Boolean.TRUE; + + /** + * 鑾峰彇缂撳瓨宸ュ叿 + * + * @return CacheManager + */ + private static CacheManager getCacheManager() { + if (cacheManager == null) { + cacheManager = SpringUtil.getBean(CacheManager.class); + } + return cacheManager; + } + + /** + * 鑾峰彇缂撳瓨瀵硅薄 + * + * @param cacheName 缂撳瓨鍚� + * @return Cache + */ + public static Cache getCache(String cacheName) { + return getCache(cacheName, TENANT_MODE); + } + + /** + * 鑾峰彇缂撳瓨瀵硅薄 + * + * @param cacheName 缂撳瓨鍚� + * @param tenantMode 绉熸埛妯″紡 + * @return Cache + */ + public static Cache getCache(String cacheName, Boolean tenantMode) { + return getCacheManager().getCache(formatCacheName(cacheName, tenantMode)); + } + + /** + * 鑾峰彇缂撳瓨瀵硅薄 + * + * @param cacheName 缂撳瓨鍚� + * @param tenantId 绉熸埛ID + * @return Cache + */ + public static Cache getCache(String cacheName, String tenantId) { + return getCacheManager().getCache(formatCacheName(cacheName, tenantId)); + } + + /** + * 鏍规嵁绉熸埛淇℃伅鏍煎紡鍖栫紦瀛樺悕 + * + * @param cacheName 缂撳瓨鍚� + * @param tenantMode 绉熸埛妯″紡 + * @return String + */ + public static String formatCacheName(String cacheName, Boolean tenantMode) { + if (!tenantMode) { + return cacheName; + } + return formatCacheName(cacheName, AuthUtil.getTenantId()); + } + + /** + * 鏍规嵁绉熸埛淇℃伅鏍煎紡鍖栫紦瀛樺悕 + * + * @param cacheName 缂撳瓨鍚� + * @param tenantId 绉熸埛ID + * @return String + */ + public static String formatCacheName(String cacheName, String tenantId) { + return (StringUtil.isBlank(tenantId) ? cacheName : tenantId.concat(StringPool.COLON).concat(cacheName)); + } + + /** + * 鑾峰彇缂撳瓨 + * + * @param cacheName 缂撳瓨鍚� + * @param keyPrefix 缂撳瓨閿墠缂� + * @param key 缂撳瓨閿�� + * @return Object + */ + @Nullable + public static Object get(String cacheName, String keyPrefix, Object key) { + return get(cacheName, keyPrefix, key, TENANT_MODE); + } + + /** + * 鑾峰彇缂撳瓨 + * + * @param cacheName 缂撳瓨鍚� + * @param keyPrefix 缂撳瓨閿墠缂� + * @param key 缂撳瓨閿�� + * @param tenantMode 绉熸埛妯″紡 + * @return Object + */ + @Nullable + public static Object get(String cacheName, String keyPrefix, Object key, Boolean tenantMode) { + if (Func.hasEmpty(cacheName, keyPrefix, key)) { + return null; + } + Cache.ValueWrapper valueWrapper = getCache(cacheName, tenantMode).get(keyPrefix.concat(String.valueOf(key))); + if (Func.isEmpty(valueWrapper)) { + return null; + } + return valueWrapper.get(); + } + + /** + * 鑾峰彇缂撳瓨 + * + * @param cacheName 缂撳瓨鍚� + * @param keyPrefix 缂撳瓨閿墠缂� + * @param key 缂撳瓨閿�� + * @param type 绫诲瀷 + * @param <T> 娉涘瀷 + * @return T + */ + @Nullable + public static <T> T get(String cacheName, String keyPrefix, Object key, @Nullable Class<T> type) { + return get(cacheName, keyPrefix, key, type, TENANT_MODE); + } + + /** + * 鑾峰彇缂撳瓨 + * + * @param cacheName 缂撳瓨鍚� + * @param keyPrefix 缂撳瓨閿墠缂� + * @param key 缂撳瓨閿�� + * @param type 绫诲瀷 + * @param tenantMode 绉熸埛妯″紡 + * @param <T> 娉涘瀷 + * @return T + */ + @Nullable + public static <T> T get(String cacheName, String keyPrefix, Object key, @Nullable Class<T> type, Boolean tenantMode) { + if (Func.hasEmpty(cacheName, keyPrefix, key)) { + return null; + } + return getCache(cacheName, tenantMode).get(keyPrefix.concat(String.valueOf(key)), type); + } + + /** + * 鑾峰彇缂撳瓨 + * + * @param cacheName 缂撳瓨鍚� + * @param keyPrefix 缂撳瓨閿墠缂� + * @param key 缂撳瓨閿�� + * @param valueLoader 閲嶈浇瀵硅薄 + * @param <T> 娉涘瀷 + * @return T + */ + @Nullable + public static <T> T get(String cacheName, String keyPrefix, Object key, Callable<T> valueLoader) { + return get(cacheName, keyPrefix, key, valueLoader, TENANT_MODE); + } + + /** + * 鑾峰彇缂撳瓨 + * + * @param cacheName 缂撳瓨鍚� + * @param keyPrefix 缂撳瓨閿墠缂� + * @param key 缂撳瓨閿�� + * @param valueLoader 閲嶈浇瀵硅薄 + * @param tenantMode 绉熸埛妯″紡 + * @param <T> 娉涘瀷 + * @return T + */ + @Nullable + public static <T> T get(String cacheName, String keyPrefix, Object key, Callable<T> valueLoader, Boolean tenantMode) { + if (Func.hasEmpty(cacheName, keyPrefix, key)) { + return null; + } + try { + Cache.ValueWrapper valueWrapper = getCache(cacheName, tenantMode).get(keyPrefix.concat(String.valueOf(key))); + Object value = null; + if (valueWrapper == null) { + T call = valueLoader.call(); + if (ObjectUtil.isNotEmpty(call)) { + Field field = ReflectUtil.getField(call.getClass(), BladeConstant.DB_PRIMARY_KEY); + if (ObjectUtil.isNotEmpty(field) && ObjectUtil.isEmpty(ClassUtil.getMethod(call.getClass(), BladeConstant.DB_PRIMARY_KEY_METHOD).invoke(call))) { + return null; + } + getCache(cacheName, tenantMode).put(keyPrefix.concat(String.valueOf(key)), call); + value = call; + } + } else { + value = valueWrapper.get(); + } + return (T) value; + } catch (Exception ex) { + ex.printStackTrace(); + return null; + } + } + + /** + * 璁剧疆缂撳瓨 + * + * @param cacheName 缂撳瓨鍚� + * @param keyPrefix 缂撳瓨閿墠缂� + * @param key 缂撳瓨閿�� + * @param value 缂撳瓨鍊� + */ + public static void put(String cacheName, String keyPrefix, Object key, @Nullable Object value) { + put(cacheName, keyPrefix, key, value, TENANT_MODE); + } + + /** + * 璁剧疆缂撳瓨 + * + * @param cacheName 缂撳瓨鍚� + * @param keyPrefix 缂撳瓨閿墠缂� + * @param key 缂撳瓨閿�� + * @param value 缂撳瓨鍊� + * @param tenantMode 绉熸埛妯″紡 + */ + public static void put(String cacheName, String keyPrefix, Object key, @Nullable Object value, Boolean tenantMode) { + getCache(cacheName, tenantMode).put(keyPrefix.concat(String.valueOf(key)), value); + } + + /** + * 娓呴櫎缂撳瓨 + * + * @param cacheName 缂撳瓨鍚� + * @param keyPrefix 缂撳瓨閿墠缂� + * @param key 缂撳瓨閿�� + */ + public static void evict(String cacheName, String keyPrefix, Object key) { + evict(cacheName, keyPrefix, key, TENANT_MODE); + } + + /** + * 娓呴櫎缂撳瓨 + * + * @param cacheName 缂撳瓨鍚� + * @param keyPrefix 缂撳瓨閿墠缂� + * @param key 缂撳瓨閿�� + * @param tenantMode 绉熸埛妯″紡 + */ + public static void evict(String cacheName, String keyPrefix, Object key, Boolean tenantMode) { + if (Func.hasEmpty(cacheName, keyPrefix, key)) { + return; + } + getCache(cacheName, tenantMode).evict(keyPrefix.concat(String.valueOf(key))); + } + + /** + * 娓呯┖缂撳瓨 + * + * @param cacheName 缂撳瓨鍚� + */ + public static void clear(String cacheName) { + clear(cacheName, TENANT_MODE); + } + + /** + * 娓呯┖缂撳瓨 + * + * @param cacheName 缂撳瓨鍚� + * @param tenantMode 绉熸埛妯″紡 + */ + public static void clear(String cacheName, Boolean tenantMode) { + if (Func.isEmpty(cacheName)) { + return; + } + getCache(cacheName, tenantMode).clear(); + } + + /** + * 娓呯┖缂撳瓨 + * + * @param cacheName 缂撳瓨鍚� + * @param tenantId 绉熸埛ID + */ + public static void clear(String cacheName, String tenantId) { + if (Func.isEmpty(cacheName)) { + return; + } + getCache(cacheName, tenantId).clear(); + } + + /** + * 娓呯┖缂撳瓨 + * + * @param cacheName 缂撳瓨鍚� + * @param tenantIds 绉熸埛ID闆嗗悎 + */ + public static void clear(String cacheName, List<String> tenantIds) { + if (Func.isEmpty(cacheName)) { + return; + } + tenantIds.forEach(tenantId -> getCache(cacheName, tenantId).clear()); + } + +} -- Gitblit v1.9.3