如何从Rails的后台进程将数据库密集型页面存储到缓存中 [英] How to store a database intensive page into cache from a background process in rails
问题描述
在我的网站上,我有一个数据库密集型页面,该页面缓存了5分钟.但是,当缓存过期并且必须重新呈现页面时,仍然会影响性能.我在想为什么不每隔5分钟从后台进程(如工作)中计算一次页面并将其存储在缓存中,并让用户请求每次都从缓存中得到服务.因此,无需用户等待该页面的呈现.
In my website i have a database intensive page which i cache for 5 mins. But still the performance is hit when the cache expires and the page has to be rendered freshly. I was thinking why not compute the page from a background process(like workling) once in 5 mins and store it in the cache and let the user request be serve from the cache everytime. And as a result no user have to wait for this page to be rendered.
有没有一种有效的方法来做到这一点? 任何帮助都非常感谢.
Is there a efficient way to do it? Any help very much appreciated.
谢谢
推荐答案
我建议您简单地从您的background-job或cron作业中为您的网页调用Web服务器.然后,让整个Rails堆栈处理该调用.您可以创建一个cron作业,每隔4-5分钟执行一次,然后将参数传递给页面以使其强制刷新缓存,例如:
I would recommend simply making a call to your web-server for the page from your background-job or cron job. Then you let your entire Rails stack handle the call. You could create a cron job to do this every 4-5 minutes and pass a parameter to the page to have it force a cache refresh, for example:
complex/page?force=true
然后,您的控制器/动作/模型将检查此参数的存在.如果出现强制,则页面将不使用缓存的版本,而是重新创建缓存.
Your controller/action/model would then check for the presence of this parameter. If force was present the page would not use the cached version, but would re-create the cache instead.
这篇关于如何从Rails的后台进程将数据库密集型页面存储到缓存中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!