ehcache3 - why cache doesn't expire?

General Tech Bugs & Fixes 2 years ago

2 1 1 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating

Posted on 02 Sep 2022, this text provides information on Bugs & Fixes related to General Tech. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (1)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago

 

I want to cache a few of my dao queries. And it works, but cache doesn't expire. What I'm doing wrong?

My ehcache.xml:

 xmlns='http://www.ehcache.org/v3'
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:jsr107="http://www.ehcache.org/v3/jsr107"
        xsi:schemaLocation="http://www.ehcache.org/v3 http://www.ehcache.org/schema/ehcache-core-3.0.xsd
                            http://www.ehcache.org/v3/jsr107 http://www.ehcache.org/schema/ehcache-107-ext-3.0.xsd">

    <cache alias="test">
    java.lang.String
    xxx.model.SignalValueType
        
             unit="seconds">5
        

        
             unit="entries">100
             unit="MB">10
        
    cache> 

In application.properties I have entry:

spring.cache.jcache.config=classpath:ehcache.xml

In pom:


    javax.cache
    cache-api
    runtime


    org.ehcache
    ehcache

And my query:

@Cacheable(value = "test", key = "#code")
@Override
public SignalValueType getSignalValueType(String code) {         
  SignalValueType signalType = (SignalValueType) sf.getCurrentSession()
.createQuery("FROM SignalValueType svt WHERE svt.code = :code").setParameter("code", code)
.uniqueResult();

 return signalType;
}

I expect 5 seconds expiring, but after this time (and longer) query is still cached. What I'm doing wrong?

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.