本文介绍 Shiro + EHCache+Spring 的结合:java
1、首先有两种方式来建立缓存实例,
只介绍spring bean配置方式:spring
一、Spring EhCacheManagerFactoryBean方式建立。apache
二、EhCacheManager方式建立。缓存
2、EHCache配置文件说明
1、EHCache 配置文件代码。app
二、EHCache 配置文件说明jvm
<?xmlversion ="1.0"encoding="UTF-8"?>ide
测试
spa
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="false"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120">
3、ShiroCacheManager管理类。
ShiroCacheManager实现org.apache.shiro.cache.Cache接口,重写里面方法。
4、实例测试
看代码。
packagecom.ehcache.test;
importorg.apache.shiro.cache.Cache;
importorg.springframework.context.support.ClassPathXmlApplicationContext;
publicclassMain {
publicstaticvoidmain(String[]args) {
//一、建立 Spring 的 IOC 容器
ClassPathXmlApplicationContextctx=newClassPathXmlApplicationContext("applicationContext.xml");
//二、获取IOC容器中ShiroCacheManager实例
ShiroCacheManagershiroCacheManager=(ShiroCacheManager)ctx.getBean("shiroCacheManager");
Cachecache=shiroCacheManager.getCache();
//三、保存
cache.put("key","12");
System.out.println(cache.get("key"));
System.out.println("***************************");
//四、 删除
cache.remove("key");
System.out.println(cache.get("key"));
}
}