gpt4 book ai didi

ruby-on-rails-3 - 使用 Memcached/Dalli 从缓存中排除图像

转载 作者:行者123 更新时间:2023-12-02 22:43:50 24 4
gpt4 key购买 nike

我最近使用 Dalli gem 为我的 Rails 应用程序实现了带有 memcached heroku 附加组件的缓存。但我发现,当部署到 Heroku 时,它还缓存了我所有的静态 Assets ,包括图像,这很快就增加了我的 memcached 大小。 heroku 日志示例如下所示

cache: [GET /assets/application.css] fresh
app[web.1]: cache: [GET /assets/sign-in-twitter.gif] fresh
app[web.1]: cache: [GET /assets/ajax-loader.gif] fresh
app[web.1]: cache: [GET /assets/sign-in-facebook.gif] fresh

特别是对于索引页面,缓存大小会针对每个不同的请求增加大约 5MB。这种行为是可配置的吗?我可以将 memcached 配置为只缓存我的片段缓存而不主动缓存每个页面中的每个图像吗?

最佳答案

使用 dalli gem,在 config/environments/production.rb 中:

config.action_dispatch.rack_cache = {  :metastore    => Dalli::Client.new,  :entitystore  => 'file:tmp/cache/rack/body',  :allow_reload => false}

上述配置将元存储信息缓存在 memcached 中,但将 Assets 的实际主体缓存到文件系统中。

config/application.rb 中:

if !Rails.env.development? && !Rails.env.test?  config.middleware.insert_before Rack::Cache, Rack::Static, urls: [config.assets.prefix], root: 'public'end

Rack::静态用法:
Rack::Static 中间件提供与根目录匹配的前缀的 url。在这里,我将 config.assets.prefix 作为我的 url 前缀,默认为 '/assets'。这应该直接从 public/assets 目录提供任何 Assets ,而不是点击 Rails::Cache。只有当您在生产中运行'rake assets:precompile' 时,这才会起作用,否则在'public/assets' 中将没有预编译 Assets 。

关于ruby-on-rails-3 - 使用 Memcached/Dalli 从缓存中排除图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10337076/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com