How to have multiple cache manager configuration in spring cache java -
i want have multiple spring cache managers configured in web-application , able use different cache manager @ various places in project. there way this.
there several ways can , right answer depends on usage of cache.
you have "main" cache manager
if use cachemanager 90% of use case , b 10% i'd advise create default cachemanager
(you'll need specify via cacheconfigurersupport
extension), like:
@configuration @enabllecaching public class cacheconfig extends cachingconfigurersupport { @override @bean // not strictly necessary public cachemanager cachemanager() { ... cachemanager } @bean public cachemanager bcachemanager() { ... cachemanager b } }
then 10% use case add cacheconfig
@ top of classes need use other cache manager
@cacheconfig(cachemanager="bcachemanager") public class myservice { ... }
if need use other cache manager 1 method, can specify @ method level well
@cacheable(cachenames = "books", cachemanager = "bcachemanager") public book findbyid(long id) { ... }
more fine grained resolution
if you're not in situation, need way know cache manager needs used on case-by-case basis. can based on target type (myservice
) or name of cache (books
). you'll need implement cacheresolver
translation you.
@configuration @enabllecaching public class cacheconfig extends cachingconfigurersupport { @override public cacheresolver cacheresolver() { ... } }
check javadoc of cacheresolver
more details. in implementation, may have several cachemanager
instances (either bean or not) you'll call internally based on logic determine manager should used.
i saw in comment referring "module". caching infrastructure matter i'd advice move decision @ application level. may tag cache being "local" , others being "clustered". should have kind of nomenclature name make easier. don't choose cache manager @ module level.
this blog post illustrates other examples.
Comments
Post a Comment