缓存配置 ehcache.xml
<?xml version="1.0" encoding="UTF-8"?> <ehcache> <!-- 此处的路径可以自行定义,eg:“c://myapp//cache” ,即是自行定义的 <diskStore path="c://myapp//cache" />--> <!-- 而这个“java.io.tmpdir”则是window环境下的tmp ,寻找环境变量下的路 径方式:System.out.println(System.getProperty("java.io.tmpdir"));--> <diskStore path="java.io.tmpdir" /> <defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="12000" timeToLiveSeconds="12000" overflowToDisk="true" diskPersistent="false" diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU" /> <!-- maxElementsOnDisk="100000" 最大的磁盘存储数 而如果保持到缓存中的数据,超过了内存 缓存的最大数据,写入了一部分到磁盘中,此处的配置值不够大的话,就会导致取内存缓存中的 的数据时抛异常 --> <cache name="logBean" maxElementsInMemory="100" eternal="false" maxElementsOnDisk="100000000" overflowToDisk="true" timeToIdleSeconds="3600" timeToLiveSeconds="3600" memoryStoreEvictionPolicy="LFU" /> <cache name="logBean2" maxElementsInMemory="100" eternal="false" maxElementsOnDisk="100000000" overflowToDisk="true" timeToIdleSeconds="3600" timeToLiveSeconds="3600" memoryStoreEvictionPolicy="LFU" /> </ehcache>
实现部分
一、初始化 cache
private static CacheManager manager;
static {
init();
}
private static void init() {
String rootPath = ClassLoader.getSystemResource("").toString();
rootPath = rootPath.substring(5, rootPath.length());
String fileName = rootPath + "ehcache.xml";
manager = new CacheManager(fileName);// 引入写好的ehcache配置文件
}
二、 测试 aop 和 cache结合
public static void main(String[] args) {
String time3 = DateUtil.getNowTimeStr("yyyy-MM-dd hh:mm:ss.sss");
for (int i = 0; i < 10000000; i++) {
testJDKcache();
}
String time4 = DateUtil.getNowTimeStr("yyyy-MM-dd hh:mm:ss.sss");
for (int i = 0; i < 10000000; i++) {
testCGLIBcache();
}
String time5 = DateUtil.getNowTimeStr("yyyy-MM-dd hh:mm:ss.sss");
}
public static void testJDKcache() {
Cache cache = manager.getCache("logBean");
IBusiness2 proxyBusiness;
Element e = cache.get("testJDKcache");
if (e == null) {
// 需要代理的接口,被代理类实现的多个接口都必须在这里定义
Class[] proxyInterface = new Class[] { IBusiness.class,
IBusiness2.class };
// 构建AOP的Advice,这里需要歘如业务的实例
LogInvocationHandler handler = new LogInvocationHandler(new Business());
// 生成代理类的字节码加载器
ClassLoader classLoader = LogInvocationHandler.class.getClassLoader();
// 织入器,织入代码并生成代理类
proxyBusiness = (IBusiness2) Proxy.newProxyInstance(
classLoader, proxyInterface, handler);
Element element = new Element("testJDKcache", proxyBusiness);
cache.put(element);
cache.flush();// 将缓存中的数据写入磁盘中,但是只有在超出配置的最大内存缓存值时,
//才会执行此操作,此处测试为100000才执行
} else {
proxyBusiness = (IBusiness2)e.getValue();
}
//使用代理类的实例调用方法
proxyBusiness.doSomeThing2();
((IBusiness)proxyBusiness).doSomeThing();
}
public static void testCGLIBcache(){
Cache cache = manager.getCache("logBean2");
IBusiness2 newBusiness;
Element e = cache.get("testCGLIBcache");
if (e == null) {
//创建一个织入器
Enhancer enhancer = new Enhancer();
//设置父类
enhancer.setSuperclass(Business.class);
//设置需要织入的逻辑
enhancer.setCallback(new LogIntercept());
//使用织入器创建子类
newBusiness = (IBusiness2)enhancer.create();
Element element = new Element("testCGLIBcache", newBusiness);
cache.put(element);
cache.flush();// 将缓存中的数据写入磁盘中,但是只有在超出配置的最大内存缓存值时,
//才会执行此操作,此处测试为100000才执行
} else {
newBusiness = (IBusiness2)e.getValue();
}
newBusiness.doSomeThing2();
((IBusiness)newBusiness).doSomeThing();
}
三、测试结果
testJDKcache:6000
testCGLIBcache:4000
四、问题
1。 Cache cache = manager.getCache("logBean2"); 需要配置两个 不能一个对多个对象,有待找原因
2。 IBusiness 接口实现 extends Serializable 在cglib中可以提升很高