标签: spring-cache

以实例变量和参数为键的Spring缓存

我正在使用 ehcache 缓存方法结果。键必须是成员对象和方法参数的组合。我的课程看起来像:

Class A {

private B b;

@Cacheable(value="someCache",key="some key based on B and C")
public Result getResult(C c){
......
}
Run Code Online (Sandbox Code Playgroud)

我需要基于 B 和 C 的密钥。我提到了https://code.google.com/p/ehcache-spring-annotations/issues/detail?id=69但他们没有指定如何包含方法参数在密钥生成中。有人可以帮我解决这个问题吗?

java spring ehcache spring-cache

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

@CacheEvict 和 @TriggersRemove 注解之间的区别

我想在我的 portlet 应用程序中使用 Ehcache。如果我想从缓存中删除数据,最好使用@CacheEvictor @TriggersRemove?

根据文档,@CacheEvict@TriggersRemove注释看起来非常相似。

caching annotations ehcache spring-annotations spring-cache

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

spring缓存确实可以使用嵌套方法

我有一个方法来调用另一个@Cacheable方法,如下所示:

public ItemDO findMethod2(long itemId) {
    this.findMethod1(itemId);
    ...
}

@Cacheable(value = "Item", key="#itemId", unless="#result == null")
public ItemDO findMethod1(long itemId) {
    ...
}
Run Code Online (Sandbox Code Playgroud)

如果我直接调用findMethod1(),缓存效果很好.但是,当我调用findMethod2()时,findMethod1()上的缓存完全被忽略.

这可能是JVM将findMethod1()内联到findMethod2()中的伎俩吗?

有没有人遇到类似的问题?

谢谢!

spring-cache

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

带有key ="#id"的@CacheEvict抛出NullPointerException

我正在尝试使用Spring Caching注释@Cacheable@CacheEvictGuavaCacheManager.

我用这两个测试创建了一个测试用例:

  1. cachesById- 验证对annotatted方法的两次调用是否@Cacheable返回相同的对象
  2. evict- 如果@CacheEvict在这两个调用之间调用了带注释的方法,则验证是否返回了两个不同的实例

当我没有指定密钥时,两者都工作正常@CacheEvict,但是当我得到以下异常时:

java.lang.NullPointerException
    at com.google.common.base.Preconditions.checkNotNull(Preconditions.java:210)
    at com.google.common.cache.LocalCache$LocalManualCache.invalidate(LocalCache.java:4764)
    at org.springframework.cache.guava.GuavaCache.evict(GuavaCache.java:135)
    at org.springframework.cache.interceptor.AbstractCacheInvoker.doEvict(AbstractCacheInvoker.java:95)
    at org.springframework.cache.interceptor.CacheAspectSupport.performCacheEvict(CacheAspectSupport.java:409)
    at org.springframework.cache.interceptor.CacheAspectSupport.processCacheEvicts(CacheAspectSupport.java:392)
    at org.springframework.cache.interceptor.CacheAspectSupport.execute(CacheAspectSupport.java:362)
    at org.springframework.cache.interceptor.CacheAspectSupport.execute(CacheAspectSupport.java:299)
    at org.springframework.cache.interceptor.CacheInterceptor.invoke(CacheInterceptor.java:61)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:653)
    at com.myorg.caching.CacheTest$Repo$$EnhancerBySpringCGLIB$$eed50f3e.update(<generated>)
    at com.myorg.caching.CacheTest.evict(CacheTest.java:50)
Run Code Online (Sandbox Code Playgroud)

这可以通过执行以下测试来再现.

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(
        classes = { Repo.class, CacheTest.SpringConfig.class },
        loader = AnnotationConfigContextLoader.class)
public class CacheTest {

    private static final String CACHE_NAME = "cacheName";

    @Inject
    private Repo repo;

