标签: spring-cache

拦截org.springframework.cache.interceptor.CacheInterceptor#invoke的spring aop

我已经尝试了以下代码,但它不起作用:

@Component
@Aspect
@Order(Integer.MAX_VALUE)
public class CacheAspect {

    @Around("execution(public * org.springframework.cache.interceptor.CacheInterceptor.invoke(..))")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        //CLASS_CACHE.set(signature.getReturnType());
        return joinPoint.proceed();
    }
}
Run Code Online (Sandbox Code Playgroud)

PS我确定CacheInterceptor是弹簧管理的bean。

java aop spring aspect spring-cache

4
推荐指数
1
解决办法
2452
查看次数

Spring缓存/ spring存储库,驱逐多个密钥

我有两种方法来获取具有两个不同参数的实体.我还有一个使用其中一个参数的保存方法.如何在两个提取键下从缓存中逐出实体?例如见下文:

@Cacheable  
public User getByUsername(String username);

@Cacheable  
public User getByEmail(String email);


@CacheEvict(key="#entity.username")
User save(User entity); 
Run Code Online (Sandbox Code Playgroud)

在上面,对getByEmail的调用将返回过时日期.

spring spring-data spring-cache

4
推荐指数
1
解决办法
5302
查看次数

Spring Boot中的ACL安全性

我在Spring Boot应用程序中通过Java配置设置ACL时遇到问题.我创建了一个小项目来重现问题.

我尝试过几种不同的方法.我遇到的第一个问题是EhCache,在我修复之后(我假设我做了)我再也无法登录了,看起来所有的数据都消失了.

有4个类具有不同的配置:

ACLConfig1.class
ACLConfig2.class
ACLConfig3.class
ACLConfig4.class
Run Code Online (Sandbox Code Playgroud)

所有@PreAuthorize@PostAuthorize注释都按预期工作,除了hasPermission.

控制器拥有4个端点:一个用于用户,一个用于管理员,一个用于公共端,最后一个用于让我头痛 @PostAuthorize("hasPermission(returnObject,'administration')")

我很确定DB中的插入是正确的.这个类是四个中的一个,也是我尝试过的最后一个类:

@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)
public class ACLConfig4 {

@Autowired
DataSource dataSource;


@Bean
public EhCacheBasedAclCache aclCache() {
    return new EhCacheBasedAclCache(aclEhCacheFactoryBean().getObject(), permissionGrantingStrategy(), aclAuthorizationStrategy());
}

@Bean
public EhCacheFactoryBean aclEhCacheFactoryBean() {
    EhCacheFactoryBean ehCacheFactoryBean = new EhCacheFactoryBean();
    ehCacheFactoryBean.setCacheManager(aclCacheManager().getObject());
    ehCacheFactoryBean.setCacheName("aclCache");
    return ehCacheFactoryBean;
}

@Bean
public EhCacheManagerFactoryBean aclCacheManager() {
    return new EhCacheManagerFactoryBean();
}

@Bean
public DefaultPermissionGrantingStrategy permissionGrantingStrategy() {
    ConsoleAuditLogger consoleAuditLogger = new ConsoleAuditLogger();
    return new DefaultPermissionGrantingStrategy(consoleAuditLogger); …
Run Code Online (Sandbox Code Playgroud)

java acl spring-security spring-boot spring-cache

4
推荐指数
1
解决办法
6041
查看次数

无法访问net.sf.ehcache.CacheManager,找不到net.sf.ehcache.CacheManager的类文件

我一直在使用我的项目中实现一些缓存EhCache.我已将以下依赖项添加到我的pom.xml中

<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>4.3.8.RELEASE</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.springframework/spring-context-support -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context-support</artifactId>
    <version>4.3.8.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.ehcache/ehcache -->
<dependency>
    <groupId>org.ehcache</groupId>
    <artifactId>ehcache</artifactId>
    <version>3.3.1</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)

请注意第三个依赖项EhCache.在我在网上找到的所有教程中,组ID都不同.我改变组ID的原因是它被移动到了org.ehcache.

我的classpath中有以下ehcache.xml文件.

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="ehcache.xsd"
         updateCheck="true"
         monitoring="autodetect"
         dynamicConfig="true">

    <diskStore path="java.io.tmpdir"/>

    <cache name="cardTypes"
           maxEntriesLocalHeap="100"
           maxEntriesLocalDisk="1000"
           eternal="false"
           timeToIdleSeconds="300"
           timeToLiveSeconds="600"
           memoryStoreEvictionPolicy="LFU"
           transactionalMode="off">
        <persistence strategy="localTempSwap"/>
    </cache>

</ehcache>
Run Code Online (Sandbox Code Playgroud)

以下是我的配置文件.

import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Bean;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.cache.CacheManager;
import org.springframework.cache.ehcache.EhCacheCacheManager;
import org.springframework.cache.ehcache.EhCacheManagerFactoryBean;
import org.springframework.core.io.ClassPathResource;

@EnableCaching
@Configuration
public class CacheConfig {

    @Bean
    public CacheManager …
Run Code Online (Sandbox Code Playgroud)

terracotta ehcache maven spring-boot spring-cache

4
推荐指数
1
解决办法
6448
查看次数

Spring缓存不适用于findAll方法

我最近开始从一个方法缓存结果.我正在使用@Cacheable和@CachePut来实现所需的功能.

但不知何故,保存操作不会更新findAll方法的缓存.以下是相同的代码段:

@RestController
@RequestMapping(path = "/test/v1")
@CacheConfig(cacheNames = "persons")
public class CacheDemoController {

    @Autowired
    private PersonRepository personRepository;

    @Cacheable
    @RequestMapping(method = RequestMethod.GET, path="/persons/{id}")
    public Person getPerson(@PathVariable(name = "id") long id) {
        return this.personRepository.findById(id);
    }

