clearing cache using cacheManager in java
Asked Answered
D

4

6

I want to clear the cache using JAVA code.

and for this goal I write this code:

public void clearCache(){
        CacheManager.getInstance().clearAll();
    }

is this code correct? and is there a way to confirm that it works good? thanks

Dybbuk answered 9/9, 2013 at 7:55 Comment(0)
P
6

Yes, your code cleares all caches you have in your cacheManager. The ehcache-documentation says:void clearAll() Clears the contents of all caches in the CacheManager, but without removing any caches

If you want to test it, you can add some elements to your cache, call clearCache()and then try to get the values. The get()-method should only return null.

You can't add values directly in your cacheManager, it just manages the caches you declared in your configuration file. (by default it's ehcache.xml, you can get that on the ehcache homepage.) You also can add caches programmatically, even without knowing anything about the configuration.

    CacheManager cacheManager = CacheManager.getInstance();
    Ehcache cache = new Cache(cacheManager.getConfiguration().getDefaultCacheConfiguration());
    cache.setName("cacheName");
    cacheManager.addCache(cache);

To add a value to the cache you have to make an Element: Element element = new Element(key, value) and simply call cache.put(element). If your cache-variable isn't visible anymore, but your cacheManager is, you can do the same with cacheManager.getCache(cacheName).put(element)

I hope this helps...

Pepsin answered 9/9, 2013 at 12:57 Comment(2)
how to add somthing in my cache?Dybbuk
in the linux os, when we send this command: free -m, the cached value is : 16796 and when we send a command to clear cache,the cache has been decreased ,i want to do the same but using java,can i do this?Dybbuk
T
5

If you know the cache name, you can retrieve it from the CacheManager and use removeAll().

CacheManager manager = CacheManager.getInstance();
Ehcache cache = manager.getCache(cacheName);
cache.removeAll();

Your approach works, but it will clear all caches' objects.

Tasiatasiana answered 10/10, 2014 at 14:55 Comment(0)
B
0

There are 2 ways to achieve this:

  1. Using dev-console: If your are using an enterprise version of ehcache which comes bundled with Terracotta Server Array (TSA), in the installed directory one can find the dev-console.bat or dev-console.sh based on the flavor of the OS, just run this file and connect to the TSA server ( if defaults for installation are used then you can connect to localhost:9520 ) select the named cache-manager and then in the overview tab select clear caches button and select the required caches to clear in the listing. This will clear the cache contents of the selected caches and can be verified in the sizing tab, where it shows the available memory pie chart.
  2. Using open source ehCache: using the cache manager api to clear cache as described in the question and then verify by using a get from the cache for a particular key is null.
Busywork answered 13/4, 2014 at 15:25 Comment(0)
T
0

Answer above mentioned net.sf.ehcache.CacheManager of Ehcache.
In common case org.springframework.cache.CacheManager is Spring interface.
For clearing all you need to inject it and clear all named caches:

@Autowired
CacheManager cacheManager;

public void clearAllCaches() {
  cacheManager.getCacheNames().forEach(name -> cacheManager.getCache(name).clear());
}

See Cache Eviction in Spring Boot

Telex answered 27/1 at 20:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.