简体   繁体   English

用Ruby on Rails重新生成静态缓存的最佳方法是什么?

[英]What's the most optimal way to regenerate static cache with Ruby on Rails?

I have a pretty slow controller action, which does some raporting here and there. 我的控制器动作很慢,到处乱跑。 I only need to refresh the data every few days, so it was no brainer to static cache the result. 我只需要每隔几天刷新一次数据,因此静态缓存结果并不费劲。

The problem is, that the action takes solid few minutes to complete and I am not sure whats the most optimal way to expire the old data and replace them with the new ones. 问题是,该操作需要几分钟的时间才能完成,而且我不确定使旧数据过期并用新数据替换它们的最佳方法是什么。

Now the problem with just generic expire/request is that for few minutes (time when the action is running) those data are unavailable. 现在,仅通用到期/请求的问题是几分钟(操作运行的时间)这些数据不可用。

Is there any resonable way to overcome this gap using just static cache mechanisms in Rails? 是否仅在Rails中使用静态缓存机制就可以克服这种差距? Or should I just rebuild the whole thing in a different way? 还是我应该以另一种方式重建整个事情?

Rails has a built-in way to use stale caches for just a bit longer when it expires while the new cache value is being regenerated. 在重新生成新的缓存值时,Rails有过期的过期时间,Rails具有一种内置的使用方式。 It's the :race_condition_ttl setting used in conjuction with :expires_in , as describe in the Rails Guides on Caching . 这是:race_condition_ttl设置与:expires_in结合使用,如《 Rails缓存指南》中所述。

With Rails Fragment caching the syntax should be: 使用Rails Fragment缓存时,语法应为:

<% cache 'my_awesome_cache_key', :expires_in => 12.hours.to_i, :race_condition_ttl => 12.hours.to_i %>
  # This block will be cached
<% end %>

Results: 结果:

  • 1) First request: Cache is empty, so block will be executed and results written to cache 1)第一个请求:缓存为空,因此将执行块并将结果写入缓存
  • 2) All requests between the next 24 hours: Will be served straight from the cache (which is fresh) 2)接下来的24小时内的所有请求:将直接从缓存中获取(这是新鲜的)
  • 3a) First request after 24 hours but WITHIN grace period will be served from the then slightly stale cache; 3a)24小时之后但在宽限期内的第一个请求将由当时稍微陈旧的缓存提供; the cache will be regenerated in the background: the block is executed and written to the cache as soon as its finished 缓存将在后台重新生成:该块被执行并在完成后立即写入缓存
  • 3b) First request after 24 hours but out of the grace period will be handled as 1), ie not served directly but block will be executed, cache will be written and request will be served. 3b)24小时之后但超出宽限期的第一个请求将被视为1),即不直接提供服务,而是执行块,将写入缓存并满足请求。

race_condition_ttl was introduced to prevent multiple processes from regenerating a cache simultaneuosly, which would result in all processes reading the data from the db at once etc. on a highly requested resource, but I think it should work well for your situation. 引入race_condition_ttl是为了防止多个进程同时重新生成高速缓存,这将导致所有进程立即从数据库中读取大量请求的资源,等等,但是我认为它应该适合您的情况。

How to choose the timing for :expires_in and :race_condition_ttl is your choice then, I'd suggest calculating it like this: expires_in + race_condition = expires_in_that_you_would_usually_set . 然后,如何选择:expires_in:race_condition_ttl是您的选择,我建议按以下方式进行计算: expires_in + race_condition = expires_in_that_you_would_usually_set This way the cache is regenerated more often but also fresher, especially if the action/view is not rendered that often. 这样,可以更频繁地重新生成缓存,但是也可以更新缓存,特别是如果操作/视图的显示频率不那么高的话。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Rails 4中缓存静态页面的最佳方法是什么? - What's the best way to cache static pages in Rails 4? 在Ruby on Rails 3中将Dropbox文件列表与表内容进行比较的最快,最有效的方法是什么? - What's the fastest most efficient way to compare a dropbox filelist to the contents of a table in Ruby on Rails 3? Ruby On Rails - 找到具有大多数(有很多)关联的对象的最佳方法是什么? - Ruby On Rails - what's the best way to find an object with most (has-many) associations? 将外部数据(tsv或json)提供给Ruby on Rails的最有效方法是什么? - What's the most efficient way to serve external data (tsv or json) to Ruby on Rails? 在Rails中处理不存在的变量的最佳方法是什么? - What's the optimal way to deal with non-existent variables in Rails? 在Ruby on Rails中实现自动完成的最专业方法是什么? - What is the most professional way to implement autocomplete in Ruby on Rails? 将日期选择器添加到 Ruby on Rails 6 项目的最原生方法是什么? - What is the most native way of adding a date picker to a Ruby on Rails 6 project? 在Ruby on rails应用程序中开发和测试缓存的最佳方法是什么? - What is the best way to develop and test cache in a Ruby on rails application? 在Ruby中深度复制对象的最有效方法是什么? - What's the most efficient way to deep copy an object in Ruby? 在Ruby中格式化全名的最简洁方法是什么? - What's the most concise way to format a full name in Ruby?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM