如何抽象出两种不同的缓存实现
我计划在我的负载平衡 Web 应用程序中使用分布式缓存。 所以我打算尝试抽象出apache ehcache和memcached之间的公共功能。
我的目标是能够进行简单的配置开关来选择要使用的缓存解决方案。 我应该走 SPI 路线,例如 XML 解析器的连接方式吗?
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 ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
从我的脑海中...
我相信这就是您在结构上需要让它可靠地工作的全部,这样您就可以在您愿意的时候扩展它。
From the top of my head...
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.
修复了界面之后,这确实是一个创作模式的问题。 依赖注入是我最喜欢的,如果缓存选择策略是动态的,你可以使用 spring bean 工厂在运行时决定。 Spring 支持 Web 应用程序上的“会话”范围,这意味着如果您愿意,您可以让工厂决定每个会话。
否则,一个简单的服务定位器也应该可以解决问题。
http://en.wikipedia.org/wiki/Service_locator_pattern
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.
http://en.wikipedia.org/wiki/Service_locator_pattern
Spring 还有一个缓存提供程序模块,它完全可以完成您想要做的事情。 不过,我只是不确定是否支持 memcached。 即使没有,为其编写适配器可能比手动滚动自己的界面要少一些工作。 请参阅 https://springmodules.dev.java.net/docs/参考/0.8/html_single/#cache
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 https://springmodules.dev.java.net/docs/reference/0.8/html_single/#cache