-
Notifications
You must be signed in to change notification settings - Fork 12.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enable access to Cache Delegates #572
Comments
I would also use this feature on the default cache implementation to access the LoggingCache decorator to get its statistical data. Currently the only accessible statistical value of a cache is its size. Hit ratio is only available in the logs, but I would like to publish that info too on our app's API. If you are there, please create also public getters for hits, requests and hitRatio attributes as well. Thanks in advance! |
If custom cache can extends from default cache( like this #818 |
@wuwen5 |
You wrote...
What did you mean by "or similar" here? To log statistics, isn't it better to use the statistics provided by each custom cache implementation like below?
If so, just stop wrapping custom caches in LoggingCache could be a solution. p.s. |
@harawata: I would use that feature with the default (myBatis internal) cache implementation. |
@terba Yes, you wrote that in your previous comment. Sorry I had missed it! I am wondering if it's possible to satisfy @FlorianSchaetz 's request (i.e. accessing external custom cache implementation) without exposing cache delegates. |
Hi @harawata , I have an idea, we can create an abstract cache decorator class. @Test
public void getSpecifiedDecorator() {
Cache cache = new PerpetualCache("default");
cache = new LruCache(cache);
cache = new LoggingCache(cache);
cache = new SynchronizedCache(cache);
cache.putObject("hello", System.currentTimeMillis());
cache.getObject("hello");
LoggingCache loggingCache = findCacheDecorator((CacheDecorator)cache, LoggingCache.class);
assertNotNull(loggingCache);
assertEquals(1,loggingCache.getHits());
PerpetualCache cacheDecorator = findCacheDecorator((CacheDecorator) cache, PerpetualCache.class);
assertNotNull(cacheDecorator);
}
private <T> T findCacheDecorator(CacheDecorator cache, Class<T> type) {
Cache delegate = cache.getDelegate();
if (delegate.getClass().equals(type)) {
return (T) delegate;
} else if (delegate instanceof CacheDecorator) {
return findCacheDecorator((CacheDecorator)delegate, type);
}
return null;
} |
and this eliminates duplicate code for the cache decorator implements. |
Hi @wuwen5 , Thank you for the PR, but I don't think allowing access to the delegates is a good idea. Regarding @terba 's request, it may be useful to provide basic statistics like hit ratio when using the built-in caches, but providing it via LoggingCache does not seem to be right approach, in my opinion. |
I have similar trouble: I need to get some more stats like max cache size during its life, and I cannot just extend PersistentCache. there is a way to provide a Custom decorator to the CacheBuilder or to extend it a bit?
cui prodest? why we avoid decorators for custom caches, but we support LoggingCache? |
@daitangio
|
For sure. I need a way to access to all the caches to collect stats about their usage (for instance how much % is used and so on). The only way I find out was to define a custom cache: but doing so I loose all the standard decorators (like LRU + ability to flush them and so on) and it was a disaster :) See "// issue #352, do not apply decorators to custom caches" above. It could be nice to extend the LoggingCache class if possible or to be able to decorate a custom perpetual cache... OR there is a simple way to access all the PerpetualCache's instances? |
Thank you for a reply, @daitangio ! I am thinking about adding As it involves other modification and will be a backward incompatible change, you may have to wait for the next major (minor?) version which is under discussion currently. p.s. |
Hi, I only need two things:
The (2) is not mandatory because you can "discover" the underlying implementation easily, but the first one is needed and as far as I know, there is no way to obtain it (but I may be wrong). |
When using a Cache, especially when using custom Cache implementations, it can be desirable to access that Cache object. Unfortunately, most of the time it will be wrapped into a LoggingCache or similar, which hides this implementation. If would be helpful if LoggingCache, etc. offered a method to get their delegate, so that access to the real Cache instances would be possible. While it is true that direct access to an underlying object might break behaviour of the Cache chain, without such an access, custom Caches will have to be registered into a custom HashMap or similar which tends also be error-prone, especially in a concurrent environment.
The text was updated successfully, but these errors were encountered: