参考数据模式
与此线程类似,但不完全一样:如何以线程安全方式缓存信息
处理“参考数据”的常用模式是什么?应用程序经常读取的数据,通常在数据库或属性文件中外部化,但更新频率很低(几天、几周) ,月)? 当数据更新时,它会在外部更新。
这通常是一个单例,我可以用 DAO 注入它,从而能够管理它自己的内容吗? 我喜欢在该服务上公开一个刷新()方法的想法,该方法将强制刷新(即通过 MBean - 这样我就不必退回应用程序)。
从另一个 SO 线程来看,听起来人们可能会在必要时实例化 DAO,并在该级别透明地缓存。
我有点喜欢单例服务的想法,要么注入一个从数据库加载数据的真实 DAO,要么注入一个返回硬编码响应的模拟/测试替身。 然而,如果我通过 java 枚举将服务实现为单例,这使得通过 Spring 连接它会出现一些问题。
那么,其他人通常如何处理参考数据呢? 随意查询但在幕后有缓存? 或者单独的内存服务?
Similar to this thread, but not exactly: How To Cache Information In A Threadsafe Manner
What is the usual pattern for dealing with "reference data" - data that is frequently read by an application, usually externalized in a database or properties file, but updated very infrequently (days, weeks, months)? When the data is updated, it would be updated externally.
Would this normally be a singleton that I could inject with a DAO and would thus be able to manage its own contents? I like the idea of exposing a refresh() method on this service that would force a refresh (i.e., through an MBean - so I wouldn't have to bounce the application).
From the other SO thread, it sounds like people might just instantiate the DAO's whenever necessary and cache transparently at that level.
I kind of like the idea of the singleton service being injected with either a real DAO that loads data from the database, or else a mock/test-double that returns a hard-coded response. However, if I were to implement the service as a singleton via java enum's, this makes wiring it up via Spring a bit more problematic.
So, how do other people typically deal with reference data? Query-at-will but with caching under the covers? Or a separate in-memory service?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我通常使用 Spring 将 DAO 实现注入到我的服务层中,正如您提到的,除了基于 SQL 的实现之外,通常还有一个测试实现(
XMLDao
、FlatFileDao
)。 对于小型数据集,我通常编写自己的缓存并将从基础表加载的所有数据存储在内存中。综上所述,我的优势在于可以使用相当小的数据集。 如果我正在处理更大的数据集,我可能会考虑现成的缓存解决方案,可能分布在多个 JVM 上(例如 Terracotta )。
正如我在上一个线程中提到的,我还公开了一个刷新()方法。 如果数据更新不需要及时传播,我只需通过 MBean 手动调用即可。 在我希望自动执行此操作的情况下,我使用 Tibrv 监听数据库的更新并刷新缓存的数据(使用 MS-SQL 触发器生成 Tibrv 消息)。
我不太明白您提到使用 Java 枚举来实现服务 - 这将如何工作?
I typically inject a DAO implementation into my service layer using Spring and, as you mention often have a test implementation (
XMLDao
,FlatFileDao
) in addition to my SQL-based implementation. For small datasets I usually write my own cache and store all data loaded from the underlying table(s) in memory.Saying all that, I have the advantage of working with reasonably small datasets. If I were dealing with a larger dataset I may consider off-the-shelf caching solutions, possibly distributed across multiple JVMs (like Terracotta).
As I mentioned in the previous thread I also expose a refresh() method. In cases where updates to the data do not need to propagate in a timely manner I simply call this manually via an MBean. In situations where I wish to automate this I've used Tibrv to listen to updates from the database and refresh the cached data (using an MS-SQL trigger to generate a Tibrv message).
I don't quite understand your reference to using Java enums to implement the service - How would this work?