Spring Boot 缓存不适用于非参数方法
SpringBoot cache not working for non parameter methods
我有一个 spring 启动应用程序
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.3.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>cachedemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>cachedemo</name>
<description>Demo project for Spring Boot cache</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.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
<!-- <version>2.2.6.RELEASE</version> -->
</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>
</project>
CacheDemoAllpication.java
package com.example.cachedemo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
@SpringBootApplication
@EnableCaching
public class CachedemoApplication {
public static void main(String[] args) {
SpringApplication.run(CachedemoApplication.class, args);
}
}
控制器
package com.example.cachedemo.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.example.cachedemo.model.Book;
import com.example.cachedemo.service.LibraryService;
@Controller
public class HomeController {
@Autowired
private LibraryService service;
@RequestMapping("/get")
public String getBooks(@RequestParam Integer id) {
Book book = service.getBook(id);
System.out.println(book);
return book.toString();
}
}
服务
package com.example.cachedemo.service;
import java.util.HashMap;
import java.util.Map;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import com.example.cachedemo.model.Book;
@Service
public class LibraryService {
// @Cacheable(value = "bookCache")
public Book getBook(Integer id) {
System.out.println("*******************************************Method called getBook()************************************");
Map<Integer,Book> bookMap = getBookMap();
return bookMap.get(id);
}
@Cacheable(value = "booksCache", key="#root.methodName")
private Map<Integer,Book> getBookMap()
{
Book b3 = new Book(2,"Peace","Arif");
Book b2 = new Book(3,"Mumbai City","Shiv Shankar");
Book b1 = new Book(1,"Test","Sunny");
Map<Integer,Book> bookMap = new HashMap<Integer, Book>();
bookMap.put(1, b1);
bookMap.put(2, b2);
bookMap.put(3, b3);
System.out.println("*******************************************Method called getBookMap()************************************");
return bookMap;
}
}
在服务 class getBookMap() return 地图中,我希望 spring 缓存它。对于后续请求,它应该 return 从缓存映射。但是对于当前的设置,它不会缓存任何内容,并且每次都会执行 getBookMap() 方法。
我第一个怀疑是 spring-boot-starter-cache 版本,但事实并非如此。行为也与最新版本相同。
我已经试过了 with/without key="#root.methodName" 但结果是一样的。
而如果我缓存方法 getBook(Integer id) 它工作得很好,这让我认为没有参数方法的行为不同。
- @Cacheable 不适用于私有方法,使您的方法 public
- 您不能从同一个 class 中调用可缓存的方法。我的意思是将您的 Cacheable 方法放入另一个 class 并使其成为 public。从另一个 class 调用该方法。然后就可以了
Spring 使用 Spring AOP 实现声明式缓存(使用代理)。
使用 @Cacheable
注释 getBook
方法并在调用通过代理时从外部调用它。
您正在 LibraryService
class 内部调用 getBookMap
,该方法不会通过代理,也不会被告知,这意味着不会进行缓存触发。将方法提取到另一个 class 并从您的 LibraryService
调用它,或者使用 AspectJ
.
进一步阅读:Spring AOP
我有一个 spring 启动应用程序
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.3.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>cachedemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>cachedemo</name>
<description>Demo project for Spring Boot cache</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.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
<!-- <version>2.2.6.RELEASE</version> -->
</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>
</project>
CacheDemoAllpication.java
package com.example.cachedemo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
@SpringBootApplication
@EnableCaching
public class CachedemoApplication {
public static void main(String[] args) {
SpringApplication.run(CachedemoApplication.class, args);
}
}
控制器
package com.example.cachedemo.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.example.cachedemo.model.Book;
import com.example.cachedemo.service.LibraryService;
@Controller
public class HomeController {
@Autowired
private LibraryService service;
@RequestMapping("/get")
public String getBooks(@RequestParam Integer id) {
Book book = service.getBook(id);
System.out.println(book);
return book.toString();
}
}
服务
package com.example.cachedemo.service;
import java.util.HashMap;
import java.util.Map;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import com.example.cachedemo.model.Book;
@Service
public class LibraryService {
// @Cacheable(value = "bookCache")
public Book getBook(Integer id) {
System.out.println("*******************************************Method called getBook()************************************");
Map<Integer,Book> bookMap = getBookMap();
return bookMap.get(id);
}
@Cacheable(value = "booksCache", key="#root.methodName")
private Map<Integer,Book> getBookMap()
{
Book b3 = new Book(2,"Peace","Arif");
Book b2 = new Book(3,"Mumbai City","Shiv Shankar");
Book b1 = new Book(1,"Test","Sunny");
Map<Integer,Book> bookMap = new HashMap<Integer, Book>();
bookMap.put(1, b1);
bookMap.put(2, b2);
bookMap.put(3, b3);
System.out.println("*******************************************Method called getBookMap()************************************");
return bookMap;
}
}
在服务 class getBookMap() return 地图中,我希望 spring 缓存它。对于后续请求,它应该 return 从缓存映射。但是对于当前的设置,它不会缓存任何内容,并且每次都会执行 getBookMap() 方法。 我第一个怀疑是 spring-boot-starter-cache 版本,但事实并非如此。行为也与最新版本相同。
我已经试过了 with/without key="#root.methodName" 但结果是一样的。
而如果我缓存方法 getBook(Integer id) 它工作得很好,这让我认为没有参数方法的行为不同。
- @Cacheable 不适用于私有方法,使您的方法 public
- 您不能从同一个 class 中调用可缓存的方法。我的意思是将您的 Cacheable 方法放入另一个 class 并使其成为 public。从另一个 class 调用该方法。然后就可以了
Spring 使用 Spring AOP 实现声明式缓存(使用代理)。
使用 @Cacheable
注释 getBook
方法并在调用通过代理时从外部调用它。
您正在 LibraryService
class 内部调用 getBookMap
,该方法不会通过代理,也不会被告知,这意味着不会进行缓存触发。将方法提取到另一个 class 并从您的 LibraryService
调用它,或者使用 AspectJ
.
进一步阅读:Spring AOP