总是执行@cacheable方法并传入timeToLiveSeconds

如何解决总是执行@cacheable方法并传入timeToLiveSeconds

@cacheable方法始终执行并传入timetoLiveSeconds

cache-service.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来解决此问题的唯一方法。对于解决此问题的任何帮助/建议,我将不胜感激。谢谢

更新:基于注释,我更新了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”)

  • 此外,如果我关闭浏览器会话,则无论时间是否到期,它都会调用方法。就像缓存是基于浏览器的缓存还是在服务器上缓存一样,非常令人困惑。抱歉,如果我的问题很基本。

谢谢大家到目前为止的评论

解决方法

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?