Skip to content

Instantly share code, notes, and snippets.

@Gregg
Created February 18, 2009 18:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save Gregg/66462 to your computer and use it in GitHub Desktop.
Save Gregg/66462 to your computer and use it in GitHub Desktop.
# To configure memcached to be used
# /config/environment.rb
config.cache_store = :mem_cache_store
# Object Store cache commands
Rails.cache.read
Rails.cache.write
Rails.cache.fetch
Rails.cache.delete
Rails.cache.exist?
Rails.cache.increment
Rails.cache.decrement
Rails.cache.clear
# Example of using expires_in
def self.recent
Rails.cache.fetch("recent_posts", :expires_in => 30.minutes) do
self.find(:all, :limit => 10)
end
end
# Using cache_key properly to give good cache keys
<% cache(post) do %>
<!-- Data related to this post -->
<% end %>
# A more realistic cache key
<% cache([post, user, "sidebar"]) do %>
<!-- Data related to this user and this post -->
<% end %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment