When you have the need of caching that’s a quite common topic, you can find many out-of-the-box solutions.
But what if if you want to cache whole class, with all the methods, maybe across your whole application?
My solution based on a proxy class and this way applies cache on all method invocations. The cache configuration of course can be defined, but the basis can see in the example:
ClassInterface class = new Class();
ClassInterface cachedClass = CacheClass.localCache(ClassInterface .class, class, CacheClass.CacheType.ACCESS, 30);
So in the above example we need an interface, a class instance and some configuration options for caching.
This way we’ll get a Proxy class, which will cache our methods, where we use the cachedClass instance.
This is all good, but what if we want to have caching across the application.
@Bean("classCached")
public ClassInterface getCachedClass() {
ClassInterface class = new Class();
ClassInterface cachedGloballyClass = CacheClass.globalCache(ClassInterface.class, class );
return cachedGloballyClass;
}
This way whereever we inject the cached instance all of the invocation, will point the same instance, this way it will share the cachepool.
My implementation is using the guava caching library, hardwired for now.
But any caching library can serve inside the proxy solution.
The source is available: https://github.com/PazsitZ/methodcaching
The jars available: http://pazsitz.hu/repo/hu/pazsitz/methodcaching/