我对Spring Security和Spring Caching有疑问。说我有一个方法,并用@PreAuthorize(“ condition”)和@Cacheable(...)注释了该方法,就像这样
@PreAuthorize("some authorization check")
@Cacheable(....)
public String calculate() {
....
}
Run Code Online (Sandbox Code Playgroud)
@PreAuthorize(http://docs.spring.io/spring-security/site/docs/3.0.x/reference/ns-config.html)是否优先于@Cacheable(http://docs.spring.io/ spring / docs / 3.1.0.M1 / spring-framework-reference / html / cache.html)?框架是否保证将对@PreAuthorize安全检查进行评估,即使calculate()函数的结果已被计算和缓存也是如此?请记住,可以由用户A调用此函数,然后由缓存中存储的值调用,然后另一个用户(用户B)执行需要再次调用此函数的操作?是否会评估@PreAuthorize条件?
从我在Spring文档中可以找到的内容,@ PreAuthorize和@Cacheable的建议顺序都定义为“ Ordered.LOWEST_PRECEDENCE”,我认为这意味着它们的评估顺序是不确定的?
谢谢!
我设置了一个过滤器bean来插入和重置Cache-Control标头.这工作正常,除了在过滤器之后的小点,Cache-Control插入额外的标题.
我正在和我一起工作Spring Boot.什么可能导致问题的解决方案?
@Component
public class CacheControlFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {}
@Override
public void destroy() {}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
Calendar expires = Calendar.getInstance();
expires.add(Calendar.HOUR, 24);
// Intercept response header
HttpServletResponse resp = (HttpServletResponse) response;
resp.setDateHeader("Expires", expires.getTimeInMillis());
resp.setHeader("Cache-Control", "max-age=2048");
chain.doFilter(request, resp);
}
}
Run Code Online (Sandbox Code Playgroud)
查看重复的Cache-Control标头:
HTTP/1.1 200 OK
...
Cache-Control: max-age=2048
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Expires: …Run Code Online (Sandbox Code Playgroud) 我正在使用Spring内置缓存使用@Cacheable和@CachePut注释.
我的@Service中有两个方法,一个用于保存数据库中的值,另一个用于从数据库中获取值.他们都使用缓存.
@CachePut(key = "#code")
MyObject saveMyObject(MyObject o, String code) {
return dao.save(o);
}
@Cacheable(key = "#code")
MyObject getMyObject(String code) {
return dao.getMyObject(code);
}
Run Code Online (Sandbox Code Playgroud)
在保存对象的同时,我想把它放在另一个缓存中,例如
@CachePut(key = "'TMP_'.concat(#code)")
Run Code Online (Sandbox Code Playgroud)
但我不能@CachePut在saveMyObject方法上使用两个注释.
我该怎么办?
我正在将 EhCache 与 spring 一起使用,并且需要公开一个端点,该端点将驱逐给定 EhCache 中的所有元素。但是我找不到任何驱逐所有元素的方法。这是微不足道的,可能已经讨论过了,但我在互联网上找不到任何资源。请提供指点。
我在 Spring Boot 中遇到这个奇怪的问题,@Cacheable它在控制器中工作,但不在服务内部工作。我可以在 Redis 中看到 GET 调用,但看不到 PUT 调用。
这是有效的,因为它位于控制器内部
@RestController
@RequestMapping(value="/places")
public class PlacesController {
private AwesomeService awesomeService;
@Autowired
public PlacesController(AwesomeService awesomeService) {
this.awesomeService = awesomeService;
}
@GetMapping(value = "/search")
@Cacheable(value = "com.example.webservice.controller.PlacesController", key = "#query", unless = "#result != null")
public Result search(@RequestParam(value = "query") String query) {
return this.awesomeService.queryAutoComplete(query);
}
}
Run Code Online (Sandbox Code Playgroud)
但是@Cacheable当我在服务中这样做时不起作用
@Service
public class AwesomeApi {
private final RestTemplate restTemplate = new RestTemplate();
@Cacheable(value = "com.example.webservice.api.AwesomeApi", key = "#query", unless = …Run Code Online (Sandbox Code Playgroud) 有没有人有 Spring Batch (Using Annotation) 的好例子来缓存一个处理器可以访问的引用表?
我只需要一个简单的缓存,运行一个返回一些 byte[] 的查询并将其保存在内存中,直到时间作业执行。
感谢有关此主题的任何帮助。
谢谢 !
我使用身份验证 API 来获取令牌并使用其他服务。此 API 返回令牌和过期时间。可以获取它返回的过期时间并使用这些值设置 expire_after_write 吗?目前这个属性在 application.properties 中,但我担心有一天服务提供者改变了 expire_time 并且我们会因为令牌过期而得到一些错误
当我的应用程序启动时,我尝试用数据初始化我的缓存,但这不起作用。我的代码:
springBoot应用程序
package com.r2b.springcache;
import org.springframework.boot.SpringApplication;
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;
import org.springframework.context.annotation.ComponentScan;
@ComponentScan("com.r2b")
@SpringBootApplication
@EnableCaching
public class SpringCacheApplication {
public static void main(String[] args) {
SpringApplication.run(SpringCacheApplication.class, args);
}
@Bean
public CacheManager cacheManager() {
return new ConcurrentMapCacheManager("student");
}
}
Run Code Online (Sandbox Code Playgroud)
学生
package com.r2b.model;
public class Student {
String id;
String name;
String clz;
public Student(String id, String name, String clz) {
super();
this.id = id;
this.name = name;
this.clz = clz;
}
public String getId() { …Run Code Online (Sandbox Code Playgroud) 我想在应用程序重新启动后使缓存可用,并在配置中添加了以下行:
<disk unit="MB">100</disk>
Run Code Online (Sandbox Code Playgroud)
之后,当我启动应用程序时,我有以下堆栈跟踪:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'cacheManager' defined in class path resource [my/pack/EhcacheConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.cache.CacheManager]: Factory method 'cacheManager' threw exception; nested exception is org.ehcache.StateTransitionException: Cache 'pow_cache' creation in EhcacheManager failed.
at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:627) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:456) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1321) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1160) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:845) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at …Run Code Online (Sandbox Code Playgroud) 我正在为 Spring Cache 使用 Caffeine Cache 库。有没有办法获取所有缓存的密钥?
我当前的应用程序处理近实时数据,流程如下:
在Cache Updater Thread(以固定时间间隔运行,与用户请求无关)中,我需要获取当前缓存中的所有键,从 Db 获取它们的最新数据,然后用于@CachePut更新缓存。
spring-cache ×10
java ×5
spring ×5
spring-boot ×5
caching ×2
ehcache ×2
caffeine ×1
jcache ×1
redis ×1
spring-batch ×1
spring-mvc ×1