SpringBoot缓存不适用于非参数方法



我有一个弹簧引导应用程序

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;
}
}

在服务类getBookMap((中返回一个Map,我希望spring缓存它。对于随后的请求,它应该从缓存中返回该映射。但在当前的设置中,它没有缓存任何内容,并且每次都在执行getBookMap((方法。我的第一个怀疑是spring-boot-starter-cache版本,但事实并非如此。行为与最新版本相同。

我尝试过使用/不使用键="#root.methodName">,但结果是一样的。

而如果我缓存方法getBook(Integer id(,它运行得很好,这会让我认为没有参数方法的行为不同。

  1. @Cacheable不适用于私有方法,请将您的方法公开
  2. 不能从同一个类中调用可缓存的方法。我的意思是把你的Cacheable方法放到另一个类中并公开它。从另一个类调用该方法。那就行了

Spring使用Spring AOP实现声明性缓存(使用代理(。

当调用通过代理时,用@Cacheable注释getBook方法并从外部调用它。

您在LibraryService类中内部调用getBookMap,该方法不会通过代理,也不会被建议,这意味着不会触发缓存。将该方法提取到另一个类并从LibraryService调用它,或者使用AspectJ

进一步阅读:Spring AOP