如何使用 ORMLite 实现对象缓存
我正在使用 ORMLite,想知道是否有办法实现缓存,以防止多次查询同一对象。 例如,当它被注释为“foreign = true”和“foreignAutoRefresh = true”时,
问题不是关于“如何创建缓存”,而是“如何让 ORMLite 在 WeakHashMap 中查找对象并查询如果在数据库中找不到它,则在数据库中”
I'm using ORMLite and wanted to know if there is a way to implement a cache in order to prevent querying multiple times for the same object.
For example when it is annotated as "foreign = true" and "foreignAutoRefresh = true"
The question is not about "how to make a cache" but "how can I make ORMLite look for an object in, say, a WeakHashMap and query for it in the DB if it is not found there"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我已经在 Android 上使用 ORMLite 一段时间了,如果不是原生内置的,我建议您不要使用这样的缓存。甚至 Hibernate 的缓存有时也会对你耍一些花招,而且他们确实投入了多年的努力。
我宁愿在 DAO 或其他东西中构建一个小型缓存(例如,如此处所述),无论如何,这都会给你更多的控制权(对我来说效果很好)。顺便说一句,ORMLite 的主要贡献者甚至考虑删除“foreignAutoRefresh”,因为更新问题。
I have been using ORMLite for Android for a while now, and I would advise you against such a cache, if not natively built-in. Even Hibernate's cache does play some tricks on you sometimes, and they literally put years of effort into it.
I would rather build a mini-cache in a DAO or something (e.g., as described here), which gives you more control anyway (works pretty well for me). BTW, ORMLite's main contributor even considered to remove "foreignAutoRefresh" because of update-issues.
仅供参考,我们在 4.26 版本中向 ORMLite 原生添加了一个对象缓存。它默认支持
WeakReference
和SoftReference
缓存,并且可能包含一个简单的 LRU 缓存。然而,缓存相当“精简”,与 ORMLite 的名称一致。 @Manmal 确实是对的,他提到了 Hibernate 缓存的局限性以及如果你不小心的话它会如何欺骗你。
我认为这将是一项正在进行的工作,我们将来可能会启动一个带有一些更复杂缓存的
ormlite-cache
包。Just FYI but we have added an object cache to ORMLite native in version 4.26. It supports a
WeakReference
andSoftReference
caches by default and may includes a simple LRU cache.The cache is pretty "lite" however, in keeping with ORMLite's name. @Manmal is certainly right when he mentions the limitations of Hibernate's cache and how it can trick you if you are not careful.
I assume that this will be a work in progress and we may start an
ormlite-cache
package with some more complicated caches in the future.