gpt4 book ai didi

java - Spring 在运行时缓存 : Enable/Disable Cache via application. 属性

转载 作者:行者123 更新时间:2023-12-01 06:03:17 28 4
gpt4 key购买 nike

鉴于我有一些可缓存的方法,是否可以通过更改 application.properties 文件中的属性来实时启用/禁用其使用?例如:

我的 application.properties 文件有:UseBookCache=true

@Cacheable(cacheNames="books", condition = "${UseBookCache}")
public Book getByIsbn(String isbn){
//Return book
}

我想通过设置 UseBookCache=false 在运行时禁用缓存

我不想禁用所有缓存,只想禁用此特定实例。这是满足我需要的最佳方式吗?

最佳答案

我不建议在运行时禁用此类功能,因为很难追踪发生的情况(除非您记录配置和结果),并且它会极大地改变应用程序的行为。

注释的属性值无法在运行时更改,需要重新启动应用程序。

另请注意,如果缓存有给定参数的条目,则该方法将不会被调用,也无法记录任何内容。

该注解可以读取一个参数值来决定是否使用缓存。 This other SO answer可能有帮助:

One work around for this You can make your method conditional for cache on the base of parameter. The cache annotations support such functionality through the condition parameter which takes a SpEL expression that is evaluated to either true or false.

@Cacheable(cacheNames="book", condition="#cached == false") public Book findBook(boolean cached)

As explained in documentation. https://docs.spring.io/spring/docs/current/spring-framework-reference/html/cache.html

关于java - Spring 在运行时缓存 : Enable/Disable Cache via application. 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51937309/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com