com.jquicker.cache.CacheProxy Maven / Gradle / Ivy
package com.jquicker.cache;
import java.util.Properties;
import com.jquicker.aop.Advice;
import com.jquicker.commons.cache.ICache;
import com.jquicker.commons.cache.LocalCache;
import com.jquicker.commons.cache.RedisCache;
import com.jquicker.configure.RedisConfig;
import com.jquicker.context.bean.ProxyFactory;
/**
* 缓存代理类
* 当使用Redis缓存时,为避免多项目同服务器部署而导致缓存键值冲突,通过代理对象增加全局的缓存前缀
* 可通过RedisConfig.setPrefix()或者配置prefix设置全局前缀
*
* @author OL
*
*/
public final class CacheProxy {
private static String PREFIX = RedisConfig.getPrefix();
public static ICache PROXY;
static {
// 目标对象
Properties config = RedisConfig.get();
if(config != null && RedisConfig.isEnable()) {
PROXY = new RedisCache(config);
// 申明一个切面Aspect
CacheAspect aspect = new CacheAspect(PREFIX);
// 创建一个 Advice
Advice advice = new CacheAdvice(PROXY, aspect);
// 为目标对象生成代理对象
PROXY = ProxyFactory.newProxyInstance(PROXY, advice);
} else {
PROXY = new LocalCache(); // 默认使用本地缓存
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy