如何抽象出两种不同的缓存实现

发布于 2024-07-10 08:59:37 字数 154 浏览 17 评论 0原文

我计划在我的负载平衡 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

陌路黄昏 2024-07-17 08:59:37

从我的脑海中...

  • 使用常见的缓存相关方法创建接口(add()、remove()、refresh() 是最明显的方法)。
  • 创建该接口的实现,该接口使用下面所需的缓存(“MyEhCacheImplementation”和“MyMemCachedImplementation”或类似的东西)。
  • 创建一个 CacheFactory,它基于数字、字符串或枚举等简单值返回某种类型的缓存。 不要忘记为默认实现做后备!
  • 创建某种方法在初始化时将单个值注入到工厂,例如,如果您有一些在启动期间读取各种设置的类,或者您正在使用 Spring applicationContext.xml 或类似的东西,则需要为缓存创建一个初始化方法它接受该一个参数,调用工厂并返回正确的缓存类型和/或将其设置到您使用它的某个位置。

我相信这就是您在结构上需要让它可靠地工作的全部,这样您就可以在您愿意的时候扩展它。

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.

雅心素梦 2024-07-17 08:59:37

修复了界面之后,这确实是一个创作模式的问题。 依赖注入是我最喜欢的,如果缓存选择策略是动态的,你可以使用 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

℡寂寞咖啡 2024-07-17 08:59:37

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文