How to abstract out 2 different implementations of cache

I plan to use a distributed cache in my load-balanced webapp. So I'm going to try to abstract out the common functions between apache ehcache and memcached.

My goal is to be able to make a simple configuration switch to select the caching solution to use. Should I go the SPI route e.g. like how XML parsers are wired in ?


From the top of my head...

  • Create interface with common cache related methods (add(), remove(), refresh() comes to mind as the most obvious ones).
  • Create implementations of that interface which use desired cache underneath ("MyEhCacheImplementation" and "MyMemCachedImplementation" or something like that).
  • Create a CacheFactory which returns some type of cache based on simple value like number, String or enum. Don't forget to make a fallback for default implementation!
  • create some way to inject that single value to the factory on initialization, for example if you have some class which reads various settings during startup or you're using Spring applicationContext.xml or something similar, you need to create an initialization method for your cache which takes in that one parameter, calls the factory and returns the correct type of cache and/or sets it to some place from where you're using it from.

I believe that's all you need structurally to get it to work reliably and so that you can extend it whenever you feel like doing so.

12.12.2008 10:16:24

After fixing the interface, this is really a creational pattern problem. Dependency Injection is my favourite, if the cache-selection strategy is dynamic you can use spring bean factories to decide at runtime. Spring has support for "session" scopes on web-applications meaning you can let the factory decide per session if you want to.

Otherwise a simple service locator should also do the trick.

12.12.2008 09:30:37

Spring also has a cache provider module which does exactly what you're looking to do. I'm just not sure if memcached is supported, though. Even if not, writing an adapter for it might be less work than hand-rolling your own interface. See

21.12.2008 04:45:11