springboot+Ehcache的简单使用
Posted hhxylm
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了springboot+Ehcache的简单使用相关的知识,希望对你有一定的参考价值。
1 maven
<!--EHcache--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId> </dependency> <dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache</artifactId> </dependency>
2
xml 文件和xsd文件
xsd:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" version="1.7"> <xs:element name="ehcache"> <xs:complexType> <xs:sequence> <xs:element maxOccurs="1" minOccurs="0" ref="diskStore"/> <xs:element maxOccurs="1" minOccurs="0" ref="sizeOfPolicy"/> <xs:element maxOccurs="1" minOccurs="0" ref="transactionManagerLookup"/> <xs:element maxOccurs="1" minOccurs="0" ref="cacheManagerEventListenerFactory"/> <xs:element maxOccurs="1" minOccurs="0" ref="managementRESTService"/> <xs:element maxOccurs="unbounded" minOccurs="0" ref="cacheManagerPeerProviderFactory"/> <xs:element maxOccurs="unbounded" minOccurs="0" ref="cacheManagerPeerListenerFactory"/> <xs:element maxOccurs="1" minOccurs="0" ref="terracottaConfig"/> <xs:element maxOccurs="1" minOccurs="0" ref="defaultCache"/> <xs:element maxOccurs="unbounded" minOccurs="0" ref="cache"/> </xs:sequence> <xs:attribute name="name" use="optional"/> <xs:attribute default="true" name="updateCheck" type="xs:boolean" use="optional"/> <xs:attribute default="autodetect" name="monitoring" type="monitoringType" use="optional"/> <xs:attribute default="true" name="dynamicConfig" type="xs:boolean" use="optional"/> <xs:attribute default="15" name="defaultTransactionTimeoutInSeconds" type="xs:integer" use="optional"/> <xs:attribute default="0" name="maxBytesLocalHeap" type="memoryUnitOrPercentage" use="optional"/> <xs:attribute default="0" name="maxBytesLocalOffHeap" type="memoryUnit" use="optional"/> <xs:attribute default="0" name="maxBytesLocalDisk" type="memoryUnit" use="optional"/> </xs:complexType> </xs:element> <xs:element name="managementRESTService"> <xs:complexType> <xs:attribute name="enabled" type="xs:boolean" use="optional"/> <xs:attribute name="bind" use="optional"/> <xs:attribute name="securityServiceLocation" use="optional"/> <xs:attribute name="securityServiceTimeout" use="optional" type="xs:integer"/> <xs:attribute name="sslEnabled" use="optional" type="xs:boolean"/> <xs:attribute name="needClientAuth" use="optional" type="xs:boolean"/> <xs:attribute name="sampleHistorySize" use="optional" type="xs:integer"/> <xs:attribute name="sampleIntervalSeconds" use="optional" type="xs:integer"/> <xs:attribute name="sampleSearchIntervalSeconds" use="optional" type="xs:integer"/> </xs:complexType> </xs:element> <xs:element name="diskStore"> <xs:complexType> <xs:attribute name="path" use="optional"/> </xs:complexType> </xs:element> <xs:element name="transactionManagerLookup"> <xs:complexType> <xs:attribute name="class" use="required"/> <xs:attribute name="properties" use="optional"/> <xs:attribute name="propertySeparator" use="optional"/> </xs:complexType> </xs:element> <xs:element name="cacheManagerEventListenerFactory"> <xs:complexType> <xs:attribute name="class" use="required"/> <xs:attribute name="properties" use="optional"/> <xs:attribute name="propertySeparator" use="optional"/> </xs:complexType> </xs:element> <xs:element name="cacheManagerPeerProviderFactory"> <xs:complexType> <xs:attribute name="class" use="required"/> <xs:attribute name="properties" use="optional"/> <xs:attribute name="propertySeparator" use="optional"/> </xs:complexType> </xs:element> <xs:element name="cacheManagerPeerListenerFactory"> <xs:complexType> <xs:attribute name="class" use="required"/> <xs:attribute name="properties" use="optional"/> <xs:attribute name="propertySeparator" use="optional"/> </xs:complexType> </xs:element> <xs:element name="terracottaConfig"> <xs:complexType> <xs:sequence> <xs:element maxOccurs="1" minOccurs="0" name="tc-config"> <xs:complexType> <xs:sequence> <xs:any maxOccurs="unbounded" minOccurs="0" processContents="skip"/> </xs:sequence> </xs:complexType> </xs:element> </xs:sequence> <xs:attribute default="localhost:9510" name="url" use="optional"/> <xs:attribute name="rejoin" type="xs:boolean" use="optional" default="false"/> <xs:attribute name="wanEnabledTSA" type="xs:boolean" use="optional" default="false"/> </xs:complexType> </xs:element> <!-- add clone support for addition of cacheExceptionHandler. Important! --> <xs:element name="defaultCache"> <xs:complexType> <xs:sequence> <xs:element minOccurs="0" maxOccurs="unbounded" ref="cacheEventListenerFactory"/> <xs:element minOccurs="0" maxOccurs="unbounded" ref="cacheExtensionFactory"/> <xs:element minOccurs="0" maxOccurs="unbounded" ref="cacheLoaderFactory"/> <xs:element minOccurs="0" maxOccurs="unbounded" ref="cacheDecoratorFactory"/> <xs:element minOccurs="0" maxOccurs="1" ref="bootstrapCacheLoaderFactory"/> <xs:element minOccurs="0" maxOccurs="1" ref="cacheExceptionHandlerFactory"/> <xs:element minOccurs="0" maxOccurs="1" ref="pinning"/> <xs:element minOccurs="0" maxOccurs="1" ref="terracotta"/> <xs:element minOccurs="0" maxOccurs="1" ref="cacheWriter"/> <xs:element minOccurs="0" maxOccurs="1" ref="copyStrategy"/> <xs:element minOccurs="0" maxOccurs="1" ref="elementValueComparator"/> <xs:element minOccurs="0" maxOccurs="1" ref="sizeOfPolicy"/> <xs:element minOccurs="0" maxOccurs="1" ref="persistence"/> </xs:sequence> <xs:attribute name="diskExpiryThreadIntervalSeconds" type="xs:integer" use="optional"/> <xs:attribute name="diskSpoolBufferSizeMB" type="xs:integer" use="optional"/> <xs:attribute name="diskPersistent" type="xs:boolean" use="optional"/> <xs:attribute name="diskAccessStripes" type="xs:integer" use="optional" default="1"/> <xs:attribute name="eternal" type="xs:boolean" use="optional" default="false"/> <xs:attribute name="maxElementsInMemory" type="xs:nonNegativeInteger" use="optional"/> <xs:attribute name="maxEntriesLocalHeap" type="xs:nonNegativeInteger" use="optional"/> <xs:attribute name="clearOnFlush" type="xs:boolean" use="optional"/> <xs:attribute name="memoryStoreEvictionPolicy" type="xs:string" use="optional"/> <xs:attribute name="overflowToDisk" type="xs:boolean" use="optional"/> <xs:attribute name="timeToIdleSeconds" type="xs:nonNegativeInteger" use="optional"/> <xs:attribute name="timeToLiveSeconds" type="xs:nonNegativeInteger" use="optional"/> <xs:attribute name="maxElementsOnDisk" type="xs:nonNegativeInteger" use="optional"/> <xs:attribute name="maxEntriesLocalDisk" type="xs:nonNegativeInteger" use="optional"/> <xs:attribute name="transactionalMode" type="transactionalMode" use="optional" default="off"/> <xs:attribute name="statistics" type="xs:boolean" use="optional" default="false"/> <xs:attribute name="copyOnRead" type="xs:boolean" use="optional" default="false"/> <xs:attribute name="copyOnWrite" type="xs:boolean" use="optional" default="false"/> <xs:attribute name="cacheLoaderTimeoutMillis" type="xs:integer" use="optional" default="0"/> <xs:attribute name="overflowToOffHeap" type="xs:boolean" use="optional" default="false"/> <xs:attribute name="maxMemoryOffHeap" type="xs:string" use="optional"/> </xs:complexType> </xs:element> <xs:element name="cache"> <xs:complexType> <xs:sequence> <xs:element minOccurs="0" maxOccurs="unbounded" ref="cacheEventListenerFactory"/> <xs:element minOccurs="0" maxOccurs="unbounded" ref="cacheExtensionFactory"/> <xs:element minOccurs="0" maxOccurs="unbounded" ref="cacheLoaderFactory"/> <xs:element minOccurs="0" maxOccurs="unbounded" ref="cacheDecoratorFactory"/> <xs:element minOccurs="0" maxOccurs="1" ref="bootstrapCacheLoaderFactory"/> <xs:element minOccurs="0" maxOccurs="1" ref="cacheExceptionHandlerFactory"/> <xs:element minOccurs="0" maxOccurs="1" ref="pinning"/> <xs:element minOccurs="0" maxOccurs="1" ref="terracotta"/> <xs:element minOccurs="0" maxOccurs="1" ref="cacheWriter"/> <xs:element minOccurs="0" maxOccurs="1" ref="copyStrategy"/> <xs:element minOccurs="0" maxOccurs="1" ref="searchable"/> <xs:element minOccurs="0" maxOccurs="1" ref="elementValueComparator"/> <xs:element minOccurs="0" maxOccurs="1" ref="sizeOfPolicy"/> <xs:element minOccurs="0" maxOccurs="1" ref="persistence"/> </xs:sequence> <xs:attribute name="diskExpiryThreadIntervalSeconds" type="xs:integer" use="optional"/> <xs:attribute name="diskSpoolBufferSizeMB" type="xs:integer" use="optional"/> <xs:attribute name="diskPersistent" type="xs:boolean" use="optional"/> <xs:attribute name="diskAccessStripes" type="xs:integer" use="optional" default="1"/> <xs:attribute name="eternal" type="xs:boolean" use="optional" default="false"/> <xs:attribute name="maxElementsInMemory" type="xs:nonNegativeInteger" use="optional"/> <xs:attribute name="maxEntriesLocalHeap" type="xs:nonNegativeInteger" use="optional"/> <xs:attribute name="memoryStoreEvictionPolicy" type="xs:string" use="optional"/> <xs:attribute name="clearOnFlush" type="xs:boolean" use="optional"/> <xs:attribute name="name" type="xs:string" use="required"/> <xs:attribute name="overflowToDisk" type="xs:boolean" use="optional"/> <xs:attribute name="timeToIdleSeconds" type="xs:nonNegativeInteger" use="optional"/> <xs:attribute name="timeToLiveSeconds" type="xs:nonNegativeInteger" use="optional"/> <xs:attribute name="maxElementsOnDisk" type="xs:nonNegativeInteger" use="optional"/> <xs:attribute name="maxEntriesLocalDisk" type="xs:nonNegativeInteger" use="optional"/> <xs:attribute name="maxEntriesInCache" type="xs:nonNegativeInteger" use="optional"/> <xs:attribute name="transactionalMode" type="transactionalMode" use="optional" default="off"/> <xs:attribute name="statistics" type="xs:boolean" use="optional" default="false"/> <xs:attribute name="copyOnRead" type="xs:boolean" use="optional" default="false"/> <xs:attribute name="copyOnWrite" type="xs:boolean" use="optional" default="false"/> <xs:attribute name="logging" type="xs:boolean" use="optional" default="false"/> <xs:attribute name="cacheLoaderTimeoutMillis" type="xs:integer" use="optional" default="0"/> <xs:attribute name="overflowToOffHeap" type="xs:boolean" use="optional" default="false"/> <xs:attribute name="maxMemoryOffHeap" type="xs:string" use="optional"/> <xs:attribute default="0" name="maxBytesLocalHeap" type="memoryUnitOrPercentage" use="optional"/> <xs:attribute default="0" name="maxBytesLocalOffHeap" type="memoryUnitOrPercentage" use="optional"/> <xs:attribute default="0" name="maxBytesLocalDisk" type="memoryUnitOrPercentage" use="optional"/> </xs:complexType> </xs:element> <xs:element name="cacheEventListenerFactory"> <xs:complexType> <xs:attribute name="class" use="required"/> <xs:attribute name="properties" use="optional"/> <xs:attribute name="propertySeparator" use="optional"/> <xs:attribute name="listenFor" use="optional" type="notificationScope" default="all"/> </xs:complexType> </xs:element> <xs:element name="bootstrapCacheLoaderFactory"> <xs:complexType> <xs:attribute name="class" use="required"/> <xs:attribute name="properties" use="optional"/> <xs:attribute name="propertySeparator" use="optional"/> </xs:complexType> </xs:element> <xs:element name="cacheExtensionFactory"> <xs:complexType> <xs:attribute name="class" use="required"/> <xs:attribute name="properties" use="optional"/> <xs:attribute name="propertySeparator" use="optional"/> </xs:complexType> </xs:element> <xs:element name="cacheExceptionHandlerFactory"> <xs:complexType> <xs:attribute name="class" use="required"/> <xs:attribute name="properties" use="optional"/> <xs:attribute name="propertySeparator" use="optional"/> </xs:complexType> </xs:element> <xs:element name="cacheLoaderFactory"> <xs:complexType> <xs:attribute name="class" use="required"/> <xs:attribute name="properties" use="optional"/> <xs:attribute name="propertySeparator" use="optional"/> </xs:complexType> </xs:element> <xs:element name="cacheDecoratorFactory"> <xs:complexType> <xs:attribute name="class" use="required"/> <xs:attribute name="properties" use="optional"/> <xs:attribute name="propertySeparator" use="optional"/> </xs:complexType> </xs:element> <xs:element name="searchAttribute"> <xs:complexType> <xs:attribute name="name" use="required" type="xs:string"/> <xs:attribute name="expression" type="xs:string"/> <xs:attribute name="class" type="xs:string"/> <xs:attribute name="type" type="xs:string" use="optional"/> <xs:attribute name="properties" use="optional"/> <xs:attribute name="propertySeparator" use="optional"/> </xs:complexType> </xs:element> <xs:element name="searchable"> <xs:complexType> <xs:sequence> <xs:element minOccurs="0" maxOccurs="unbounded" ref="searchAttribute"/> </xs:sequence> <xs:attribute name="keys" use="optional" type="xs:boolean" default="true"/> <xs:attribute name="values" use="optional" type="xs:boolean" default="true"/> <xs:attribute name="allowDynamicIndexing" use="optional" type="xs:boolean" default="false"/> </xs:complexType> </xs:element> <xs:element name="pinning"> <xs:complexType> <xs:attribute name="store" use="required" type="pinningStoreType"/> </xs:complexType> </xs:element> <xs:element name="terracotta"> <xs:complexType> <xs:sequence> <xs:element minOccurs="0" maxOccurs="1" ref="nonstop"/> </xs:sequence> <xs:attribute name="clustered" use="optional" type="xs:boolean" default="true"/> <xs:attribute name="coherentReads" use="optional" type="xs:boolean" default="true"/> <xs:attribute name="localKeyCache" use="optional" type="xs:boolean" default="false"/> <xs:attribute name="localKeyCacheSize" use="optional" type="xs:positiveInteger" default="300000"/> <xs:attribute name="orphanEviction" use="optional" type="xs:boolean" default="true"/> <xs:attribute name="orphanEvictionPeriod" use="optional" type="xs:positiveInteger" default="4"/> <xs:attribute name="copyOnRead" use="optional" type="xs:boolean" default="false"/> <xs:attribute name="coherent" use="optional" type="xs:boolean" default="false"/> <xs:attribute name="consistency" use="optional" type="consistencyType" default="eventual"/> <xs:attribute name="synchronousWrites" use="optional" type="xs:boolean" default="false"/> <xs:attribute name="concurrency" use="optional" type="xs:nonNegativeInteger" default="0"/> <xs:attribute name="localCacheEnabled" use="optional" type="xs:boolean" default="true"/> <xs:attribute name="compressionEnabled" use="optional" type="xs:boolean" default="false"/> </xs:complexType> </xs:element> <xs:simpleType name="consistencyType"> <xs:restriction base="xs:string"> <xs:enumeration value="strong"/> <xs:enumeration value="eventual"/> </xs:restriction> </xs:simpleType> <xs:element name="nonstop"> <xs:complexType> <xs:sequence> <xs:element minOccurs="0" maxOccurs="1" ref="timeoutBehavior"/> </xs:sequence> <xs:attribute name="enabled" use="optional" type="xs:boolean" default="true"/> <xs:attribute name="immediateTimeout" use="optional" type="xs:boolean" default="false"/> <xs:attribute name="timeoutMillis" use="optional" type="xs:positiveInteger" default="30000"/> <xs:attribute name="searchTimeoutMillis" use="optional" type="xs:positiveInteger" default="30000"/> </xs:complexType> </xs:element> <xs:element name="timeoutBehavior"> <xs:complexType> <xs:attribute name="type" use="optional" type="timeoutBehaviorType" default="exception"/> <xs:attribute name="properties" use="optional" default=""/> <xs:attribute name="propertySeparator" use="optional" default=","/> </xs:complexType> </xs:element> <xs:simpleType name="timeoutBehaviorType"> <xs:restriction base="xs:string"> <xs:enumeration value="noop"/> <xs:enumeration value="exception"/> <xs:enumeration value="localReads"/> <xs:enumeration value="localReadsAndExceptionOnWrite"/> </xs:restriction> </xs:simpleType> <xs:simpleType name="monitoringType"> <xs:restriction base="xs:string"> <xs:enumeration value="autodetect"/> <xs:enumeration value="on"/> <xs:enumeration value="off"/> </xs:restriction> </xs:simpleType> <xs:simpleType name="pinningStoreType"> <xs:restriction base="xs:string"> <xs:enumeration value="localMemory"/> <xs:enumeration value="inCache"/> </xs:restriction> </xs:simpleType> <xs:simpleType name="terracottaCacheValueType"> <xs:restriction base="xs:string"> <xs:enumeration value="serialization"/> <xs:enumeration value="identity"/> </xs:restriction> </xs:simpleType> <xs:simpleType name="transactionalMode"> <xs:restriction base="xs:string"> <xs:enumeration value="off"/> <xs:enumeration value="xa_strict"/> <xs:enumeration value="xa"/> <xs:enumeration value="local"/> </xs:restriction> </xs:simpleType> <xs:element name="cacheWriter"> <xs:complexType> <xs:sequence> <xs:element minOccurs="0" maxOccurs="1" ref="cacheWriterFactory"/> </xs:sequence> <xs:attribute name="writeMode" use="optional" type="writeModeType" default="write-through"/> <xs:attribute name="notifyListenersOnException" use="optional" type="xs:boolean" default="false"/> <xs:attribute name="minWriteDelay" use="optional" type="xs:nonNegativeInteger" default="1"/> <xs:attribute name="maxWriteDelay" use="optional" type="xs:nonNegativeInteger" default="1"/> <xs:attribute name="rateLimitPerSecond" use="optional" type="xs:nonNegativeInteger" default="0"/> <xs:attribute name="writeCoalescing" use="optional" type="xs:boolean" default="false"/> <xs:attribute name="writeBatching" use="optional" type="xs:boolean" default="false"/> <xs:attribute name="writeBatchSize" use="optional" type="xs:positiveInteger" default="1"/> <xs:attribute name="retryAttempts" use="optional" type="xs:nonNegativeInteger" default="0"/> <xs:attribute name="retryAttemptDelaySeconds" use="optional" type="xs:nonNegativeInteger" default="1"/> <xs:attribute name="writeBehindConcurrency" use="optional" type="xs:nonNegativeInteger" default="1"/> <xs:attribute name="writeBehindMaxQueueSize" use="optional" type="xs:nonNegativeInteger" default="0"/> </xs:complexType> </xs:element> <xs:simpleType name="writeModeType"> <xs:restriction base="xs:string"> <xs:enumeration value="write-through"/> <xs:enumeration value="write-behind"/> </xs:restriction> </xs:simpleType> <xs:element name="cacheWriterFactory"> <xs:complexType> <xs:attribute name="class" use="required"/> <xs:attribute name="properties" use="optional"/> <xs:attribute name="propertySeparator" use="optional"/> </xs:complexType> </xs:element> <xs:element name="copyStrategy"> <xs:complexType> <xs:attribute name="class" use="required" type="xs:string"/> </xs:complexType> </xs:element> <xs:element name="elementValueComparator"> <xs:complexType> <xs:attribute name="class" use="required" type="xs:string"/> </xs:complexType> </xs:element> <xs:element name="sizeOfPolicy"> <xs:complexType> <xs:attribute name="maxDepth" use="required" type="xs:integer"/> <xs:attribute name="maxDepthExceededBehavior" use="optional" default="continue" type="maxDepthExceededBehavior"/> </xs:complexType> </xs:element> <xs:element name="persistence"> <xs:complexType> <xs:attribute name="strategy" use="required" type="persistenceStrategy"/> <xs:attribute name="synchronousWrites" use="optional" default="false" type="xs:boolean"/> </xs:complexType> </xs:element> <xs:simpleType name="persistenceStrategy"> <xs:restriction base="xs:string"> <xs:enumeration value="localTempSwap"/> <xs:enumeration value="localRestartable"/> <xs:enumeration value="none"/> <xs:enumeration value="distributed"/> </xs:restriction> </xs:simpleType> <xs:simpleType name="maxDepthExceededBehavior"> <xs:restriction base="xs:string"> <xs:enumeration value="continue"/> <xs:enumeration value="abort"/> </xs:restriction> </xs:simpleType> <xs:simpleType name="notificationScope"> <xs:restriction base="xs:string"> <xs:enumeration value="local"/> <xs:enumeration value="remote"/> <xs:enumeration value="all"/> </xs:restriction> </xs:simpleType> <xs:simpleType name="memoryUnit"> <xs:restriction base="xs:token"> <xs:pattern value="[0-9]+[bBkKmMgG]?"/> </xs:restriction> </xs:simpleType> <xs:simpleType name="memoryUnitOrPercentage"> <xs:restriction base="xs:token"> <xs:pattern value="([0-9]+[bBkKmMgG]?|100%|[0-9]1,2%)"/> </xs:restriction> </xs:simpleType> </xs:schema>
xml:配置缓存名字
<?xml version="1.0" encoding="UTF-8"?> <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="Ehcache.xsd"> <!--timeToIdleSeconds 当缓存闲置n秒后销毁 --> <!--timeToLiveSeconds 当缓存存活n秒后销毁 --> <!-- 缓存配置 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:内存数量最大时是否清除。 --> <!-- 磁盘缓存位置 --> <diskStore path="java.io.tmpdir" /> <!-- 默认缓存 --> <defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" maxElementsOnDisk="10000000" diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU"> <persistence strategy="localTempSwap" /> </defaultCache> <!-- 测试 --> <cache name="MyTest" eternal="false" timeToIdleSeconds="2400" timeToLiveSeconds="2400" maxEntriesLocalHeap="10000" maxEntriesLocalDisk="10000000" diskExpiryThreadIntervalSeconds="120" overflowToDisk="false" memoryStoreEvictionPolicy="LRU"> </cache> <!-- MM --> <cache name="scripts" eternal="false" timeToIdleSeconds="1200" timeToLiveSeconds="1200" maxEntriesLocalHeap="10000" maxEntriesLocalDisk="10000000" diskExpiryThreadIntervalSeconds="120" overflowToDisk="false" memoryStoreEvictionPolicy="LRU"> </cache> </ehcache>
3 代码中使用
ps controller层调用方法,如果缓存中查到数据则不进入方法直接返回数据
参考代码
package com.cloud.cloudClient02.Service; import com.cloud.cloudClient02.controller.CurrentScript; import org.springframework.cache.annotation.CacheConfig; import org.springframework.cache.annotation.CacheEvict; import org.springframework.cache.annotation.CachePut; import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Repository; import java.util.HashMap; import java.util.Map; @Repository @CacheConfig(cacheNames="scripts") public class CacheDaoImpl @Cacheable(key="#user.getId()") public CurrentScript save(CurrentScript user) System.out.println("save()被执行了...."); return user; @Cacheable(key="#userId") public CurrentScript select(String userId) System.out.println("select()执行了============="); return new CurrentScript(); /*@CachePut(key="#user.getId()") public CurrentScript update(CurrentScript user) System.out.println("update()执行了============="); map.put(user.getId(), user); return user; @CacheEvict(key="#user.getId()") public boolean delete(CurrentScript user) System.out.println("delete()执行了============="); map.remove(user.getId()); return true; @CacheEvict(allEntries=true,value= "MyTest","MM") public void deleteAll() map.clear(); @Cacheable(value="scripts") public String selectFromOtherCache() System.out.println("其他缓存"); return "来自缓存"; */ /* @Cacheable(allEntries=true) public String selectAll() return */
以上是关于springboot+Ehcache的简单使用的主要内容,如果未能解决你的问题,请参考以下文章
SpringBoot 使用 EhCache2.x 缓存(三十一)