Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

The active record queries like User.all are lazily loaded, so it’s totally possible to pass them around like keys without them being executed.

That said, I wouldn’t use a query as a cache key since there may be more than one thing I want to cache about that query (GP’s example is their as_json representation, but what if I wanted something different to be computed? Like maybe html snippets? I would expect the cache key to mention everything about the thing it’s caching.)



> what if I wanted something different to be computed? Like maybe html snippets? I would expect the cache key to mention everything about the thing it’s caching.

This is exactly where you'd use something like ActiveSupport::Cache.expand_cache_key(["blog posts", blog.posts]). You can add bits of info to your cache keys to distinguish them from one another.

It's probably also worth mentioning that the `cache` view helper does parts of it for you already, and takes into account the digest of the view code itself among other things. Rails provides a really neat abstraction here that does make caching easier.

It has some rough edges, and some gotchas, but overall it's pretty smart.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: