1

I'm using JCache annotations with Spring and EhCache. If I use @CacheResult on a method without specifying the cache name (or a @CacheDefaults on the class), then the default cache name resolves to the fully qualified method name. However such a cache is not found, unless explicitly created using the CacheManager. This may be manageable for a few such cache-enabled methods, but not if I have to create 50 different caches manually.

Is there a way to tell Spring (or any JCache implementer) to automatically create caches with the default name, if not found? This would allow me to use @CacheResult on any method without having to go update the cache configuration every time.

cruftex
  • 5,545
  • 2
  • 20
  • 36
Oceanic
  • 1,418
  • 2
  • 12
  • 19

1 Answers1

-1

Is there a way to tell Spring (or any JCache implementer) to automatically create caches with the default name, if not found?

This problem is covered in cache2k. You can configure cache2k to use a default configuration, if the requested cache name is not known.

Here is an example XML configuration for this scenario, which is placed at /cache2k.xml in the class path:

<cache2k xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
         xmlns='https://cache2k.org/schema/v1.x'
         xsi:schemaLocation="https://cache2k.org/schema/v1.x https://cache2k.org/schema/cache2k-core-v1.x.xsd">
  <version>1.0</version>
  <ignoreMissingCacheConfiguration>true</ignoreMissingCacheConfiguration>
  <defaults>
    <!-- default settings for every cache -->
    <cache>
      <entryCapacity>100_000</entryCapacity>
      <expireAfterWrite>5m</expireAfterWrite>
    </cache>
  </defaults>
  <templates>
    <cache>
      <name>shortExpiry</name>
      <expireAfterWrite>5m</expireAfterWrite>
    </cache>
    <cache>
      <name>lessResilient</name>
      <resilienceDuration>1m</resilienceDuration>
    </cache>
  </templates>
  <caches>
    <cache>
      <name>products</name>
      <entryCapacity>10_000</entryCapacity>
      <include>shortExpiry,lessResilient</include>
    </cache>
    <cache>
      <name>users</name>
      <entryCapacity>1000</entryCapacity>
      <include>shortExpiry</include>
    </cache>
  </caches>
</cache2k>

By default, according to this configuration, a cache without a specific configuration entry will get 100K entry limit and 5 minutes expiry. The configuration may be suitable for the trails in production. Later on, you, or a system operator, can check what caches are existing and their performance via JMX and then add a more specific cache configuration as soon as there is need. If you have many caches, the configuration has a template mechanism to avoid repetition.

It may happen that caches need a specific configuration upfront and some don't. I suggest separating them into different cache managers.

If you need an in process cache, cache2k is a a good alternative to EHCache. It is way faster and more memory efficient.

You can use cache2k via JCache, however there is also direct Spring framework support. See the User Guide Spring Framework Section for details.

I am the author of cache2k and not a heavy Spring user at the moment, however, I am happy to help if there are any issues.

cruftex
  • 5,545
  • 2
  • 20
  • 36