org.nervousync.cache.provider.impl.AbstractProvider Maven / Gradle / Ivy
/*
* 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.provider.impl;
import java.util.List;
import jakarta.xml.bind.annotation.XmlElement;
import org.nervousync.cache.annotation.CacheProvider;
import org.nervousync.cache.commons.CacheGlobals;
import org.nervousync.cache.exceptions.CacheException;
import org.nervousync.cache.provider.Provider;
import org.nervousync.security.factory.SecureFactory;
import org.nervousync.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.nervousync.cache.config.CacheConfig;
import org.nervousync.cache.config.CacheConfig.CacheServer;
import org.nervousync.commons.core.Globals;
/**
* Abstract provider class, all providers must extend this class
* 缓存适配器抽象类,所有缓存适配器实现类必须继承本抽象类
*
* @author Steven Wee [email protected]
* @version $Revision: 1.0 $ $Date: Apr 25, 2017 3:01:30 PM $
*/
public abstract class AbstractProvider implements Provider {
/**
* Logger instance
* 日志实例
*/
protected final Logger logger = LoggerFactory.getLogger(this.getClass());
/**
* Default port number
* 默认端口号
*/
private final int defaultPort;
/**
* Server connect timeout
* 缓存服务器的连接超时时间
*/
private int connectTimeout = Globals.DEFAULT_VALUE_INT;
/**
* Client pool size
* 连接池大小
*/
private int clientPoolSize = Globals.DEFAULT_VALUE_INT;
/**
* Connect retry count
* 连接超时重试次数
*/
@XmlElement(name = "retry_count")
private int retryCount = CacheGlobals.DEFAULT_RETRY_COUNT;
/**
* Limit size of generated client instance
* 客户端实例阈值
*/
private int maximumClient = Globals.DEFAULT_VALUE_INT;
/**
* Default expire time
* 默认过期时间
*/
private int expireTime = Globals.DEFAULT_VALUE_INT;
/**
* Default constructor
*
* @throws CacheException If class not annotation by org.nervousync.cache.annotation.CacheProvider
* @see CacheProvider
*/
protected AbstractProvider() throws CacheException {
if (this.getClass().isAnnotationPresent(CacheProvider.class)) {
this.defaultPort = this.getClass().getAnnotation(CacheProvider.class).defaultPort();
} else {
throw new CacheException("Provider implement class must annotation with " + CacheProvider.class.getName());
}
}
/**
* Initialize cache agent
* 初始化缓存实例
*
* @param cacheConfig Cache config instance
* 缓存配置实例
*/
public void initialize(CacheConfig cacheConfig) {
this.connectTimeout = cacheConfig.getConnectTimeout();
this.clientPoolSize = cacheConfig.getClientPoolSize();
this.retryCount = cacheConfig.getRetryCount();
this.maximumClient = cacheConfig.getMaximumClient();
this.expireTime = cacheConfig.getExpireTime();
String passWord = cacheConfig.getPassWord();
if (StringUtils.notBlank(passWord) && StringUtils.notBlank(cacheConfig.getSecureName())
&& SecureFactory.getInstance().registeredConfig(cacheConfig.getSecureName())) {
passWord = SecureFactory.getInstance().decrypt(cacheConfig.getSecureName(), passWord);
}
this.initializeConnection(cacheConfig.getServerConfigList(), cacheConfig.getUserName(), passWord);
}
/**
* Retrieve server connect timeout
* 读取缓存服务器的连接超时时间
*
* @return Connect timeout
* 连接超时时间
*/
public int getConnectTimeout() {
return connectTimeout;
}
/**
* Retrieve connect client pool size
* 读取客户端连接池的大小
*
* @return Client pool size
* 连接池大小
*/
public int getClientPoolSize() {
return clientPoolSize;
}
/**
* Retrieve server connect retry count
* 读取缓存服务器的连接超时重试次数
*
* @return Connect retry count
* 连接超时重试次数
*/
public int getRetryCount() {
return retryCount;
}
/**
* Retrieve limit size of generated client instance
* 读取允许创建的客户端实例阈值
*
* @return Limit size of generated client instance
* 客户端实例阈值
*/
public int getMaximumClient() {
return maximumClient;
}
/**
* Retrieve default expire time
* 读取缓存的默认过期时间
*
* @return Default expire time
* 默认过期时间
*/
public int getExpireTime() {
return expireTime;
}
/**
* Initialize cache server connections
* 初始化缓存服务器连接池
*
* @param serverConfigList cache server list
*/
protected abstract void initializeConnection(final List serverConfigList,
final String userName, final String passWord);
/**
* Set key-value to cache server by default expire time
* 使用默认的过期时间设置缓存信息
*
* @param key Cache key
* 缓存键值
* @param value Cache value
* 缓存数据
*/
public final void set(String key, String value) {
this.set(key, value, this.expireTime);
}
/**
* Add a new key-value to cache server by default expire time
* 使用默认的过期时间添加缓存信息
*
* @param key Cache key
* 缓存键值
* @param value Cache value
* 缓存数据
*/
public final void add(String key, String value) {
this.add(key, value, this.expireTime);
}
/**
* Replace exists value of given key by given value by default expire time
* 使用默认的过期时间替换已存在的缓存信息
*
* @param key Cache key
* 缓存键值
* @param value Cache value
* 缓存数据
*/
public final void replace(String key, String value) {
this.replace(key, value, this.expireTime);
}
protected final int serverPort(final int serverPort) {
return serverPort == Globals.DEFAULT_VALUE_INT ? this.defaultPort : serverPort;
}
/**
* Set expire time to new given expire value which cache key was given
* 将指定的缓存键值过期时间设置为指定的新值
*
* @param key Cache key
* 缓存键值
* @param expire New expire time
* 新的过期时间
*/
public abstract void expire(String key, int expire);
}