如何使用 ORMLite 实现对象缓存

发布于 2024-11-15 19:04:57 字数 183 浏览 2 评论 0原文

我正在使用 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 技术交流群。

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

发布评论

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

评论(2

浮云落日 2024-11-22 19:04:57

我已经在 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.

极度宠爱 2024-11-22 19:04:57

仅供参考,我们在 4.26 版本中向 ORMLite 原生添加了一个对象缓存。它默认支持 WeakReferenceSoftReference 缓存,并且可能包含一个简单的 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 and SoftReference 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.

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