org.nervousync.cache.CacheUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of cache-api-jdk11 Show documentation
Show all versions of cache-api-jdk11 Show documentation
Cache API Package, development by Nervousync Studio (NSYC)
The newest version!
/*
* Licensed to the Nervousync Studio (NSYC) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.nervousync.cache;
import org.nervousync.cache.api.CacheClient;
import org.nervousync.cache.api.CacheManager;
import org.nervousync.cache.config.CacheConfig;
import org.nervousync.cache.exceptions.CacheException;
import org.nervousync.utils.LoggerUtils;
import java.util.ServiceLoader;
/**
* Cache utilities instance
* 缓存工具类
*
* @author Steven Wee [email protected]
* @version $Revision: 1.0 $ $Date: 2022-11-18 17:21 $
*/
public final class CacheUtils {
private final LoggerUtils.Logger logger = LoggerUtils.getLogger(CacheUtils.class);
private static CacheUtils INSTANCE = null;
/**
* Cache manager instance
* 缓存管理器实例
*/
private final CacheManager cacheManager;
/**
* Constructor for cache utilities
* 缓存工具类构建方法
*/
private CacheUtils(final CacheManager cacheManager) {
this.cacheManager = cacheManager;
}
/**
* Retrieve singleton instance of CacheUtils
* 获取缓存工具类的单例实例
*
* @return Instance of cache utilities
* 缓存工具类的单例实例
*/
public static CacheUtils getInstance() throws CacheException {
if (CacheUtils.INSTANCE == null) {
CacheUtils.initialize();
}
return CacheUtils.INSTANCE;
}
/**
* Initialize cache utilities instance
* 初始化缓存工具类
*
* @throws CacheException Not found cache manager implements class
* 未找到缓存管理器的实现类
*/
public static void initialize() throws CacheException {
if (CacheUtils.INSTANCE == null) {
CacheUtils.INSTANCE = new CacheUtils(ServiceLoader.load(CacheManager.class)
.findFirst()
.orElseThrow(() -> new CacheException(0x000C00000001L, "Not_Found_Cache_Manager_Impl")));
}
}
/**
* Register cache
* 注册缓存
*
* @param cacheName Cache identify name
* 缓存识别名称
* @param cacheConfig Cache configure information
* 缓存配置信息
* @return Register result, Boolean.TRUE for register succeed, Boolean.FALSE for register failed
* 注册结果,成功返回Boolean.TRUE,失败返回Boolean.FALSE
*/
public boolean register(final String cacheName, final CacheConfig cacheConfig) {
if (this.logger.isDebugEnabled()) {
this.logger.debug("Register_Cache_Debug", cacheName, cacheConfig);
}
return this.cacheManager.register(cacheName, cacheConfig);
}
/**
* Check given cache name was registered
* 使用指定的缓存名称、配置信息注册缓存
*
* @param cacheName Cache identify name
* 缓存识别名称
*/
public boolean registered(final String cacheName) {
if (this.logger.isDebugEnabled()) {
this.logger.debug("Check_Register_Cache_Debug", cacheName);
}
return this.cacheManager.registered(cacheName);
}
/**
* Retrieve cache client by given cache name
* 根据给定的缓存识别名称获取缓存客户端
*
* @param cacheName Cache identify name
* 缓存识别名称
* @return CacheClient instance or null if given cache name not registered
* 缓存客户端实例,如果给定的缓存识别名称未找到,则返回null
*/
public CacheClient client(final String cacheName) {
return this.cacheManager.client(cacheName);
}
/**
* Deregister cache
* 取消注册缓存
*
* @param cacheName Cache identify name
* 缓存识别名称
*/
public static void deregister(final String cacheName) {
if (CacheUtils.INSTANCE == null) {
return;
}
INSTANCE.cacheManager.deregister(cacheName);
}
/**
* Destroy singleton instance
* 取消注册缓存
*/
public static void destroy() {
if (CacheUtils.INSTANCE != null) {
CacheUtils.INSTANCE.cacheManager.destroy();
CacheUtils.INSTANCE = null;
}
}
}