How to view content of Cache in JCache

3k views Asked by At

There are options to read the cache by using keys. Like this :

   Collection<Integer> userIds = context.getUserDao().allUserIds();  
   for (Integer userId : userIds) {
        User user = cache.getUserCache().get(userId);
        System.out.println(user.toString());
    }

With the latter it will load the expired ones to the cache and then display it.

But the requirement is to view all the content currently in the cache.

2

There are 2 answers

0
Shenal On BEST ANSWER

This is how to view all the content of the Cache. Found the method after going through the Java Docs of JCache.

public void printAllCache(){

    Cache<String, String> cache = cacheManager.getCache(CACHENAME, String.class, String.class);

    Iterator<Cache.Entry<String,String>> allCacheEntries= cache.iterator();
    while(allCacheEntries.hasNext()){
        Cache.Entry<String,String> currentEntry = allCacheEntries.next();
        System.out.println("Key: "+currentEntry.getKey()+" Value: "+ currentEntry.getValue());
    }
    return returnProperties;

}
1
Ahsen On