在Spring中使用缓存可以有效地避免不断地获取相同数据,重复地访问数据库,导致程序性能恶化。
在Spring中已经定义了缓存的CacheManager和Cache接口,只需要实例化便可使用。
Spring整合EHCache框架
a.首先在pom.xml中引入EHCache框架和Spring.Support
1 2 3 4 5 6 7 8 9 10 11 | <dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache</artifactId> <version> 2.10 . 4 </version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version> 4.1 . 4 .RELEASE</version> </dependency> |
b.配置CacheManager对象,可以通过xml,在xml中配置springMVC和mybaties已经相当臃肿,此处使用注解方式。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | import net.sf.ehcache.CacheManager; import org.springframework.cache.annotation.EnableCaching; import org.springframework.cache.ehcache.EhCacheCacheManager; import org.springframework.cache.ehcache.EhCacheManagerFactoryBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.io.ClassPathResource; @Configuration @EnableCaching public class CacheConfigurarion { @Bean public EhCacheCacheManager cacheManger(CacheManager ehcacheCacheManger){ return new EhCacheCacheManager(ehcacheCacheManger); } @Bean public EhCacheManagerFactoryBean ehCacheManagerFactoryBean() { EhCacheManagerFactoryBean ehCacheManagerFactoryBean = new EhCacheManagerFactoryBean(); ehCacheManagerFactoryBean.setConfigLocation( new ClassPathResource( "ehcache.xml" )); return ehCacheManagerFactoryBean; } } |
EhCacheManagerFactoryBean, 封装了三个属性(CacheManager类型的cacheManager,boolean类型的shared和表示ehcache配置信息的configLocation). 从类名和封装了的属性上可以看出Spring用此类(afterPropertiesSet方法)来new出一个 CacheManager实例.
cacheManger将EhCache框架中CacheManager引入Spring框架,这也为@Cacheable,@CachePut,@CacheEvit等缓存注解。在ehcache.xml中配置缓存策略,可以定义多个cache,见下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | <ehcache> <diskStore path= "java.io.tmpdir" /> <cache name= "demoCache" maxEntriesLocalHeap= "10000" maxEntriesLocalDisk= "1000" eternal= "false" diskSpoolBufferSizeMB= "20" timeToIdleSeconds= "300" timeToLiveSeconds= "600" memoryStoreEvictionPolicy= "LFU" transactionalMode= "off" > <persistence strategy= "localTempSwap" /> </cache> <cache name= "demo" maxEntriesLocalHeap= "10000" maxEntriesLocalDisk= "1000" eternal= "false" diskSpoolBufferSizeMB= "20" timeToIdleSeconds= "300" timeToLiveSeconds= "600" memoryStoreEvictionPolicy= "LFU" transactionalMode= "off" > <persistence strategy= "localTempSwap" /> </cache> </ehcache> |
name:缓存名称。
maxElementsInMemory:缓存最大个数。
eternal:对象是否永久有效,一但设置了,timeout将不起作用。
timeToIdleSeconds:设置对象在失效前的允许闲置时间(单位:秒)。仅当eternal=false对象不是永久有效时使用,可选属性,默认值是0,也就是可闲置时间无穷大。
timeToLiveSeconds:设置对象在失效前允许存活时间(单位:秒)。最大时间介于创建时间和失效时间之间。仅当eternal=false对象不是永久有效时使用,默认是0.,也就是对象存活时间无穷大。
overflowToDisk:当内存中对象数量达到maxElementsInMemory时,Ehcache将会对象写到磁盘中。
diskSpoolBufferSizeMB:这个参数设置DiskStore(磁盘缓存)的缓存区大小。默认是30MB。每个Cache都应该有自己的一个缓冲区。
maxElementsOnDisk:硬盘最大缓存个数。
diskPersistent:是否缓存虚拟机重启期数据 Whether the disk store persists between restarts of the Virtual Machine. The default value is false.
diskExpiryThreadIntervalSeconds:磁盘失效线程运行时间间隔,默认是120秒。
memoryStoreEvictionPolicy:当达到maxElementsInMemory限制时,Ehcache将会根据指定的策略去清理内存。默认策略是LRU(最近最少使用)。你可以设置为FIFO(先进先出)或是LFU(较少使用)。
clearOnFlush:内存数量最大时是否清除。
3.在Service中定义缓存注解,可以不断调用,发现打印的字符串只在第一次查询时被打印。
1 2 3 4 5 6 7 8 | @Cacheable (value = "demo" ) public List<User> selectUserByPage( int pageNum, int pageSize){ DataSourceContextHolder. setDbType(DataSourceType.DateBase_master); PageHelper.startPage(pageNum, pageSize); List<User> users = userDao.selectAll(); System.out.println( "为数据做了缓存" ); return users; } |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步