The previous chapter, explained the [Spring Boot integrated Spring Cache] (https://blog.csdn.net/Simple_Yangger/article/details/102693316), Spring Cache has implemented a variety of caches, including EhCache, RedisCache, and ConcurrentMapCache.
In this section we look at Spring Cache using EhCache.
EhCache usage demonstration
EhCache is a pure Java in-process Cache framework, which is fast and lean. The default Cache used in Hibernate is EhCache.
This section sample is in [Spring Boot integrated Spring Cache] on the basis of the source (https://blog.csdn.net/Simple_Yangger/article/details/102693316). Source code address:
Using EhCache as the cache, we first introduce dependencies.
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <! --> <dependency> <groupId>net.sf.ehcache</groupId> <artifactId> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId> </dependency>Copy the code
Then create the EhCache configuration file ehcache.xml.
<? The XML version = "1.0" encoding = "utf-8"? > <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd" updateCheck="false"> <! - 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="cache" maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="600" overflowToDisk="true" /> </ehcache>Copy the code
The SpringBoot configuration file then specifies the cache type and declares the location of the Ehcache configuration file.
server:
port: 10900
spring:
profiles:
active: dev
cache:
type: ehcache
ehcache:
config: classpath:/ehcache.xmlCopy the code
So you can start using Ehcache, test code and [Spring Boot integrated Spring Cache] (https://blog.csdn.net/Simple_Yangger/article/details/102693316).
SpringBoot Starts the class. @enablecaching Enables the Spring Cache function.
@EnableCaching @SpringBootApplication public class SpringbootApplication { public static void main(String[] args) { String tmpDir = System.getProperty("java.io.tmpdir"); System.out.println(" temporary path: "+ tmpDir); SpringApplication.run(SpringbootApplication.class, args); }}Copy the code
The CacheApi interface invokes classes to facilitate the invocation for testing.
@RestController @RequestMapping("cache") public class CacheApi { @Autowired private CacheService cacheService; @GetMapping("get") public User get(@RequestParam int id){ return cacheService.get(id); } @PostMapping("set") public User set(@RequestParam int id, @RequestParam String code, @RequestParam String name){ User u = new User(code, name); return cacheService.set(id, u); } @DeleteMapping("del") public void del(@RequestParam int id){ cacheService.del(id); }}Copy the code
CacheService CacheService processing class, adding, updating, and deleting caches.
@Slf4j @Service public class CacheService { private Map<Integer, User> dataMap = new HashMap <Integer, User>(){ { for (int i = 1; i < 100 ; i++) { User u = new User("code" + i, "name" + i); put(i, u); }}}; @cacheable (value = "cache", key = "'user:' + #id") public user get(int id){log.info(" id{} ", id); return dataMap.get(id); @cacheput (value = "cache", key = "'user:' + #id") public user set(int id, user u){log.info(" update id{} data ", id); dataMap.put(id, u); return u; @cacheevict (value = "cache", key = "user:' + #id") public void del(int id){log.info(" id{} ", id);} @cacheevict (value = "cache", key =" user:' + #id") public void del(int id){log.info(" id{} ", id); dataMap.remove(id); }}Copy the code
Source code address: