Skip to content

Instantly share code, notes, and snippets.

@kellysutton
Last active December 20, 2015 02:39
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 kellysutton/6058382 to your computer and use it in GitHub Desktop.
Save kellysutton/6058382 to your computer and use it in GitHub Desktop.
ActiveModel::Serializer cache_digest-esque behavior
class PersonSerializer < ActiveModel::Serializer
def model_version
Digest::MD5.hexdigest(File.read(File.join(Rails.root, "app/serializers", "#{self.class.to_s.underscore}.rb")).gsub(/\s/,''))
end
end
@kellysutton
Copy link
Author

When you're serializing data for an API, you probably want your caches to get cleared when you add/remove/change payloads. It would be nice to have functionality similar to cache_digests, such that if the template changes, so does the cached data. Including this code in your cache key gives you that.

This code obviously makes a lot of assumptions. You will need to twerk it as necessary.

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