Skip to content

Instantly share code, notes, and snippets.

@aaronjensen
Created July 23, 2013 14:47
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aaronjensen/6062912 to your computer and use it in GitHub Desktop.
Save aaronjensen/6062912 to your computer and use it in GitHub Desktop.
module AllCacheKey
extend ActiveSupport::Concern
module ClassMethods
def cache_key
pluck("COUNT(*)", "MAX(updated_at)").flatten.map(&:to_i).join("-")
end
end
end
@aaronjensen
Copy link
Author

Note the multi-valued pluck is a Rails 4 feature, Rails 3 will need something slightly different

@tortuetorche
Copy link

This is similar to Caching with Rails guide cache_key_for_products method, isn't it ?
Or i'ts more optimized ?

@ream88
Copy link

ream88 commented Sep 11, 2013

For Rails 3 you need a little bit more code:

def cache_key
  sql = select('COUNT(*), MAX(updated_at)').to_sql
  connection.execute(sql).to_a.flatten.map(&:to_i).join('-')
end

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