org.nervousync.cache.builder.AbstractCacheConfigBuilder 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.builder;
import org.nervousync.builder.AbstractBuilder;
import org.nervousync.cache.commons.CacheGlobals;
import org.nervousync.cache.config.CacheConfig;
import org.nervousync.cache.enumeration.ClusterMode;
import org.nervousync.commons.Globals;
import org.nervousync.exceptions.builder.BuilderException;
import org.nervousync.security.factory.SecureConfig;
import org.nervousync.security.factory.SecureFactory;
import org.nervousync.utils.StringUtils;
import java.util.List;
import java.util.Objects;
/**
* Abstract cache configure builder
* 缓存配置构建器
*
* @author Steven Wee [email protected]
* @version $Revision: 1.0 $ $Date: 2023-03-14 09:18 $
*/
public abstract class AbstractCacheConfigBuilder extends AbstractBuilder {
/**
* Cache config instance
* 缓存配置信息
*/
protected final CacheConfig cacheConfig;
/**
* Constructor for cache configure builder
* 缓存配置构造器构建方法
*
* @param parentBuilder Parent builder instance
* 上级构建器实例
* @param cacheConfig Current configure instance or null for generate new configure
* 当前的缓存配置,如果传入null则生成一个新的配置
*/
protected AbstractCacheConfigBuilder(final T parentBuilder, final CacheConfig cacheConfig) {
super(parentBuilder);
this.cacheConfig = (cacheConfig == null) ? new CacheConfig() : cacheConfig;
}
/**
* Configure cache provider
* 设置缓存适配器
*
* @param providerName Cache provider name
* 缓存适配器名称
* @return Current cache configure builder
* 当前缓存配置构建器
*/
public final AbstractCacheConfigBuilder providerName(final String providerName) {
if (StringUtils.notBlank(providerName)) {
this.cacheConfig.setProviderName(providerName);
}
return this;
}
/**
* Configure secure name for protect password
* 设置用于保护密码的安全配置名称
*
* @param secureName Secure name
* 安全配置名称
* @return Current cache configure builder
* 当前缓存配置构建器
*/
public final AbstractCacheConfigBuilder secureName(final String secureName) {
if (SecureFactory.initialized()) {
if (StringUtils.notBlank(this.cacheConfig.getSecureName())
|| !Objects.equals(this.cacheConfig.getSecureName(), secureName)) {
this.cacheConfig.setPassWord(
SecureFactory.update(this.cacheConfig.getPassWord(),
this.cacheConfig.getSecureName(), secureName));
}
if (this.cacheConfig.getSecureConfig() != null
&& !Objects.equals(this.cacheConfig.getSecureName(), secureName)) {
SecureFactory.deregister(this.cacheConfig.getSecureName());
}
this.cacheConfig.setSecureConfig(null);
this.cacheConfig.setSecureName(StringUtils.notBlank(secureName) ? secureName : Globals.DEFAULT_VALUE_STRING);
}
return this;
}
/**
* Configure secure config information
* 设置用于保护密码的安全配置
*
* @param secureName Secure name
* 安全配置名称
* @param secureConfig Secure config information
* 安全配置信息
* @return Current cache configure builder
* 当前缓存配置构建器
*/
public final AbstractCacheConfigBuilder secureConfig(final String secureName, final SecureConfig secureConfig) {
if (SecureFactory.initialized() && StringUtils.notBlank(secureName) && secureConfig != null) {
if (StringUtils.notBlank(this.cacheConfig.getPassWord())) {
SecureFactory.register(Globals.DEFAULT_TEMPORARY_SECURE_NAME, secureConfig);
String passWord = this.cacheConfig.getPassWord();
if (StringUtils.notBlank(this.cacheConfig.getSecureName())) {
passWord = SecureFactory.update(passWord, this.cacheConfig.getSecureName(),
Globals.DEFAULT_TEMPORARY_SECURE_NAME);
} else {
passWord = SecureFactory.encrypt(passWord, Globals.DEFAULT_TEMPORARY_SECURE_NAME);
}
this.cacheConfig.setPassWord(passWord);
SecureFactory.deregister(Globals.DEFAULT_TEMPORARY_SECURE_NAME);
}
this.cacheConfig.setSecureName(secureName);
this.cacheConfig.setSecureConfig(secureConfig);
}
return this;
}
/**
* Configure server connect timeout
* 设置缓存服务器的连接超时时间
*
* @param connectTimeout Connect timeout
* 连接超时时间
* @return Current cache configure builder
* 当前缓存配置构建器
*/
public final AbstractCacheConfigBuilder connectTimeout(final int connectTimeout) {
if (connectTimeout > 0) {
this.cacheConfig.setConnectTimeout(connectTimeout);
} else {
this.cacheConfig.setConnectTimeout(CacheGlobals.DEFAULT_CONNECTION_TIMEOUT);
}
return this;
}
/**
* Configure default expire time, setting -1 for never expire
* 设置缓存的默认过期时间,设置为-1则永不过期
*
* @param expireTime Default expire time
* 默认过期时间
* @return Current cache configure builder
* 当前缓存配置构建器
*/
public final AbstractCacheConfigBuilder expireTime(final int expireTime) {
if (expireTime > 0) {
this.cacheConfig.setExpireTime(expireTime);
} else {
this.cacheConfig.setExpireTime(CacheGlobals.DEFAULT_EXPIRE_TIME);
}
return this;
}
/**
* Configure connect client pool size
* 设置客户端连接池的大小
*
* @param clientPoolSize Client pool size
* 连接池大小
* @return Current cache configure builder
* 当前缓存配置构建器
*/
public final AbstractCacheConfigBuilder clientPoolSize(final int clientPoolSize) {
if (clientPoolSize > 0) {
this.cacheConfig.setClientPoolSize(clientPoolSize);
} else {
this.cacheConfig.setClientPoolSize(CacheGlobals.DEFAULT_CLIENT_POOL_SIZE);
}
return this;
}
/**
* Configure limit size of generated client instance
* 设置允许创建的客户端实例阈值
*
* @param maximumClient Limit size of generated client instance
* 客户端实例阈值
* @return Current cache configure builder
* 当前缓存配置构建器
*/
public final AbstractCacheConfigBuilder maximumClient(final int maximumClient) {
if (maximumClient > 0) {
this.cacheConfig.setMaximumClient(maximumClient);
} else {
this.cacheConfig.setMaximumClient(CacheGlobals.DEFAULT_MAXIMUM_CLIENT);
}
return this;
}
/**
* Configure connection timeout retry count
* 设置连接超时后的重试次数
*
* @param retryCount Connect retry count
* 连接超时重试次数
* @return Current cache configure builder
* 当前缓存配置构建器
*/
public final AbstractCacheConfigBuilder retryCount(final int retryCount) {
if (retryCount > 0) {
this.cacheConfig.setRetryCount(retryCount);
} else {
this.cacheConfig.setRetryCount(CacheGlobals.DEFAULT_RETRY_COUNT);
}
return this;
}
/**
* Configure cache server authorization information
* 设置缓存服务器的用户名和密码
*
* @param userName Cache server username
* 缓存服务器用户名
* @param passWord Cache server password
* 缓存服务器密码
* @return Current cache configure builder
* 当前缓存配置构建器
*/
public final AbstractCacheConfigBuilder authorization(final String userName, final String passWord) {
this.cacheConfig.setUserName(userName);
if (StringUtils.notBlank(passWord)) {
String encPassword;
if (StringUtils.notBlank(this.cacheConfig.getSecureName())) {
encPassword = SecureFactory.encrypt(this.cacheConfig.getSecureName(), passWord);
} else {
encPassword = passWord;
}
this.cacheConfig.setPassWord(encPassword);
} else {
this.cacheConfig.setPassWord(Globals.DEFAULT_VALUE_STRING);
}
return this;
}
/**
* Configure cache cluster mode
* 设置缓存服务器的集群类型
*
* @param clusterMode Cache Cluster Mode
* 缓存集群类型
* @return Current cache configure builder
* 当前缓存配置构建器
* @see ClusterMode
*/
public final AbstractCacheConfigBuilder clusterMode(final ClusterMode clusterMode) {
this.cacheConfig.setClusterMode(clusterMode.toString());
return this;
}
/**
* Configure cache cluster mode
* 设置缓存服务器的集群类型
*
* @param masterName Master server name
* 主服务器名称
* @return Current cache configure builder
* 当前缓存配置构建器
*/
public final AbstractCacheConfigBuilder masterName(final String masterName) {
this.cacheConfig.setMasterName(masterName);
return this;
}
/**
* Configure cache server information
* 设置缓存服务器相关信息
*
* @return Current cache configure builder
* 当前缓存配置构建器
*/
public final CacheServerConfigBuilder serverBuilder() {
return CacheServerConfigBuilder.newBuilder(this, new CacheConfig.ServerConfig());
}
/**
* Configure cache server information
* 设置缓存服务器相关信息
*
* @param serverAddress Cache server address
* 缓存服务器地址
* @param serverPort Cache server port
* 缓存服务器端口号
* @return Current cache configure builder
* 当前缓存配置构建器
*/
public final CacheServerConfigBuilder serverBuilder(final String serverAddress, final int serverPort) {
return CacheServerConfigBuilder.newBuilder(this,
this.cacheConfig.getServerConfigList()
.stream()
.filter(existsConfig -> existsConfig.match(serverAddress, serverPort))
.findFirst()
.orElse(new CacheConfig.ServerConfig()));
}
/**
* Remove cache server information
* 删除缓存服务器信息
*
* @param serverAddress Cache server address
* 缓存服务器地址
* @param serverPort Cache server port
* 缓存服务器端口号
* @return Current cache configure builder
* 当前缓存配置构建器
*/
public final AbstractCacheConfigBuilder removeServer(final String serverAddress, final int serverPort) {
List serverConfigList = this.cacheConfig.getServerConfigList();
if (serverConfigList.removeIf(serverConfig -> serverConfig.match(serverAddress, serverPort))) {
this.cacheConfig.setServerConfigList(serverConfigList);
}
return this;
}
/**
* Upsert cache server information
* 更新缓存服务器信息
*
* @param serverConfig Cache server configure information
* 缓存服务器配置信息
* @throws BuilderException Throws if cache server address is empty
* 如果缓存服务器地址未配置,则抛出异常
*/
final void serverConfig(final CacheConfig.ServerConfig serverConfig) throws BuilderException {
if (serverConfig == null) {
return;
}
if (StringUtils.isEmpty(serverConfig.getServerAddress())) {
throw new BuilderException(0x000C00000002L, "Server_Address_Cache_Error");
}
List serverConfigList = this.cacheConfig.getServerConfigList();
if (serverConfigList.stream().anyMatch(existsConfig -> existsConfig.match(serverConfig))) {
serverConfigList.replaceAll(existsConfig -> {
if (existsConfig.match(serverConfig)) {
return serverConfig;
}
return existsConfig;
});
} else {
serverConfigList.add(serverConfig);
}
this.cacheConfig.setServerConfigList(serverConfigList);
}
}