Java Spring Boot基本缓存对我不起作用
我试图做一个简单的数据缓存,但每次我测试它。它似乎从不缓存数据 软件包应用程序; 包app.cache; 软件包app.controllers; 依赖关系Java Spring Boot基本缓存对我不起作用,java,spring-mvc,caching,spring-boot,Java,Spring Mvc,Caching,Spring Boot,我试图做一个简单的数据缓存,但每次我测试它。它似乎从不缓存数据 软件包应用程序; 包app.cache; 软件包app.controllers; 依赖关系 。。。 org.springframework.boot SpringBootStarterWeb org.springframework.boot spring引导启动器数据jpa mysql mysql连接器java org.springframework.boot 弹簧启动安全 org.apache.directory.server a
。。。
org.springframework.boot
SpringBootStarterWeb
org.springframework.boot
spring引导启动器数据jpa
mysql
mysql连接器java
org.springframework.boot
弹簧启动安全
org.apache.directory.server
apacheds服务器jndi
1.5.5
org.springframework
spring上下文
...
当我试图保存数据时,它似乎无法保存数据。当我再次请求时,不会缓存任何数据 之所以会出现这种行为,是因为缓存工作正常。数据已保存,但已缓存
null
:if(userCache.getUserInfo()
为了在设置值时清除缓存,可以使用
@cacheexecute(cacheNames=“books”,allEntries=true)注释setUserInfo
方法
我如何获得它,以便按需更新缓存。我尝试添加CachePut
,但仍然没有成功。我更新了更多代码。为什么set方法不起作用。它跳过了getUserInfo()
因为正如您所说,它已经为空,但它确实设置了值,并且没有保存任何内容。您必须通过使用@cacheexecute
注释setUserInfo
来清除缓存@CachePut
确实会在缓存中放入一些内容,但是setUserInfo
没有参数,因此它将是无用的。您的但它仍然不起作用。我将cacheexecute
添加到setUserInfo
中,但仍然没有任何内容。当我尝试执行getUserInfo
时,它仍然返回null。另一方面,当我在执行第一个getUserInfo()之前设置值时
method。它返回一个值。您很接近。我无法将CacheExit添加到setUserInfo中。相反,我必须创建一个名为Clear的单独方法,并在再次设置值之前调用它。如果有办法,我可以在设置值之前而不是之后进行CacheExit。我将感谢您提供的提示。无论如何,请更新您的答案,使其包含我的建议必须先调用cacheexecute,我会将其标记为正确。
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.concurrent.ConcurrentMapCacheManager;
import org.springframework.context.annotation.Bean;
@EnableAutoConfiguration
@SpringBootApplication
@EnableCaching
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Bean
public CacheManager cacheManager() {
return new ConcurrentMapCacheManager("getUserInfo");
}
}
import app.repo.User.User;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
@Service
public class UserService {
private User userInfo;
@CacheEvict("getUserInfo")
public void setUserInfo(User userInfo) {
this.userInfo = userInfo;
}
@Cacheable("getUserInfo")
public User getUserInfo() {
return userInfo;
}
}
import app.cache.UserService;
import app.repo.User.Player;
import app.repo.User.User;
import app.repo.User.UserRepo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
@RestController
public class UserController {
@Autowired
private UserService userCache;
@RequestMapping("/test-user-cache")
@ResponseBody
public User testUserCache() {
if(userCache.getUserInfo() != null) {
return userCache.getUserInfo();
}
User user = new User("joejoe","wordpass");
user.setId(44444444);
userCache.setUserInfo(user);
return userCache.getUserInfo();
}
}
...
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.apache.directory.server</groupId>
<artifactId>apacheds-server-jndi</artifactId>
<version>1.5.5</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
</dependencies>
...