【问题标题】:@cacheable method is always executed and ingoing timeToLiveSeconds@cacheable 方法总是被执行并且进入 timeToLiveSeconds
【发布时间】:2020-10-01 00:05:08
【问题描述】:

@cacheable 方法总是被执行并且进入 timeToLiveSeconds

缓存服务.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:ehcache="http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring"
    xsi:schemaLocation="
     http://www.springframework.org/schema/beans
     http://www.springframework.org/schema/beans/spring-beans.xsd
     http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring
     http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/ehcache-spring-1.1.xsd">
    <ehcache:annotation-driven create-missing-caches="true" cache-manager="cacheManager" />
    <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
        <property name="shared" value="true" />
    </bean>
    <bean id="cacheKeyGenerator" class="com.test.StringArgumentCacheKeyGenerator" />
</beans>

pom.xml

<properties>
    <jdk.version>1.8</jdk.version>
    <spring.framework.version>5.1.6.RELEASE</spring.framework.version>
    <spring.security.version>3.2.9.RELEASE</spring.security.version>
    <spring.integration.version>5.1.4.RELEASE</spring.integration.version>
</properties>


    <dependency>
        <groupId>com.googlecode.ehcache-spring-annotations</groupId>
        <artifactId>ehcache-spring-annotations</artifactId>
        <version>1.1.3</version>
    </dependency>
 

ehcache.xml

<cache name="myList" 
        overflowToDisk="false"  statistics="true"
        eternal="false"
        maxElementsInMemory="10000"
        timeToIdleSeconds="0"
        timeToLiveSeconds="300"
        memoryStoreEvictionPolicy="LFU" />

Java 类:每次访问显示部门的页面时都会调用 getDepartmentsList()。 我将系统添加到“我正在获取部门”以进行调试。 timeToLiveSeconds 设置为 5 分钟。所以我希望如果我在 5 分钟内刷新页面,我不会看到打印语句“我正在获取部门”。

  public class MyClass
  {
    @Cacheable(cacheName = "myList", keyGeneratorName = "cacheKeyGenerator" )
    public final Map<String, String> getDepartmentsList(String userid) {
        System.out.println("I am getting the dept");
        Map<String, String> deptMap = myService.getAuthorizedDept(userid);
        return deptMap;
    }
 }

我是 spring ehcache 的新手,调试这并不容易。我能够通过添加 system.out 来解决此问题的唯一方法。我将不胜感激有关如何解决此问题的任何帮助/建议。谢谢

更新:基于 cmets 我更新了 java 类,我看到以下内容:

  public class MyClass
  {
  
    public final Map<String, String> getDepartmentsList(String userid) {
        System.out.println("I am getting the dept" );
        Map<String, String> deptMap = myService.getAuthorizedDept(userid);
        return deptMap;
    }
 }

@Service 
public class Myservice
{
@Cacheable(cacheName = "myList", keyGeneratorName = "cacheKeyGenerator" )
    public Map<String, String> getDepartmentsList(String userid){
    }
}
  • 根据建议的更新代码,该方法不会经常调用,但会在 5 分钟之前得到调用。我只是在刷新浏览器。
  • 我收到部门 2020/09/30 19:53:43
  • 我收到部门 2020/09/30 19:55:47

基于以上,2分钟刷新

  • 我收到部门 2020/09/30 20:10:56
  • 我收到部门 2020/09/30 20:12:57

基于上述,它在 2 分钟内刷新,但时间设置为 5 分钟(timeToLiveSeconds="300")

  • 另外,如果我关闭浏览器会话,无论时间是否过期,它都会调用该方法。非常混乱,好像缓存是基于浏览器的,或者它是在服务器上缓存的。抱歉,如果我的问题非常基本。

感谢大家迄今为止的cmets。

【问题讨论】:

  • MyClass 是 Spring bean 吗?您是从另一个 Spring bean 还是从同一类中的另一个方法调用可缓存方法?
  • 为什么在 2020 年使用 Spring XML 配置?这是你在网上找到的吗?
  • 放弃谷歌缓存的东西并使用 Spring 缓存。您当前正在配置 google 缓存注释,但正在使用 Spring 实现。这显然行不通。所以删除 google 的东西并使用 Spring 的东西。
  • 对于 spring 配置而不是 google,你能给我一个 pom.xml 的例子,说明要包含哪个依赖项。我猜如果我使用 spring 而不是 google,我的 java 实现不会改变。感谢您的帮助。

标签: java spring ehcache


【解决方案1】:

首先你应该使用@Component 或@Service 将MyClass 作为一个Bean。 然后在另一个 spring 对象(例如 Controller 或 RestController)中调用方法 getDepartmentsList()。如果在同一个类中调用该方法,缓存将无效。

【讨论】:

  • 感谢您的建议。使用建议的更新代码,该方法不会经常调用,但会在 5 分钟之前调用。我只是在刷新浏览器。我收到了部门 2020/09/30 19:53:43 我收到了部门 2020/09/30 19:55:47 根据您的建议,我在这篇文章中添加了更新的代码。
猜你喜欢
  • 2013-04-02
  • 2012-05-07
  • 1970-01-01
  • 2015-11-23
  • 2020-06-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-02
相关资源
最近更新 更多