如何在Rails中缓存任意对象(基于时间)? [英] How to cache an arbitrary object in Rails(time-based)?

查看:52
本文介绍了如何在Rails中缓存任意对象(基于时间)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我阅读了官方指南.它说有page cacheaction cachefragment cache,但它们不是我想要的.

I read the official guide. It says there are page cache, action cache and fragment cache, but they are not what I want.

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

I just like to cache an object, not the whole page or fragment of view, like this pseudocode:

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

有可能吗?与memcacheredis?

推荐答案

尝试一下:

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

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

这篇关于如何在Rails中缓存任意对象(基于时间)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