Skip to content

Instantly share code, notes, and snippets.

@ch1ago
Created July 20, 2011 01:14
Show Gist options
  • Save ch1ago/1094132 to your computer and use it in GitHub Desktop.
Save ch1ago/1094132 to your computer and use it in GitHub Desktop.
module UserModuleKeyValueStore
def self.included(base)
base.extend ClassMethods
base.class_eval do
after_save :kv_save
end
end
module ClassMethods
def kv_find(id_key)
return kv_find_id(id_key) unless id_key.is_a? Array
id_key.map { |id| kv_find_id(id) }
end
def kv_find_id(id)
return nil unless id
puts "****** USER '#{id}' from KEY VALUE STORE"
Rails.cache.read("user_kv/id/#{id}") || (x = find(id) and x.try(:kv_save))
find(id)
end
end
def kv_save
Rails.cache.write("user_kv/id/#{self.id}", self)
puts "****** USER '#{self.id}' saved in KEY VALUE STORE"
self
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment