All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
org.nervousync.cache.client.impl.CacheClientImpl Maven / Gradle / Ivy
package org.nervousync.cache.client.impl;
import org.nervousync.cache.api.CacheClient;
import org.nervousync.cache.config.CacheConfig;
import org.nervousync.cache.exceptions.CacheException;
import org.nervousync.cache.provider.impl.AbstractProvider;
import org.nervousync.commons.core.Globals;
import org.nervousync.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public final class CacheClientImpl implements CacheClient {
/**
* Logger instance
* 日志实例
*/
private final Logger logger = LoggerFactory.getLogger(this.getClass());
/**
* Current cache provider instance
* 缓存实现对象
*/
private final AbstractProvider cacheProvider;
/**
* Constructor for cache agent
*
* @param cacheConfig System cache config instance
* 系统缓存配置实例
* @param providerImplClass Cache provider implement class
* 缓存适配器实现类
* @throws CacheException Generate instance of provider failed or provider implement class not extends with AbstractCacheProvider
* 缓存适配器实现类没有继承AbstractCacheProvider或初始化缓存适配器对象出错
*/
public CacheClientImpl(CacheConfig cacheConfig, Class> providerImplClass) throws CacheException {
if (providerImplClass != null && AbstractProvider.class.isAssignableFrom(providerImplClass)) {
try {
this.cacheProvider = (AbstractProvider)providerImplClass.getDeclaredConstructor().newInstance();
this.cacheProvider.initialize(cacheConfig);
return;
} catch (ReflectiveOperationException e) {
throw new CacheException(e);
}
}
throw new CacheException("Provider implement class is invalid! ");
}
/**
* Set key-value to cache server by default expire time
* 使用默认的过期时间设置缓存信息
*
* @param key Cache key
* 缓存键值
* @param value Cache value
* 缓存数据
*/
public void set(String key, String value) {
this.logInfo(key, value);
this.cacheProvider.set(key, value);
}
/**
* Set key-value to cache server and set expire time
* 使用指定的过期时间设置缓存信息
*
* @param key Cache key
* 缓存键值
* @param value Cache value
* 缓存数据
* @param expire Expire time
* 过期时间
*/
public void set(String key, String value, int expire) {
this.logInfo(key, value);
this.cacheProvider.set(key, value, expire);
}
/**
* Add a new key-value to cache server by default expire time
* 使用默认的过期时间添加缓存信息
*
* @param key Cache key
* 缓存键值
* @param value Cache value
* 缓存数据
*/
public void add(String key, String value) {
this.logInfo(key, value);
this.cacheProvider.add(key, value);
}
/**
* Add a new key-value to cache server and set expire time
* 使用指定的过期时间添加缓存信息
*
* @param key Cache key
* 缓存键值
* @param value Cache value
* 缓存数据
* @param expire Expire time
* 过期时间
*/
public void add(String key, String value, int expire) {
this.logInfo(key, value);
this.cacheProvider.add(key, value, expire);
}
/**
* Replace exists value of given key by given value by default expire time
* 使用默认的过期时间替换已存在的缓存信息
*
* @param key Cache key
* 缓存键值
* @param value Cache value
* 缓存数据
*/
public void replace(String key, String value) {
this.logInfo(key, value);
this.cacheProvider.replace(key, value);
}
/**
* Replace exists value of given key by given value and set expire time
* 使用指定的过期时间替换已存在的缓存信息
*
* @param key Cache key
* 缓存键值
* @param value Cache value
* 缓存数据
* @param expire Expire time
* 过期时间
*/
public void replace(String key, String value, int expire) {
this.logInfo(key, value);
this.cacheProvider.replace(key, value, expire);
}
/**
* Set expire time to new given expire value which cache key was given
* 将指定的缓存键值过期时间设置为指定的新值
*
* @param key Cache key
* 缓存键值
* @param expire New expire time
* 新的过期时间
*/
public void expire(String key, int expire) {
this.cacheProvider.expire(key, expire);
}
/**
* Operate touch to given keys
* @param keys Keys
*/
public void touch(String... keys) {
this.cacheProvider.touch(keys);
}
/**
* Remove cache key-value from cache server
* 移除指定的缓存键值
*
* @param key Cache key
* 缓存键值
*/
public void delete(String key) {
this.cacheProvider.delete(key);
}
/**
* Read cache value from cache key which cache key was given
* 读取指定缓存键值对应的缓存数据
*
* @param key Cache key
* 缓存键值
* @return Cache value or null if cache key was not exists or it was expired
* 读取的缓存数据,如果缓存键值不存在或已过期,则返回null
*/
public String get(String key) {
if (StringUtils.isEmpty(key)) {
return null;
}
return this.cacheProvider.get(key);
}
/**
* Increment data by given cache key and value
*
* @param key Cache key
* 缓存键值
* @param step Increment step value
* 自增步进值
* @return Operate result
* 操作结果
*/
public long incr(String key, long step) {
if (StringUtils.isEmpty(key)) {
return Globals.DEFAULT_VALUE_LONG;
}
return this.cacheProvider.incr(key, step);
}
/**
* Decrement data by given cache key and value
*
* @param key Cache key
* 缓存键值
* @param step Decrement step value
* 自减步进值
* @return Operate result
* 操作结果
*/
public long decr(String key, long step) {
if (StringUtils.isEmpty(key)) {
return Globals.DEFAULT_VALUE_LONG;
}
return this.cacheProvider.decr(key, step);
}
/**
* Destroy agent instance
* 销毁缓存对象
*/
public void destroy() {
this.cacheProvider.destroy();
}
/**
* Logging cache key and value when debug mode was enabled
* 当调试模式开启时,在日志中输出缓存键值和数据
*
* @param key Cache key
* 缓存键值
* @param value Cache value
* 缓存数据
*/
private void logInfo(String key, Object value) {
if (this.logger.isDebugEnabled()) {
this.logger.debug("Cached key: {}", key);
this.logger.debug("Cached value: {}", value);
}
}
}