ehcache使用
依赖
<dependencies><dependency><groupId>net.sf.ehcache</groupId><artifactId>ehcache</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-cache</artifactId></dependency></dependencies>
配置文件详解
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:noNamespaceSchemaLocation="../config/ehcache.xsd"><!--diskStore:缓存数据持久化的目录 地址 --><diskStore path="F:\develop\ehcache" /><defaultCachemaxElementsInMemory="1000" maxElementsOnDisk="10000000"eternal="false" overflowToDisk="false" diskPersistent="true"timeToIdleSeconds="120"timeToLiveSeconds="120" diskExpiryThreadIntervalSeconds="120"memoryStoreEvictionPolicy="LRU"></defaultCache></ehcache>
maxElementsInMemory: 内存中缓存的element的最大数目
maxElementsOnDisk:在磁盘上缓存的element的最大数目,默认值为0,表示不限制
eternal: 设定缓存的elements是否过期。如果为true,则缓存的数据始终有效,如果是false,要根据timeToIdleSeconds,timeToLiveSeconds判断
overflowToDisk:内存中数据超过限制,是否需要缓存到磁盘上
diskPersistent:是否在磁盘上持久化,重启jvm后,数据是否有效
timeToIdleSeconds:对象空闲时间,只对eternal属性为false起作用
timeToLiveSeconds:对象存活时间,只对eternal属性为false起作用 ,磁盘缓存的清理线程运行间隔
memoryStoreEvictionPolicy:超过内存空间,向磁盘存储数据的策略,可选:LRU 最近最少使用 FIFO 先进先出 LFU 最少使用
API代码(不使用xml)
public class ProgrammaticEhcacheDemo {public static void main(String[] args) {Configuration configuration = new Configuration();DiskStoreConfiguration diskStoreConfigurationParameter = new DiskStoreConfiguration();diskStoreConfigurationParameter.setPath("/opt/test");configuration.addDiskStore(diskStoreConfigurationParameter);CacheManager cacheManager = CacheManager.create(configuration);CacheConfiguration cacheConfiguration = new CacheConfiguration("test",10);PersistenceConfiguration persistenceConfiguration = new PersistenceConfiguration();persistenceConfiguration.strategy(PersistenceConfiguration.Strategy.LOCALTEMPSWAP);cacheConfiguration.persistence(persistenceConfiguration);Cache cache = new Cache(cacheConfiguration);cacheManager.addCache(cache);Cache test = cacheManager.getCache("test");test.put(new Element(1L,"Hello world"));System.out.println(test.get(1L));cacheManager.shutdown();}}
使用XML
public class XmlEhcacheDemo {public static void main(String[] args) throws URISyntaxException {URL resource = XmlEhcacheDemo.class.getClassLoader().getResource("ehcache.xml");CacheManager cacheManager = CacheManager.create(resource);CacheConfiguration cacheConfiguration = new CacheConfiguration("test",10);PersistenceConfiguration persistenceConfiguration = new PersistenceConfiguration();persistenceConfiguration.strategy(PersistenceConfiguration.Strategy.LOCALTEMPSWAP);cacheConfiguration.persistence(persistenceConfiguration);Cache cache = new Cache(cacheConfiguration);cacheManager.addCache(cache);Cache test = cacheManager.getCache("sampleCache");test.put(new Element(10L,"Hello world"));System.out.println(test.get(10L));}}
与Spring的结合
public class SpringEhcacheDemo {public static void main(String[] args) {AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();context.register(SpringEhcacheDemo.class);context.refresh();CacheManager cacheManager = context.getBean(CacheManager.class);Cache springCache = cacheManager.getCache("springCache");springCache.put(1L,"ttttt");Cache.ValueWrapper valueWrapper = springCache.get(1L);System.out.println(valueWrapper.get());EhCacheCacheManager ehCacheCacheManager = (EhCacheCacheManager)cacheManager;ehCacheCacheManager.getCacheManager().shutdown();context.close();}@Beanpublic CacheManager ehCacheCacheManager(){URL resource = XmlEhcacheDemo.class.getClassLoader().getResource("ehcache-spring.xml");EhCacheCacheManager ehCacheCacheManager = new EhCacheCacheManager();net.sf.ehcache.CacheManager cacheManager = net.sf.ehcache.CacheManager.create(resource);ehCacheCacheManager.setCacheManager(cacheManager);return ehCacheCacheManager;}}
github代码地址
文章版权声明:除非注明,否则均为彭超的博客原创文章,转载或复制请以超链接形式并注明出处。
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。