    @Cacheable
    @RequestMapping(method = RequestMethod.GET, path="/persons")
    public List<Person> findAll() {
        return this.personRepository.findAll();
    }

    @CachePut
    @RequestMapping(method = RequestMethod.POST, path="/save")
    public Person savePerson(@RequestBody Person person) {
        return this.personRepository.save(person);
    }
}
Run Code Online (Sandbox Code Playgroud)

对于第一次调用findAll方法,它将结果存储在"person"缓存中,对于所有后续调用,即使在其间执行了save()操作,它也会返回相同的结果.

我对缓存很新,所以对此的任何建议都会有很大的帮助.

谢谢!

caching spring-cache

4
推荐指数
1
解决办法
2933
查看次数

Redisson CacheConfig 中的 ttl 和 maxIdletime 有什么区别

我试图参考文档,但我仍然不清楚 Redisson 中CacheConfig类中的ttl 和 maxIdleTime 之间的区别。

任何人都可以请说明一下吗?

redis spring-cache redisson

4
推荐指数
1
解决办法
1855
查看次数

在Redis中使用Spring缓存时,在特定时间设置到期密钥

我正在春季缓存抽象注释,以将缓存应用于我的服务方法。

由于我将Redis用作缓存存储,因此我想使用在特定时间使缓存过期的选项,因为Redis支持该选项。Redis中的expireat命令可用于设置将来的到期时间。

我不确定使用RedisCache时如何对属于缓存的键进行操作。

我试图通过创建一个bean来定制RedisCacheManager。

我看到有一个暴露的getNativeCache()方法。但是我没有找到任何方法来设置使用它的expireat的值。

如果有一种自定义RedisCacheManager的方法,以便特定缓存的所有键在到期时使用同一时间,请告诉我。

redis spring-data-redis spring-cache

4
推荐指数
2
解决办法
1849
查看次数

无法在 spring boot 应用程序中为构建器找到名为 xxx 的缓存

我有一个 Spring Boot 应用程序,我想在存储库方法上使用 spring bot 缓存。我在 Spring Boot 应用程序中指定了 @EnableCaching annotaion,当我尝试在我的存储库方法上使用 @Cacheable 注释时,它会引发错误,例如

java.lang.IllegalArgumentException: 无法为 Builder[public abstract java.util.Optional myRepoMethod(java.lang.String,java.lang.String)] caches=[cache] 找到名为“cache”的缓存 键='' | 密钥生成器='' | 缓存管理器='' | cacheResolver='' | 条件='' | 除非='' | sync='false' 在 org.springframework.cache.interceptor.AbstractCacheResolver.resolveCaches(AbstractCacheResolver.java:84) ~[spring-context-5.0.6.RELEASE.jar:5.0.6.RELEASE] 在 org.springframework.cache .interceptor.CacheAspectSupport.getCaches(CacheAspectSupport.java:224) ~[spring-context-5.0.6.RELEASE.jar:5.0.6.RELEASE] 在 org.springframework.cache.interceptor.CacheAspectSupport$CacheOperationContext.(CacheAspectSupport.java) :669) ~[spring-context-5.0.6.RELEASE.jar:5.0.6。

我不知道我错过了哪里!!

我的存储库方法看起来像,

@Cacheable("cache")
Optional<ModelClass> findByUserIdAndProduct(String userId, String product);
Run Code Online (Sandbox Code Playgroud)

spring-boot spring-cache

4
推荐指数
1
解决办法
9979
查看次数

@Cacheable注解,在不同的方法中使用相同的值

我正在尝试使用 Spring @Cacheable 注释。

 @Cacheable(value="users")          
public List<User> findAll() {
    System.out.println("Looking for All users : ");
    return userRepository.findAll();
}

@Override
@Cacheable(value="users")  
public User findOne(String userId) {
    System.out.println("Looking for user : "+ userId);
    return userRepository.findById(userId).get();
}
Run Code Online (Sandbox Code Playgroud)

当我执行第一个方法时,List<User>我得到:

  • 第一次:从数据库中选择所有字段
  • 第二次:从缓存中选择所有字段
  • 第三次:从缓存中选择所有字段

到目前为止这都很好。

当我执行第二种方法时,findOne(String userId)我得到的结果是:

  • 第一次:从数据库中选择特定字段
  • 第二次:从缓存中选择特定字段
  • 第三次:从缓存中选择特定字段

这又好了。

当我执行第一个方法时,List<User>我得到:

从缓存中选择所有字段数据

问题:这两种方法(和 )如何具有相同的缓存名称,但返回不同的结果。List<User>findOne(String userId)

caching ehcache spring-cache cacheapi

4
推荐指数
1
解决办法
2375
查看次数

如何在Spring中访问缓存值

场景:我需要在另一种方法中访问作为一种方法的一部分创建的缓存的值。我该怎么做?

public class Invoice {
  private String invoiced;
  private BigDecimal amount;
  //Getters and Setters
}
Run Code Online (Sandbox Code Playgroud)

方法 1:当客户想要从 UI 获取发票列表时调用

@Cacheable(value="invoices")
public List<Invoice> getAllInvoices(String customerId){
...
//Get all invoices from Database and return 
...
return invoices
}
Run Code Online (Sandbox Code Playgroud)

方法2:当客户点击UI上的下载时调用

public File downloadInvoice(String invoiceId) {
 //TODO: 
 //Validate if invoiceId is present in the cache. This is a validation step
 //How can I access the cache "invoices" here.
 ...
 //if InvoiceId is present in cache then download from db else throw Exception
 return file; …
Run Code Online (Sandbox Code Playgroud)

spring caching spring-mvc spring-boot spring-cache

4
推荐指数
1
解决办法
3万
查看次数