如果是,则不执行实际方法就返回缓存的结果。
如果否,则首先执行该方法,并将结果缓存并返回给用户。
缓存声明: 它标识需要缓存的方法。
缓存配置: 用于存储和读取数据的后备缓存。
不经常更改的数据。
经常使用的读取查询,其查询结果在每个调用中至少一段时间没有变化。
内存中缓存
数据库缓存
Web服务器缓存
CDN缓存
缓存 | 缓冲区 |
缓存基于最近最少使用。 | 缓冲区基于先进先出 |
它是页面缓存的大小。 | 它是内存中的原始块I/O缓冲区。 |
它生存了很长时期。 | 它生存了短时期。 |
我们从缓存中读取。 | 我们写入到缓冲区。 |
它存储实际文件数据。 | 它存储文件元数据。 |
它提高了 read 性能。 | 它提高了写入性能。 |
@SpringBootApplication
@EnableCaching
public class SpringBootCachingApplication
{
public static void main(String[] args)
{
SpringApplication.run(SpringBootCachingApplication.class, args);
}
}
@CacheConfig(cacheNames={"employee"})
public class UserService
{
//some code
}
@Caching(evict = {@CacheEvict("phone_number"), @CacheEvict(value="directory", key="#student.id") })
public String getAddress(Student student)
{
//some code
}
@Cacheable(value="cacheStudentInfo", key="#id")
public List studentInfo()
{
//some code
return studentDetails;
}
@Cacheable(value="student", condition="#name.length<20")
public Student findStudent(String name)
{
//some code
}
@CacheEvict(allEntries=true)
@CacheEvict(key="#student.stud_name")
@CacheEvict(value="student_data", allEntries=true) //removing all entries from the cache
public String getNames(Student student)
{
//some code
}
@CachePut(cacheNames="employee", key="#id") //updating cache
public Employee updateEmp(ID id, EmployeeData data)
{
//some code
}
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<?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.3.0.M1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.lidihuo</groupId>
<artifactId>spring-boot-cache-example</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-boot-cache-example</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-cache</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
</pluginRepository>
</pluginRepositories>
</project>
SpringBootCacheExampleApplication.java
package com.lidihuo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
@SpringBootApplication
//enabling caching
@EnableCaching
public class SpringBootCacheExampleApplication
{
public static void main(String[] args)
{
SpringApplication.run(SpringBootCacheExampleApplication.class, args);
}
}
定义三个accountno, customername, acounttype和balance。
使用Constructor生成构造器 。
右键单击文件->源->使用字段生成构造器->全选->生成
生成Getters and Setters。
右键单击文件->源->生成Getter和设置器->全选->生成
package com.lidihuo.model;
public class Customer
{
private int accountno;
private String customername;
private String accounttype;
private double balance;
public Customer(int accountno, String customername, String accounttype, double balance)
{
this.accountno = accountno;
this.customername = customername;
this.accounttype = accounttype;
this.balance = balance;
}
public int getAccountno()
{
return accountno;
}
public void setAccountno(int accountno)
{
this.accountno = accountno;
}
public String getCustomername()
{
return customername;
}
public void setCustomername(String customername)
{
this.customername = customername;
}
public String getAccounttype()
{
return accounttype;
}
public void setAccounttype(String accounttype)
{
this.accounttype = accounttype;
}
public double getBalance()
{
return balance;
}
public void setBalance(double balance)
{
this.balance = balance;
}
}
使用注解 @RestController将类标记为 Controller 。
使用注解 @RequestMapping为控制器定义映射。我们已经定义了映射/customerinfo 。
创建缓存以使用注解 @Cacheable获取数据。 我们已经通过使用注解的 value 属性定义了缓存名称。
我们在中添加了两个客户详细信息
package com.lidihuo.controller;
import java.util.Arrays;
import java.util.List;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.lidihuo.model.Customer;
@RestController
public class CustomerController
{
@RequestMapping("/customerinfo")
//defines a cache for method's return value
@Cacheable(value="customerInfo")
public List customerInformation()
{
System.out.println("customer information from cache");
//adding customer detail in the List
List detail=Arrays.asList(new Customer(5126890,"Charlie Puth","Current A/c", 450000.00),
new Customer(7620015,"Andrew Flintoff","Saving A/c", 210089.00)
);
return detail;
}
}