Skip to content

Instantly share code, notes, and snippets.

@capitalist
Forked from reinh/gist:405328
Created May 18, 2010 21:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save capitalist/405573 to your computer and use it in GitHub Desktop.
Save capitalist/405573 to your computer and use it in GitHub Desktop.
class MyView < Mustache
def cache_by_user_id
lambda do |text|
cache_key = "user:#{current_user.id}"
if value = $cache.get(cache_key)
value
else
rendered = render(text)
$cache.set(cache_key, rendered)
rendered
end
end
end
end
# assumes $cache.set returns the value that is set, which seems sensible.
def $cache.fetch(key)
get(key) || set(key, yield)
end
class MyView < Mustache
def cache_by_user_id
lambda { |text| $cache.fetch("user:#{current_user.id}"){ render(text) } }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment