javascript
Spring Boot文档阅读笔记-EhCache的使用
這里要先注意2個概念:
buffer和cache,很多人會講這兩個概念混用。但其實這是兩個概念!
?
buffer:一般是指存儲臨時數據的實體。只能讀寫一次,對于程序員來說buffer是可見的,比如TCB中,接收tcp數據的buffer。
?
cache:對于程序員來說是不可見的。允許多次獲取相同的數據。這也就是EhCache不叫EhBuffer的原因。
?
程序結構如下:
首先來看下pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.1.10.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.example</groupId><artifactId>demo</artifactId><version>0.0.1-SNAPSHOT</version><name>demo</name><description>Demo project for Spring Boot</description><properties><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-cache</artifactId></dependency><dependency><groupId>org.ehcache</groupId><artifactId>ehcache</artifactId></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><excludes><exclude><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></exclude></excludes></configuration></plugin></plugins></build></project>下面是EhCache的配置
ehcache的配置文件需要放到資源文件下面,如上圖的目錄結構。
新建ehcache.xml內容如下:
<?xml version="1.0" encoding="UTF-8"?> <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"><!-- default cache configurations if no cache configuration is defined --><defaultCache eternal="true" maxElementsInMemory="100"overflowToDisk="false" memoryStoreEvictionPolicy="LFU" /><!-- define our own cache configuration --><cache name="employee" maxElementsInMemory="10000"eternal="false" overflowToDisk="false" memoryStoreEvictionPolicy="LFU" /> </ehcache>上面的xml配置了自定義的緩存配置,還指定了放入緩存的元素的最大個數。以及內存回收策略采用LFU(最不長使用)。并且還設置了如果內存滿了要不要存到磁盤上。
?
下面是在application.properties中指定ehcache.xml文件路徑:
spring.cache.ehcache.config=classpath:ehcache.xml下面創建一個實體類,緩存中保存的就是這個實體類的數據(邏輯上是一個對象,物理上得看EhCache的存儲規則)
Employee.java
@Data public class Employee {private int id;private String name;private String role;public Employee() {}public Employee(int id, String name, String role) {this.id = id;this.name = name;this.role = role;} }下面是緩存服務層的代碼:
@Service public class EmployeeService {@Cacheable(value = "employee")public List<Employee> getListOfEmployees(){System.out.println("getListOfEmployees is running...");List<Employee> employees = new ArrayList<Employee>(4);employees.add(new Employee(1000, "Sumit", "Manager"));employees.add(new Employee(1001, "Souvik", "Java Developer"));employees.add(new Employee(1002, "Liton", "SQl Developer"));employees.add(new Employee(1003, "Debina", "Leader"));return employees;}@Cacheable(value = "employee", key = "#name")public Employee findEmployeeByName(String name, List<Employee> employees) {System.out.println("findEmployeeByName is running...");for (Employee emp : employees) {if (emp.getName().equalsIgnoreCase(name)) {return emp;}}return null;} }@Cacheable:這個注解和ehcache.xml文件對應。如value為employee對應ehcache.xml中的
?
下面是啟動類DemoApplication.java
@EnableCaching @SpringBootApplication public class DemoApplication implements CommandLineRunner {@Autowiredprivate EmployeeService employeeService;public static void main(String[] args) {SpringApplication.run(DemoApplication.class, args);}@Overridepublic void run(String... args) throws Exception {List<Employee> listOfEmployees = employeeService.getListOfEmployees();System.out.println(listOfEmployees);System.out.println("---------------------------------------------------");listOfEmployees = employeeService.getListOfEmployees();System.out.println(listOfEmployees);System.out.println("---------------------------------------------------");Employee employee = employeeService.findEmployeeByName("Sumit", listOfEmployees);System.out.println(employee);System.out.println("---------------------------------------------------");employee = employeeService.findEmployeeByName("Sumit", listOfEmployees);System.out.println(employee);System.out.println("---------------------------------------------------");employee = employeeService.findEmployeeByName("Liton", listOfEmployees);System.out.println(employee);System.out.println("---------------------------------------------------");} }程序運行結果如下:
可見,前兩次調用一樣的數據,第二次是直接從緩存中拿的。
?
?
源碼打包下載地址:
https://github.com/fengfanchen/Java/tree/master/ehcacheDemo
?
?
總結
以上是生活随笔為你收集整理的Spring Boot文档阅读笔记-EhCache的使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Spring Boot文档阅读笔记-Cr
- 下一篇: Qt工作笔记-WebEngineView