Spring Cache get key from the Value

12,133

Spring Cache and EhCache are two completely different implementations of caching mechanisms. Although there is a way to cast Spring-based cache to EhCache-based one, it doesn't mean Spring provides automatically its implementation. You have to import the EhCache library (using Maven, Gradle, etc.).

Here you go. You get an instance ofnet.sf.ehcache.EhCache which contains all the cache regions from Spring org.springframework.cache.CacheManager.

EhCache cache = (EhCache) CacheManager.getCache("myCache").getNativeCache();

Then, it's not possible to access all the values directly like with the Map. Iterate through the keys and get the particular elements matching the key. This way you iterate through all the values as well.

for (Object key: cache.getKeys()) {
    Element element = cache.get(key);
    if (element != null) {
        Object value = element.getObjectValue();     // here is the value
    }
}

I haven't tested the snippets, however, I hope you get the idea.

Share:
12,133
Anas
Author by

Anas

Learning Java .... Java is love

Updated on July 26, 2022

Comments

  • Anas
    Anas almost 2 years

    I have used spring cache in a spring boot application to store value against a certain key. I now have the value, is it possible to get the key from cache against the value? If so Please help.

    I tried using solutions regarding net.sf.ehcache.Cache but for some reason it doesn't show any import suggestions and gives error net.sf.ehcache.Cache cannot be resolved to a type. I am new to spring cache so have no clue what to do further.

    The dependencies in my project are

    <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-cache</artifactId>
    </dependency>
    
    <dependency>
            <groupId>org.ehcache</groupId>
            <artifactId>ehcache</artifactId>
    </dependency>
    
    <dependency>
            <groupId>javax.cache</groupId>
            <artifactId>cache-api</artifactId>
    </dependency>
    

    The code I am using is

    public String getEmailByOtp(String otp)
    {
        String email = "";
        Ehcache cache = (Ehcache) CacheManager.getCache("otpCache").getNativeCache();
        for (Object key: cache.getKeys()) {
            Element element = cache.get(key);
            if (element != null) {
                Object value = element.getObjectValue();     // here is the value
                if(value.equals(otp)) {
                    email = key.toString();
                }
            }
        }
    
        return email;
    
    }
    
  • Anas
    Anas almost 6 years
    I tried and getting the error Cannot make a static reference to the non-static method getCache(String) from the type CacheManager for now i autowired Cachemanager to have its object ... is this a correct approach?
  • Anas
    Anas almost 6 years
    and the next error is in the for loop its showing to me The method getKeys() is undefined for the type CacheProperties.EhCache
  • Nikolas Charalambidis
    Nikolas Charalambidis almost 6 years
    I haven't tested my code and I should include this information. Firstly, you can't mix the static and autowired content. Secondly, check if the library is properly imported. EhCache has this method: ehcache.org/apidocs/2.9/net/sf/ehcache/Ehcache.html#getKeys(‌​)
  • Anas
    Anas almost 6 years
    I beleive the import is correct ... the part where i cache the value is working fine since the past week i hope i didnt make a mistake in the import, import org.springframework.boot.autoconfigure.cache.CacheProperties‌​.EhCache; this is the import i have in my code
  • Nikolas Charalambidis
    Nikolas Charalambidis almost 6 years
    You need net.sf.ehcache.EhCache.
  • Anas
    Anas almost 6 years
    net.sf.ehcache.EhCache this is not suggest to me by the IDE even though this is the solution i got from every where else. I have added the dependencies in my question can you let me know if they are correct
  • Nikolas Charalambidis
    Nikolas Charalambidis almost 6 years
    Update the project, force to reimport all the dependencies. What dependency have you included to the pom.xml?
  • Nikolas Charalambidis
    Nikolas Charalambidis almost 6 years
    You use org.ehcache but need net.sf.ehcache.EhCache. Try this one: mvnrepository.com/artifact/net.sf.ehcache/ehcache/2.10.5
  • Anas
    Anas almost 6 years
    it seems that my repo was not correct i tried maven install and got an issue of weblogic plugin... resolving them and after that will get back to you thanks for the help
  • Anas
    Anas almost 6 years
    CacheManager.getCache("myCache") at this piece of code i am getting error Cannot make a static reference to the non-static method getCache(String) from the type CacheManager
  • Anas
    Anas almost 6 years
  • Anas
    Anas almost 6 years
    CacheManager.getCache will not work because getCache is not static method. also after autowiring Cachemanager when using the same code i get exception of casting java.lang.ClassCastException: org.ehcache.jsr107.Eh107Cache cannot be cast to net.sf.ehcache.Ehcache