标签: spring-cache

春季:@PreAuthorize是否优先于@Cacheable?

我对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”,我认为这意味着它们的评估顺序是不确定的?

谢谢!

spring spring-mvc spring-security spring-cache

2
推荐指数
1
解决办法
760
查看次数

我设置过滤器bean时插入了重复的Cache-Control标头

我设置了一个过滤器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 spring-boot spring-cache

2
推荐指数
1
解决办法
1662
查看次数

Spring @CachePut用两个键放置相同的值

我正在使用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)

但我不能@CachePutsaveMyObject方法上使用两个注释.

我该怎么办?

java spring spring-cache

2
推荐指数
1
解决办法
2162
查看次数

驱逐 EhCache 中的所有元素

我正在将 EhCache 与 spring 一起使用,并且需要公开一个端点,该端点将驱逐给定 EhCache 中的所有元素。但是我找不到任何驱逐所有元素的方法。这是微不足道的,可能已经讨论过了,但我在互联网上找不到任何资源。请提供指点。

java spring ehcache spring-cache

2
推荐指数
1
解决办法
2043
查看次数

@Cacheable 在控制器中工作,但不在服务内部工作

我在 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)

redis spring-boot spring-cache spring-restcontroller

2
推荐指数
1
解决办法
3055
查看次数

带有注释和缓存的 Spring Batch

有没有人有 Spring Batch (Using Annotation) 的好例子来缓存一个处理器可以访问的引用表?

我只需要一个简单的缓存,运行一个返回一些 byte[] 的查询并将其保存在内存中,直到时间作业执行。

感谢有关此主题的任何帮助。

谢谢 !

spring-batch spring-boot spring-cache

2
推荐指数
1
解决办法
3136
查看次数

Spring 缓存动态设置过期时间 - 咖啡因

我使用身份验证 API 来获取令牌并使用其他服务。此 API 返回令牌和过期时间。可以获取它返回的过期时间并使用这些值设置 expire_after_write 吗?目前这个属性在 application.properties 中,但我担心有一天服务提供者改变了 expire_time 并且我们会因为令牌过期而得到一些错误

spring-cache caffeine

2
推荐指数
1
解决办法
2018
查看次数

Spring 启动缓存不适用于 @PostConstruct 或 @AfterPropertiesSet

当我的应用程序启动时,我尝试用数据初始化我的缓存,但这不起作用。我的代码:

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)

java spring caching spring-boot spring-cache

2
推荐指数
1
解决办法
2593
查看次数

IllegalStateException:找不到持久资源的服务:磁盘 [ehCache 3]

我想在应用程序重新启动后使缓存可用,并在配置中添加了以下行:

<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)

java ehcache jcache spring-cache

2
推荐指数
1
解决办法
2449
查看次数

Spring Boot Cache 中的咖啡因缓存:获取所有缓存的键

我正在为 Spring Cache 使用 Caffeine Cache 库。有没有办法获取所有缓存的密钥?

我当前的应用程序处理近实时数据,流程如下:

在此处输入图片说明

Cache Updater Thread(以固定时间间隔运行,与用户请求无关)中,我需要获取当前缓存中的所有键,从 Db 获取它们的最新数据,然后用于@CachePut更新缓存。

java caching spring-boot spring-cache caffeine-cache

2
推荐指数
1
解决办法
5634
查看次数