Using Rails 3.1.1 on Heroku, Dalli & Memcachier.
Production.rb
config.cache_store = :dalli_store
config.action_controller.perform_caching = true
Gemfile
gem 'memcachier'
gem 'dalli'
Controller#Show
unless fragment_exist?("gift-show--" + @gift.slug)
# Perform complicated database query at 4-5 sec
end
View
<% cache('gift-show--' + @gift.slug, :expires_in => 3456000) do # 40 days %>
# Create an HTML
<% end %>
Log output when a cached page is loaded
2012-10-17T03:15:43+00:00 app[web.2]: Started GET "/present/baka-kaka-set" for 23.20.90.66 at 2012-10-17 03:15:43 +0000
2012-10-17T03:15:43+00:00 app[web.2]: Could not find fragment for gift-show--baka-kaka-set # my log comment
2012-10-17T03:15:44+00:00 heroku[router]: GET www.mydomain.com/present/baka-kaka-set dyno=web.2 queue=0 wait=0ms service=195ms status=200 bytes=17167
2012-10-17T03:15:43+00:00 app[web.2]: cache: [GET /present/baka-kaka-set] miss
2012-10-17T03:15:43+00:00 app[web.2]: Processing by GiftsController#show as */*
2012-10-17T03:15:43+00:00 app[web.2]: Parameters: {"id"=>"baka-kaka-set"}
2012-10-17T03:15:43+00:00 app[web.2]: Exist fragment? views/gift-show--baka-kaka-set (1.5ms)
2012-10-17T03:15:43+00:00 app[web.2]: Read fragment views/gift-show--baka-kaka-set (1.5ms)
2012-10-17T03:15:43+00:00 app[web.2]: Write fragment views/gift-show--baka-kaka-set (4.0ms)
Each page is pretty much static once it has been created, thus the long expiry time (40 days).
If I load a page like this it seems to be written to the cache, I can verify that by reloading the page and see that it bypasses the controller (as I expect) and deliver the page quite fast.
My problem
The problem is that if I return to the page a few minutes later it has already been deleted from the cache! fragment_exist?('gift-show--gift-baka-kaka-set')
returns false (so does Rails.cache.exist?('views/gift-show--gift-baka-kaka-set')
)
I can see in the Memcachier analytics that the number of keys are decreasing. Even when I run a script that load each page (to create fragment caches) the number of keys in Memcachier does not increase in the same rate.
I am at about 34% of memory usage in Memcachier, so I am not close to the limit.
My questions
Am I doing something complete wrong? What should I rather do?
Could it be that I am writing to two different caches or something?
The last line in the log file confuses me a bit. It seems like that even after a fragment is read, a new one is still being written? Is that weird?
UPDATE 1: I realized I had commented out the line:
# Set expire header of 30 days for static files
config.static_cache_control = "public, max-age=2592000"
before. Could that have caused the problem? Are cached actions considered 'static assets'?
Turned out to be a bug in Memcachier. Memcachier support came to this conclusion and it works fine with Memcache. Memcachier will try to fix the bug.