Reputation: 11
I want to use Ehcache dynamic configuration in version 3.
As seen in the doc here https://www.ehcache.org/documentation/2.8/configuration/configuration.html#ehcache.xsd
since Ehcache 2.0, certain cache configuration parameters can be modified dynamically at runtime
This is simply done in ehcache v2 by setting the <ehcache>
element’s dynamicConfig attribute to “true” in the ehcache.xml configuration file.
Now I'd like to know how to do that in ecache v3, since there is no <ehcache>
element in v3, so the dynamicConfig attribute is nowhere to be found in my v3 ehcache.xml file.
I cannot find it anywhere in the documentation or in SO.
This is my ehcache v3 xml file, but it seems neither <config>
element nor <cache>
element have some sort of dynamicConfig attribute that I need.
<config xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
xmlns='http://www.ehcache.org/v3'
xsi:schemaLocation="
http://www.ehcache.org/v3
http://www.ehcache.org/schema/ehcache-core-3.7.xsd">
<persistence directory="spring-boot-ehcache/cache" />
<cache alias="categoriesCache">
<expiry>
<ttl unit="seconds">3000</ttl>
</expiry>
<listeners>
<listener>
<class>com.something.somethingv.jr.be.cache.CacheEventLogger
</class>
<event-firing-mode>ASYNCHRONOUS</event-firing-mode>
<event-ordering-mode>UNORDERED</event-ordering-mode>
<events-to-fire-on>CREATED</events-to-fire-on>
<events-to-fire-on>EXPIRED</events-to-fire-on>
<events-to-fire-on>EVICTED</events-to-fire-on>
</listener>
</listeners>
<resources>
<heap>1000</heap>
<offheap unit="MB">10</offheap>
<disk persistent="true" unit="MB">20</disk>
</resources>
</cache>
<cache alias="autocompleteCache">
<expiry>
<ttl unit="seconds">3000</ttl>
</expiry>
<listeners>
<listener>
<class>com.something.somethingv.jr.be.cache.CacheEventLogger
</class>
<event-firing-mode>ASYNCHRONOUS</event-firing-mode>
<event-ordering-mode>UNORDERED</event-ordering-mode>
<events-to-fire-on>CREATED</events-to-fire-on>
<events-to-fire-on>EXPIRED</events-to-fire-on>
<events-to-fire-on>EVICTED</events-to-fire-on>
</listener>
</listeners>
<resources>
<heap>1000</heap>
<offheap unit="MB">10</offheap>
<disk persistent="true" unit="MB">20</disk>
</resources>
</cache>
</config>
So, how can I do it?
Upvotes: 1
Views: 794
Reputation: 160
private void modifyCacheSize(int newSize) {
org.ehcache.config.ResourcePools rp = ResourcePoolsBuilder.newResourcePoolsBuilder().heap(newSize, EntryUnit.ENTRIES).build();
javax.cache.Cache<Long, MyObject> cache = cacheManager.getCache(YOURCACHENAME);
org.ehcache.jsr107.Eh107Configuration<Long, MyObject> configuration = cache.getConfiguration(org.ehcache.jsr107.Eh107Configuration.class);
org.ehcache.config.CacheRuntimeConfiguration<Long, MyObject> rtc = configuration.unwrap(org.ehcache.config.CacheRuntimeConfiguration.class);
rtc.updateResourcePools(rp);
SizedResourcePool srp = rtc.getResourcePools().getPoolForResource(ResourceType.Core.HEAP);
long size = srp.getSize();
System.out.println(size);
This may not answer your question but this is the top search result for modifying a running cache in ehcache3. The documentation could be more help.
This code sample shows how to dynamically update the size of a heap resource pool in a running cache. The rtc variable exposes anything you can modify.
Hopefully this helps someone trying to do this.
Upvotes: 0