org.nervousync.cache.builder.CacheConfigBuilder Maven / Gradle / Ivy
package org.nervousync.cache.builder;
import org.nervousync.cache.commons.CacheGlobals;
import org.nervousync.cache.config.CacheConfig;
import org.nervousync.cache.config.CacheConfig.CacheServer;
import org.nervousync.cache.provider.ProviderManager;
import org.nervousync.commons.core.Globals;
import org.nervousync.security.factory.SecureFactory;
import org.nervousync.utils.StringUtils;
import java.util.List;
/**
* Cache configure builder
* 缓存配置构建器
*
* @author Steven Wee [email protected]
* @version $Revision: 1.0 $ $Date: 2018-12-26 17:23 $
*/
public final class CacheConfigBuilder {
/**
* Cache configure instance
* 缓存配置实例
*/
private final CacheConfig cacheConfig;
/**
* Configure modify status
* 配置修改状态
*/
private boolean modified = Boolean.FALSE;
/**
* Constructor for cache configure builder
* 缓存配置构造器构建方法
*
* @param cacheConfig Current configure instance or null for generate new configure
* 当前的缓存配置,如果传入null则生成一个新的配置
*/
private CacheConfigBuilder(final CacheConfig cacheConfig) {
if (cacheConfig == null) {
this.cacheConfig = new CacheConfig();
this.modified = Boolean.TRUE;
} else {
this.cacheConfig = cacheConfig;
}
}
public static CacheConfigBuilder builder() {
return builder(null);
}
public static CacheConfigBuilder builder(final CacheConfig cacheConfig) {
return new CacheConfigBuilder(cacheConfig);
}
/**
* Configure cache provider
* 设置缓存适配器
*
* @param providerName Cache provider name
* 缓存适配器名称
* @return Current cache configure builder
* 当前缓存配置构建器
*/
public CacheConfigBuilder providerName(final String providerName) {
if (StringUtils.notBlank(providerName) && ProviderManager.registeredProvider(providerName)) {
if (!providerName.equalsIgnoreCase(this.cacheConfig.getProviderName())) {
this.modified = Boolean.TRUE;
}
this.cacheConfig.setProviderName(providerName);
}
return this;
}
/**
* Configure secure name for protect password
* 设置用于保护密码的安全配置名称
*
* @param secureName Secure name
* 安全配置名称
* @return Current cache configure builder
* 当前缓存配置构建器
*/
public CacheConfigBuilder secureName(final String secureName) {
if (SecureFactory.initialized()) {
this.cacheConfig.setPassWord(
SecureFactory.getInstance().update(this.cacheConfig.getPassWord(),
this.cacheConfig.getSecureName(), secureName));
this.cacheConfig.setSecureName(StringUtils.notBlank(secureName) ? secureName : Globals.DEFAULT_VALUE_STRING);
}
return this;
}
/**
* Configure server connect timeout
* 设置缓存服务器的连接超时时间
*
* @param connectTimeout Connect timeout
* 连接超时时间
* @return Current cache configure builder
* 当前缓存配置构建器
*/
public CacheConfigBuilder connectTimeout(final int connectTimeout) {
if (connectTimeout > 0) {
if (connectTimeout != this.cacheConfig.getConnectTimeout()) {
this.modified = Boolean.TRUE;
}
this.cacheConfig.setConnectTimeout(connectTimeout);
} else {
if (this.cacheConfig.getConnectTimeout() != CacheGlobals.DEFAULT_CONNECTION_TIMEOUT) {
this.modified = Boolean.TRUE;
}
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 CacheConfigBuilder expireTime(final int expireTime) {
if (expireTime > 0) {
if (expireTime != this.cacheConfig.getExpireTime()) {
this.modified = Boolean.TRUE;
}
this.cacheConfig.setExpireTime(expireTime);
} else {
if (this.cacheConfig.getExpireTime() != CacheGlobals.DEFAULT_EXPIRE_TIME) {
this.modified = Boolean.TRUE;
}
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 CacheConfigBuilder clientPoolSize(final int clientPoolSize) {
if (clientPoolSize > 0) {
if (clientPoolSize != this.cacheConfig.getClientPoolSize()) {
this.modified = Boolean.TRUE;
}
this.cacheConfig.setClientPoolSize(clientPoolSize);
} else {
if (this.cacheConfig.getClientPoolSize() != CacheGlobals.DEFAULT_CLIENT_POOL_SIZE) {
this.modified = Boolean.TRUE;
}
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 CacheConfigBuilder maximumClient(final int maximumClient) {
if (maximumClient > 0) {
if (maximumClient != this.cacheConfig.getMaximumClient()) {
this.modified = Boolean.TRUE;
}
this.cacheConfig.setMaximumClient(maximumClient);
} else {
if (this.cacheConfig.getMaximumClient() != CacheGlobals.DEFAULT_MAXIMUM_CLIENT) {
this.modified = Boolean.TRUE;
}
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 CacheConfigBuilder retryCount(final int retryCount) {
if (retryCount > 0) {
if (retryCount != this.cacheConfig.getRetryCount()) {
this.modified = Boolean.TRUE;
}
this.cacheConfig.setRetryCount(retryCount);
} else {
if (this.cacheConfig.getRetryCount() != CacheGlobals.DEFAULT_RETRY_COUNT) {
this.modified = Boolean.TRUE;
}
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 CacheConfigBuilder authorization(final String userName, final String passWord) {
if (StringUtils.notBlank(userName)) {
if (!userName.equalsIgnoreCase(this.cacheConfig.getUserName())) {
this.modified = Boolean.TRUE;
}
} else {
if (StringUtils.notBlank(this.cacheConfig.getUserName())) {
this.modified = Boolean.TRUE;
}
}
this.cacheConfig.setUserName(userName);
if (StringUtils.notBlank(passWord)) {
String encPassword;
if (StringUtils.notBlank(this.cacheConfig.getSecureName())) {
encPassword = SecureFactory.getInstance().encrypt(this.cacheConfig.getSecureName(), passWord);
} else {
encPassword = passWord;
}
if (!encPassword.equalsIgnoreCase(this.cacheConfig.getPassWord())) {
this.modified = Boolean.TRUE;
this.cacheConfig.setPassWord(encPassword);
}
} else {
if (StringUtils.notBlank(this.cacheConfig.getPassWord())) {
this.modified = Boolean.TRUE;
}
this.cacheConfig.setPassWord(Globals.DEFAULT_VALUE_STRING);
}
return this;
}
/**
* Configure cache server information
* 设置缓存服务器相关信息
*
* @param serverAddress Cache server address
* 缓存服务器地址
* @param serverPort Cache server port
* 缓存服务器端口号
* @param serverWeight Cache server weight
* 缓存服务器权重值
* @param readOnly Cache server read only status
* 缓存服务器只读状态
* @return Current cache configure builder
* 当前缓存配置构建器
*/
public CacheConfigBuilder configServer(final String serverAddress, final int serverPort,
final int serverWeight, final boolean readOnly) {
List cacheServerList = this.cacheConfig.getServerConfigList();
if (cacheServerList.stream().anyMatch(serverConfig -> serverConfig.match(serverAddress, serverPort))) {
cacheServerList.replaceAll(serverConfig -> {
if (serverConfig.match(serverAddress, serverPort)) {
serverConfig.setServerWeight(serverWeight);
serverConfig.setReadOnly(readOnly);
this.modified = Boolean.TRUE;
}
return serverConfig;
});
} else {
CacheServer serverConfig = new CacheServer();
serverConfig.setServerAddress(serverAddress);
serverConfig.setServerPort(serverPort);
serverConfig.setServerWeight(serverWeight);
serverConfig.setReadOnly(readOnly);
cacheServerList.add(serverConfig);
this.modified = Boolean.TRUE;
}
this.cacheConfig.setServerConfigList(cacheServerList);
return this;
}
/**
* Remove cache server information
* 删除缓存服务器信息
*
* @param serverAddress Cache server address
* 缓存服务器地址
* @param serverPort Cache server port
* 缓存服务器端口号
* @return Current cache configure builder
* 当前缓存配置构建器
*/
public CacheConfigBuilder removeServer(final String serverAddress, final int serverPort) {
List cacheServerList = this.cacheConfig.getServerConfigList();
if (cacheServerList.removeIf(serverConfig -> serverConfig.match(serverAddress, serverPort))) {
this.modified = Boolean.TRUE;
this.cacheConfig.setServerConfigList(cacheServerList);
}
return this;
}
/**
* Confirm cache config and read cache config instance
* 确认配置完成并读取缓存配置实例
*
* @return Cache config instance
* 缓存配置实例
*/
public CacheConfig confirmConfig() {
return this.cacheConfig;
}
/**
* Read configure instance modify status
* 读取当前缓存配置的修改状态
*
* @return Modify status
* 缓存配置的修改状态
*/
public boolean isModified() {
return this.modified;
}
}