Created
July 20, 2011 01:14
-
-
Save ch1ago/1094132 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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