Springboot integrates Ehcache
Cache open
Some basic notes we need to know are:
@enablecache: Enable annotation-based caching. Usually we put it in the main method of application
@cacheable: Cache results for method request parameters
@cachePUT: Ensures method invocation/execution and updates to the cache
@cacheevict: Clear the cache
Pom file
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
Copy the code
@cacheable / @cachePUT / @cacheevict Major parameters
The name of the | explain |
---|---|
value | Cacheable(value= “mycache”) or @cacheable (value={” cache1 “, “cache2”} |
key | Cacheable(value= “testCache”,key= “#id”); Cacheable(value= “testCache”,key= “#id”); |
condition | The cache condition, which can be null, is written using SpEL, returns true or false, and only true is cached/cleared. For example: Testcache @ Cacheable (value = “, “condition =” # userName. The length () > 2 “) |
unless | Deny caching. When the conditional result is TRUE, no caching is performed. Testcache @ Cacheable (value = “, “unless =” # userName. The length () > 2 “) |
allEntries(@CacheEvict ) | CachEvict(value= “testCache”,allEntries=true) |
beforeInvocation(@CacheEvict) | The default value is false. If true, the cache is cleared before the method is executed. By default, the cache is not cleared if an exception is thrown by method execution. @ CachEvict (value = “testcache”, beforeInvocation = true) |
If you need to cache more and more, you can use @cacheconfig (cacheNames = {“myCache”}) annotation to specify the value of the method. You can omit the value, and if you still have a value in your method, the value of the method will be used.
@CacheConfig(cacheNames = {"myCache"}) public class BotRelationServiceImpl implements BotRelationService { @Override @cacheable (key = "targetClass + methodName +#p0")// value public List<BotRelation> findAllLimit(int num) {return botRelationRepository.findAllLimit(num); }... }Copy the code
Integration of Ehcache
pom
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
Copy the code
Yml configuration
Note that config: classpath:/ehcache. XML is not needed because it is the default path. But ehcache.xml must be available. Create ehcache.xml in the Resources directory.
spring:
cache:
type: ehcache
ehcache:
config: classpath:/ehcache.xml
Copy the code
ehcache.xml
<ehcache> <! - Disk storage: Moves the objects that are not used in the cache to disks, similar to the virtual memory in Windows. Path: Specifies the path for storing objects on disks. User.home (user's home directory) user.dir (user's current working directory) java.io. Tmpdir (default temporary directory) Ehcache.disk.store. dir (Ehcache configuration directory) Absolute path (e.g. String tmpDir = system.getProperty ("java.io.tmpdir"); --> <diskStore path="java.io.tmpdir" /> <! MaxElementsInMemory: Sets the maximum number of recorded objects to store in the cache. Eternal: indicates whether the object will never expire TimeToLiveSeconds: maximum retention time/second overflowToDisk: Specifies whether to allow the object to be written to the disk. The following configuration is valid 600 seconds (10 minutes) after the cache is established. If the cache is not accessed for 120 consecutive seconds (2 minutes) within the valid 600 seconds (10 minutes), the cache is invalidated. If there is access, it only lasts 600 seconds. --> <defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="600" timeToLiveSeconds="600" overflowToDisk="true" /> <cache name="myCache" maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="600" overflowToDisk="true" /> </ehcache>Copy the code
Use the cache
@cacheconfig (cacheNames = {” myCache “}) sets the name of ehCache, which must be configured in ehcache.xml.
@CacheConfig(cacheNames = {"myCache"}) public class BotRelationServiceImpl implements BotRelationService { @Cacheable(key = "targetClass + methodName +#p0") public List<BotRelation> findAllLimit(int num) { return botRelationRepository.findAllLimit(num); }}Copy the code