ruby-on-rails - 如何在 Rails 中缓存任意对象(基于时间)?

标签 ruby-on-rails caching memcached redis

我阅读了官方指南。它说有页面缓存 Action 缓存片段缓存,但它们不是我想要的。

我只是喜欢缓存一个对象,而不是整个页面或 View 片段,就像这样的伪代码:

def show
  cache @ads, :expires_in => 1.hour do
    @ads = Advertisement.all
  do
end

这可能吗?使用 memcache 还是 redis

最佳答案

试试这个:

#To cache the object
Rails.cache.write('cache-key', object)

#Load the object from the cache
Rails.cache.read('cache-key')

关于ruby-on-rails - 如何在 Rails 中缓存任意对象(基于时间)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8236637/

相关文章:

sql - 在 Ruby on Rails 中连接 SQL 服务器

ruby-on-rails - 脚手架还原

spring - 在缓存中找不到但不缓存结果时,如何使@Cacheable 返回null?

PHP7.1 使用 memcached 作为保存处理程序, session 启动缓慢

php - Memcached 对具有大量选择的小型数据库的好处

php - Thrift能否在php中维护Memcached持久连接

ruby-on-rails - Rails 应用程序需要拆分

ruby-on-rails - Rails 以 JSON 格式返回所有 404 和 500 错误

gwt - 为 GWT RPC 异步调用启用缓存

c# - 从Azure Redis缓存插入或删除值时,是否需要在代码级别进行同步?