Skip to content

Instantly share code, notes, and snippets.

@teeparham
Created August 17, 2012 23:01
Show Gist options
  • Save teeparham/3383344 to your computer and use it in GitHub Desktop.
Save teeparham/3383344 to your computer and use it in GitHub Desktop.
Patch for nanosecond cache_key precision for Rails 3.x
# see https://github.com/rails/rails/pull/6376
# put this in an initializer
module NsecCacheKeyPatch
# Override ActiveRecord::Integration#cache_key
def cache_key
case
when new_record?
"#{self.class.model_name.cache_key}/new"
when timestamp = self[:updated_at]
timestamp = timestamp.utc.to_s(:nsec)
"#{self.class.model_name.cache_key}/#{id}-#{timestamp}"
else
"#{self.class.model_name.cache_key}/#{id}"
end
end
end
::Time::DATE_FORMATS[:nsec] = '%Y%m%d%H%M%S%9N'
ActiveRecord::Base.send :include, NsecCacheKeyPatch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment