CacheResource { boolean clearCache() { boolean cleared = false; try { CacheConfiguration ehConfig = new CacheConfiguration() .name(cacheName) .memoryStoreEvictionPolicy(MemoryStoreEvictionPolicy.LRU) .persistence(new PersistenceConfiguration() .strategy(PersistenceConfiguration.Strategy.LOCALTEMPSWAP)) .maxBytesLocalDisk(10, MemoryUnit.MEGABYTES) .maxBytesLocalHeap(1, MemoryUnit.MEGABYTES) .clearOnFlush(true); Configuration managerConfig = new Configuration() .diskStore(new DiskStoreConfiguration().path(cacheLocation)) .dynamicConfig(false) .cache(ehConfig); CacheManager cacheManager = CacheManager.create(managerConfig); Ehcache ehcache = cacheManager.getEhcache(cacheName); if (ehcache != null) { ehcache.flush(); ehcache.removeAll(); cleared = true; } else { cleared = false; } } catch (Exception e) { log.debug("Unable to clear cache", e); cleared = false; } return cleared; }  @DELETE @ConfiguredRolesAllowed(ConfiguredRolesAllowedDynamicFeature.CCH_ADMIN_USER_PROP) Response deleteCache();  }
@Test public void testClearCache() { CacheConfiguration ehConfig = new CacheConfiguration() .name(CACHE_NAME) .memoryStoreEvictionPolicy(MemoryStoreEvictionPolicy.LRU) .persistence(new PersistenceConfiguration() .strategy(PersistenceConfiguration.Strategy.LOCALTEMPSWAP)) .timeToLiveSeconds(1000) .timeToIdleSeconds(1000) .clearOnFlush(true); Configuration managerConfig = new Configuration() .diskStore(new DiskStoreConfiguration().path(CACHE_LOCATION)) .maxBytesLocalHeap(1, MemoryUnit.MEGABYTES) .maxBytesLocalDisk(100, MemoryUnit.MEGABYTES) .dynamicConfig(false) .cache(ehConfig); CacheManager cacheManager = CacheManager.create(managerConfig); Ehcache ehcache = cacheManager.getEhcache(CACHE_NAME); for (int i = 0; i < 100; i++) { Element e = new Element(i, "value" + i); ehcache.put(e); } assertThat(ehcache.getSize(), is(equalTo(100))); CacheResource instance = new CacheResource(); boolean cleared = instance.clearCache(); assertThat(cleared, is(equalTo(true))); assertThat(ehcache.getSize(), is(equalTo(0))); }