如何在Rails中使用多个缓存? [英] How to use multiple caches in rails?

查看:54
本文介绍了如何在Rails中使用多个缓存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Rails应用程序,出于不同的目的,我想同时使用memcached和文件存储缓存。

I have a rails application where I would like to use both memcached and the file store cache, for different purposes.

我想使用文件存储缓存来保留大量不经常更改的页面(有些根本不更改)-即页面缓存-并将memcached用于其他所有内容(动作和数据库缓存等)。原因是存储在文件存储高速缓存中的页面可能需要大量的存储,但是大多数情况下个别访问很少。

I want to use the file store cache to keep a large number of pages that don't change often (some not at all) - i.e. page caching - and use memcached for everything else (action and DB caching etc). The reason is that the pages stored on the file store cache are likely to require a large amount of storage, but individually most will be accessed infrequently.

是否可以这样做?还是将memcached配置为缓存意味着它也用于页面缓存?

Is this possible to do or will configuring memcached as the cache mean that it is also used for page caching?

作为第二个问题,从文件存储缓存中删除页面的安全方法是什么?在某种形式的cron作业中,因为似乎没有为该缓存指定ttl的选项。例如,UNIX find命令将快速查找并删除所有旧页面或很长时间未访问的页面-这样做是安全的,因为应用服务器可能会尝试同时提供这些页面之一(例如这是不太可能的)?如果不是,那么执行此操作的最佳方法是什么。

As a secondary question, what is a safe way to remove pages from the file store cache in some form of cron job, as there does not seem to be an option to specify ttl for this cache. For example a UNIX find command would quickly find and remove all old pages or pages that haven't been accessed in a long time - is this safe to do given the app server might potentially try to serve one of those pages at the time (tho this is very unlikely)? If not then what is the best way to do this.

推荐答案

如果只想将文件系统用于页面缓存和内存缓存对于操作和片段缓存,您还可以。页面缓存始终使用文件系统。只需记住页面缓存会绕过Rails应用程序,因此您不能将其用于包含内容随用户而变化的页面或用于使用过滤器进行访问控制的页面。

If you want to use the filesystem only for page caching and memcached for action and fragment caching, you're fine. Page caching always uses the filesystem. Just remember that page caching bypasses your Rails application, so you can't use it for pages that include content that changes from user to user or for pages that are access controlled with filters.

关于页面的删除,在Unix上,可以删除一个文件,但是直到关闭所有打开的文件句柄后,该文件才真正从磁盘上删除。如果应用服务器已打开文件以处理请求,并且find命令在一秒钟后将其删除,则应用服务器尝试读取时不会突然出现错误。

Regarding the removal of pages, on Unix, a file can be deleted, but it is not actually removed from disk until all open file handles are closed. If the app server has opened the file to serve a request, and the find command deletes it a split-second later, the app server doesn't suddenly get an error when it tries to read.

您还可以考虑根据文件的上次访问时间删除文件,而不是创建或修改文件,并在Rails应用程序中使用清除程序删除文件内容过期时显示缓存的页面。

You could also consider having find delete files based on their last access time, instead of creation or modification, and using a sweeper in your Rails app to delete the cached page when its content is out of date.

这篇关于如何在Rails中使用多个缓存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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