如何缓存对象以供多个请求存储它们?

发布于 2024-08-24 09:12:53 字数 235 浏览 12 评论 0原文

我正在使用 Ruby on Rails,我需要存储通过连接到另一台服务器获得的搜索结果集。问题是我不想将结果集存储在会话中,并且我想要可以在多个请求中存储结果集对象的东西。

查询需要时间,所以我不想重复。有没有办法可以存储对象或缓存对象,这样我就不必一次又一次地查询它?

可以使用某种对象存储吗?

任何帮助都会很棒。

如果可以选择记忆,我该如何记忆对象?连接仍然需要时间,因此如何存储结果集。

I am using Ruby on Rails and I need to store a search result set obtained by connecting to another server. The problem is I don't want to store the result set in the session and I want something where I can store the result set object over multiple requests.

The querying takes time so I don't want to repeat it. Is there a way I could store objects or cache objects so that I don't have to query it again and again?

Can use some kind of object store?

Any help would be great.

If memoizing is an option how do I memoize objects? The connection would still take time so how to store the result set.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

泪意 2024-08-31 09:12:53

如果您不想将其存储在会话中,显然您可以在这里选择。

您可以临时存储在数据库中(我假设查询数据库比从另一台服务器重新获取要快:))。

还可以选择使用 memcached 之类的东西。尽管您必须注意重新启动它会丢失所有数据。

取决于您需要实现什么以及您需要如何处理数据。

If you don't want to store it in the session, obviously you have options here.

You can store in your DB temporarily (I assume querying the DB is faster than re-fetching from another server :)).

Also there's an option to use something like memcached. Although you have to be aware that restarting it will throw all your data away.

Depends on what you need to achieve and how you need to handle your data.

春风十里 2024-08-31 09:12:53

记忆仅在单个请求中起作用。不能在多个请求中使用。

所有请求都有所有存储资源,例如 Memcache 或 BDD。如果您使用 ActiveSupport: :Cache::MemCacheStore。您可以推送和获取所有请求中的所有对象。

The memoization works only in single request. Can't be use in several request.

All request have all storage resource like Memcache or BDD.. If you use the ActiveSupport::Cache::MemCacheStore. you can push and fetch all object in all of your request.

我不在是我 2024-08-31 09:12:53

如果您需要在请求之间存储数据,那么记忆可能不是您想要的。当您在单个请求中重复调用相同的方法时,通常在 Ruby/Rails 中使用记忆化,并且该方法的成本很高(CPU 密集型、多个数据库请求等)。

您可以记住一个方法,该方法将结果存储在实例变量中,下次调用该方法时,将返回实例变量值,而不是重新计算该方法。如果您想进一步研究的话,有大量这方面的资源。

对于需要跨会话持久保存并且可能需要在不同用户之间共享的数据,我强烈推荐 memcached。 Rails 有一些内置的支持,因此挖掘一些好的资源应该不会太难。

If you need to store data between requests, memoization likely isn't what you are looking for. Memoization is typically used in Ruby/Rails when you are calling the same method repeatedly within a single request, and that method is expensive (either CPU intensive, multiple DB requests, etc).

You can memoize a method, which stores the result in an instance variable, and the next time it is called, the instance variable value is returned, rather than reevaluating the method. There are tons of resources out there on this if you want to look into it further.

For data that needs to persist across sessions, and may need to be shared between different users, I highly recommend memcached. Rails has some built in support for it, so it shouldn't be too hard to dig up some good resources.

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