/** * Copyright (c) 2018-2028, Chill Zhuang 庄骞 (smallchill@163.com). *
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; * you may not use this file except in compliance with the License. * You may obtain a copy of the License at *
* http://www.gnu.org/licenses/lgpl.html *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springblade.core.tool.utils;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.lang.Nullable;
import java.util.concurrent.Callable;
/**
* 缓存工具类
*
* @author Chill
*/
public class CacheUtil {
public static final String SYS_CACHE = "blade:sys";
private static CacheManager cacheManager;
/**
* 获取缓存工具
*
* @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 getCacheManager().getCache(cacheName);
}
/**
* 获取缓存
*
* @param cacheName 缓存名
* @param keyPrefix 缓存键前缀
* @param key 缓存键值
* @return Cache
*/
@Nullable
public static Object get(String cacheName, String keyPrefix, Object key) {
if (Func.hasEmpty(cacheName, keyPrefix, key)) {
return null;
}
return getCache(cacheName).get(keyPrefix.concat(String.valueOf(key))).get();
}
/**
* 获取缓存
*
* @param cacheName 缓存名
* @param keyPrefix 缓存键前缀
* @param key 缓存键值
* @param type 转换类型
* @param