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