Pec*_*eck 5 ruby caching ruby-on-rails
我在我们的一个仪表板系统上有一个很长的数据库查询,我想缓存,因为结果不需要实时准确,但可以从缓存中提供"足够接近"的值.
我想在没有用户等待的情况下这样做.我正在寻找使用类似的东西
Rails.cache.write('my_val', 'val', :expires_in => 60.minutes)
Run Code Online (Sandbox Code Playgroud)
存储此值,但我不相信它提供了我想要的确切功能.我想打个电话
Rails.fetch('my_val') { create a background task to update my_val; return expired my_val}
Run Code Online (Sandbox Code Playgroud)
似乎my_val在缓存过期后会从缓存中删除.有没有办法访问这个过期的值或可能启用此功能的另一种内置机制?
谢谢.
只需这样做:
Rails.cache.write('my_val', 'val')
Run Code Online (Sandbox Code Playgroud)
永不过期
现在运行您的后台作业:
SomeLongJob.process
Run Code Online (Sandbox Code Playgroud)
在 SomeLongJob.process 作业中执行以下操作:
def SomeLongJob.process
some_long_calculation = Blah.calc
Rails.cache.write('my_val', some_long_calculation)
end
Run Code Online (Sandbox Code Playgroud)
现在读取数据
def get_value
val = Rails.cache.read('my_val', 'val')
end
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1192 次 |
| 最近记录: |