Skip to content

Instantly share code, notes, and snippets.

@mynameisrufus
Created July 30, 2013 09:37
Show Gist options
  • Save mynameisrufus/6111616 to your computer and use it in GitHub Desktop.
Save mynameisrufus/6111616 to your computer and use it in GitHub Desktop.
Using cache with data you don't know has changed or not.
class Mario
def initialize data_hash
@data_hash = data_hash
end
def cache_key
Digest::MD5.hexdigest @data_hash.to_s
end
end
class MarioController
def stuff
@marios = GetMarios.new.fetch #=> [Mario, Mario, Mario, Mario, Mario, Mario]
end
end
Read fragment views/f60eb1a953001b8e2e7223e7e413f1c3/a6fcb29b761b4e1496c1c6b5b502f794/c98cf2c35cfcaa7fac0446ddacaa2e65/ea74793cc73779ec3f51b0bce1b586a9/c39c8e2284f5fd01677ae40a1c47b430/4a6f3d91a01ce37b8bd9334e811c35ed/d7c0e914aa6b81024e495fcd3f55b6c0/b3ae71b90c2f1d46ea23d76077838b86/93425db00d648241dc04f82631a23700
Write fragment views/f60eb1a953001b8e2e7223e7e413f1c3/a6fcb29b761b4e1496c1c6b5b502f794/c98cf2c35cfcaa7fac0446ddacaa2e65/ea74793cc73779ec3f51b0bce1b586a9/c39c8e2284f5fd01677ae40a1c47b430/4a6f3d91a01ce37b8bd9334e811c35ed/d7c0e914aa6b81024e495fcd3f55b6c0/b3ae71b90c2f1d46ea23d76077838b86/93425db00d648241dc04f82631a23700
<% cache @marios, expires_in: 30.minutes do %>
<% @marios.each do |mario| %>
<strong>Lots of heavy rendering here</strong>
<% end %>
<% end %>
@mariovisic
Copy link

I can see what you're suggesting but the downside of this approach means getting the objects for the cache key from the database on each request, which is slow. So I was looking to avoid it.

@mynameisrufus
Copy link
Author

This is suited to API requests, I would not do this with a DB unless it was some sort of freaky DB that another process was updating 🐑

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment