1

I am trying to temporarily disable (by disable I mean: act as if caching was turned off entirely) a set of cache2k caches and there seems to be no way to do this with the default implementation at "runtime" (and by runtime I mean after the cache has been created and potentially populated). So I thought I would provide a new Cache (possibly using the existing cache implementation along with org.cache2k.ForwardingCache and just wrapping each call to act as I would expect if disabled). Please note that I am not using any loader functionality (and would like not to).

How do I register a new cache implementation within cache2k? Is there a better way to do this without using a loader?

Ideally I would like to do something like this:

public void enableCaching() {
    CacheManager.getInstance().getActiveCaches().forEach(cache -> {
        cache.enable();
    });
}
public void disableCaching() {
    CacheManager.getInstance().getActiveCaches().forEach(cache -> {
        cache.clear();
        cache.disable();
    });
}
DavidT
  • 356
  • 2
  • 7
  • Hi David, thanks for the interesting question! Can you give some background about your use case; why you want to disable the cache? – cruftex Nov 10 '17 at 18:01
  • Is disabling the cache an ops/user intervention or something that happens regularly on behalf of what an event? – cruftex Nov 10 '17 at 18:03
  • It is a customer requirement. The idea would be to be able to debug any issues that may be caused by caching. I will be clearing the cache before enabling it again so I am not at all concerned about the cache contents. – DavidT Nov 10 '17 at 18:04
  • Yes, it is an ops intervention, it is not something that happens often. – DavidT Nov 10 '17 at 18:05
  • I am currently writing an administration page (meant for ops types) that will allow for clearing of individual caches, but there was a request to disable caching all around. I think it may also be useful to disable an individual cache rather than all of them but either solution will work for me. – DavidT Nov 10 '17 at 18:07
  • The most elegant solution I can think of is to add exactly that functionality in the JMX interface. Its not there at the moment, but quite easy to add. Do you like to open an issue? – cruftex Nov 10 '17 at 18:08
  • I can, but is there not existing functionality to extend caching implementations? – DavidT Nov 10 '17 at 18:10
  • BTW: I was thinking about adding support in http://hawt.io for the next version. – cruftex Nov 10 '17 at 18:10
  • "extend caching implementation" probably means that the cache builder is returning a wrapped version right away, which is configurable? no there isn't, that would be another idea/issue :) – cruftex Nov 10 '17 at 18:12
  • The internal technical solution for your request is actually to "resize" the cache to size 0 and back again. That has zero overhead. – cruftex Nov 10 '17 at 18:13
  • In this particular case I am using cache2k inside of a JIRA plugin. The plugin admin would be able to clear/disable the cache for the plugin. Sadly supporting hawt.io won't help me in this case but I do hope it helps others. I like having a nice admin interface to show clients, especially if it is hawt! :) – DavidT Nov 10 '17 at 18:13
  • Yes, the resize to 0 was my original idea but there does not seem to be "runtime" (as defined above) functionality to do this (which is a bummer). Why are caches not re-configurable after they have been created? – DavidT Nov 10 '17 at 18:15
  • What actions would you like me to take in order to request any of the above to be added to a new version? What would the ETA for the new version be? – DavidT Nov 10 '17 at 18:16
  • Well it's version 1.0 ;) There is quite a lot stuff there already.... – cruftex Nov 10 '17 at 18:18
  • ETA would be a couple of days I think. Can we move the conversion to an issue? Pls. register as github user and open an issue at https://github.com/cache2k/cache2k/issues – cruftex Nov 10 '17 at 18:19
  • I am not complaining :) ... I would rather do this myself as it is not core functionality but I can't seem to do that without a fork. – DavidT Nov 10 '17 at 18:20
  • 1
    Submitted as https://github.com/cache2k/cache2k/issues/74 – DavidT Nov 10 '17 at 18:38

0 Answers0