    @Test
    public void cachesById() {
        Entity …
Run Code Online (Sandbox Code Playgroud)

spring-cache google-guava-cache

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

使用springcache只读,如何设置springcacheredis只读

当我在redis中使用spring缓存时,我在两个应用程序中使用它,一个是读写,另一个是只读,我该如何配置?

我尝试这样做,但它不起作用!

@Cacheable(value = "books", key = "#isbn", condition = "false")
Run Code Online (Sandbox Code Playgroud)

有人可以帮忙吗?

spring-cache

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

如何为spring缓存编写自定义拦截器(@cachable)

我正在使用 spring 缓存来缓存数据。现在我想在写入缓存之前加密一些数据并在读取时解密数据。那么有什么方法可以为 @cachable 注释编写自定义拦截器/aop

spring-boot spring-cache

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

Spring Cache - 我如何在除非参数中捕获异常?

我得到以下代码:

@Cacheable(value = "cacheName", key = "#someMap.toString()", unless="#result.error")
public List<Book> methodName(Map<Integer, Integer> someMap) throws BookException {
//...
Run Code Online (Sandbox Code Playgroud)

该方法抛出BookException并且我想避免在发生这种情况时缓存结果。但是当我执行该方法时:

org.springframework.expression.spel.SpelEvaluationException: EL1008E: Property or field 'error' cannot be found on object of type 'java.util.ArrayList' - maybe not public?
Run Code Online (Sandbox Code Playgroud)

java spring ehcache spring-el spring-cache

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

Hazelcast是否支持同步(sync)@Cacheable实现

我想使用 hazelcast 作为 spring 的缓存提供程序。假设我能够配置集群并选择正确的 hazelcast CacheManager 实现。

我想确保 hazelcast 缓存支持“同步”注释属性,如此处所述

我想确保编写像这样的代码

@Cacheable(cacheNames="foos", sync=true)
public Foo executeExpensiveOperation(String id) {...}
Run Code Online (Sandbox Code Playgroud)

...将为我保证条目将被锁定,而缓存值将由 CacheLoader 计算。

现代版本的 hazelcast 支持同步缓存实现吗?

我知道分布式读取 IMap 具有与同步缓存类似的行为。我可以通过按键显式锁定映射条目。我应该使用这些结构作为替代吗?

hazelcast spring-cache hazelcast-imap

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

Spring Boot中函数调用后如何释放所有缓存数据

我正在使用 Spring Boot 2.0.4.RELEASE 版本,仅使用默认缓存提供程序启用缓存,不使用外部缓存提供程序。

我已经安排了一批在每天的特定时间运行。在其运行期间,涉及数据访问的某些方法调用被缓存并且工作正常。

现在我想在下次功能启动之前在预定时间释放所有缓存的项目。

我无法实现此功能。你们能否指导我一些想法或如何实施它的方法。

这就是我正在努力实现的。我有一个标记为 @Configuration 的 JobExecutionListener 类。我正在使用它的 afterJob 方法来清除所有缓存。

@Configuration
@JobScope
public class JobTwoExecutionListener implements JobExecutionListener {

    private static final Logger logger = LoggerFactory.getLogger(JobTwoExecutionListener.class);

    @Autowired
    private CacheManager cacheManager;

    @Override
    public void beforeJob(JobExecution jobExecution) {
        final String methodName = "beforeJob() : ";
        logger.info(methodName + "called");

        if(cacheManager == null) return;

        logger.info(methodName + "CacheManager FOUND. Listing all the caches 
        before Job Run");
        for(String name : cacheManager.getCacheNames()){
            logger.info(methodName + "CACHE_NAME BEFORE JOB " + name);
        }
    }

    @Override …
Run Code Online (Sandbox Code Playgroud)

java spring-batch spring-boot spring-cache

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

从使用@Cacheable注释的方法中清除缓存

我有一个用 @Cacheable 注释的方法。如果方法内部捕获到异常,我希望清除缓存。但是,缓存似乎是在清除缓存的行之后执行的方面加载的。因此,当方法中捕获到异常时,即使清除了缓存,空字符串结果仍保留在缓存中。

我应该从哪里清除缓存?

@Cacheable("myCache") 
public String myMethod() {
    String result="";
    try {
        result = doSomething();
    } catch (Exception e) {
        cacheManager.getCache("myCache").clear();
    }
    return token;
}
Run Code Online (Sandbox Code Playgroud)

java spring caching spring-cache

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

Spring @Cacheable 带过滤器

每个实体类都有 user.id 值,我在所有服务上都有过滤器,它们在数据库级别按 principal.id 和实体 user.id 过滤数据,只需添加 where 子句。我开始使用@Cacheable spring 选项。但是过滤器不适用于 spring-cache。如何从缓存中过滤数据?

@Override
@Cacheable(value = "countries")
public List<Country> getAll() {
    return countryDao.findAll();
}
Run Code Online (Sandbox Code Playgroud)

如果值在缓存中,不同的用户可以访问其他用户的值。

spring spring-mvc ehcache spring-security spring-cache

0
推荐指数
1
解决办法
1624
查看次数

如何在 spring Cacheable 键的方法中指定多个参数

http://docs.spring.io/spring/docs/current/spring-framework-reference/html/cache.html#cache-annotations-cacheable-key 上面的链接显示了当一个默认的缓存键不需要方法。但是如何在 Cacheable 注释中指定多个参数(但不是全部在方法 arg 列表中)作为缓存的键?

spring caching spring-cache

0
推荐指数
1
解决办法
3468
查看次数

用于键的 Spring Cacheable concat 参数

我正在使用 Spring Caching 框架和 redis。以下是我使用的 Cacheable

@Cacheable(value = "oauth2token", key="#value + #type")
public OAuth2Token findOneByValueAndType(String value, String type);
Run Code Online (Sandbox Code Playgroud)

我只是想创建一个我知道的密钥。这给出了错误

14:20:21,199 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/veip-web].[Resteasy]] (http-/0.0.0.0:8080-63) JBWEB000236: Servlet.service() for servlet Resteasy threw exception: org.springframework.expression.spel.SpelEvaluationException: EL1030E:(pos 0): The operator 'ADD' is not supported between objects of type 'null' and 'null'
Run Code Online (Sandbox Code Playgroud)

因为当我没有将 key 指定为流时。

@Cacheable(value = "oauth2token")
public OAuth2Token findOneByValueAndType(String value, String type);
Run Code Online (Sandbox Code Playgroud)

我可以看到生成了一个疯狂的密钥。我需要知道密钥,因为稍后我需要CachePut更新相同的项目。

我也尝试跟随。

@Cacheable(value = "oauth2token", key="#type.contact(#value)")
public OAuth2Token findOneByValueAndType(String value, String type);
Run Code Online (Sandbox Code Playgroud)

Spring 抱怨那个时候#type是空的。

这里有什么问题。

spring-mvc spring-el spring-cache

0
推荐指数
1
解决办法
5999
查看次数