Skip to content
Find file
Fetching contributors…
Cannot retrieve contributors at this time
executable file 152 lines (132 sloc) 3.99 KB
module CacheRead
def self.included(base)
base.extend ClassMethods
base.class_eval do
include Redis::Objects
ActiveRecord::Relation.send(:include,RelationCache)
after_update :update_cache_read
after_save :update_cache_read
after_create :update_cache_read
after_destroy :clear_cache_read
column_names.each do |column_name|
cache_name = (column_name+"_ca").to_sym
value cache_name
define_method (column_name+"_cache").to_sym do
cache_value = send(cache_name)
if cache_value.nil?
value = send(column_name)
marshal_value = Marshal.dump value
cache_value.send(:value=,marshal_value)
value
else
Marshal.load(cache_value.to_s)
end
end
end
end
base.send :include, InstanceMethods
end # self.included
module RelationCache
def first_cache
re = self.select(:id).first
key = "#{re.class.name}:#{re.id}"
cache_in_redis = $redis.get(key)
if cache_in_redis.nil?
cached_record = re.class.find(re.id)
$redis.set(key,Marshal.dump(cached_record))
else
cached_record = Marshal.load(cache_in_redis)
end
cached_record
end
def last_cache
re = self.order('id desc').select(:id).first
key = "#{re.class.name}:#{re.id}"
cache_in_redis = $redis.get(key)
if cache_in_redis.nil?
cached_record = re.class.find(re.id)
$redis.set(key,Marshal.dump(cached_record))
else
cached_record = Marshal.load(cache_in_redis)
end
cached_record
end
def in_cache
keys = []
cached_records = []
not_cached_ids = []
res = self.select(:id)
record_class = res.first.class
record_class_name = res.first.class.name
res.each do |re|
key = "#{record_class_name}:#{re.id}"
cache_in_redis = $redis.get(key)
if cache_in_redis.nil?
not_cached_ids << re.id
else
cached_record = Marshal.load(cache_in_redis)
end
cached_records << cached_record
end
record_class.find(not_cached_ids).each do |not_cached_record|
key = "#{record_class_name}:#{not_cached_record.id}"
$redis.set(key,Marshal.dump(not_cached_record))
cached_records << not_cached_record
end
cached_records
end
end
module ClassMethods
def first_cache
re = self.limit(1).select(:id).first
key = "#{re.class.name}:#{re.id}"
cache_in_redis = $redis.get(key)
if cache_in_redis.nil?
cached_record = re.class.find(re.id)
$redis.set(key,Marshal.dump(cached_record))
else
cached_record = Marshal.load(cache_in_redis)
end
cached_record
end
def last_cache
re = self.order('id desc').limit(1).select(:id).first
key = "#{re.class.name}:#{re.id}"
cache_in_redis = $redis.get(key)
if cache_in_redis.nil?
cached_record = re.class.find(re.id)
$redis.set(key,Marshal.dump(cached_record))
else
cached_record = Marshal.load(cache_in_redis)
end
cached_record
end
def cached_method(*symbols)
end
def get(column_name,id)
column_name = column_name.to_s
cache_name = (column_name+"_ca").to_sym
cache_value = self.redis.get("#{self.name.downcase}:12:#{column_name+'_ca'}")
if cache_value.nil?
m_instance = self.find(id)
value = m_instance.send(column_name)
marshal_value = Marshal.dump value
cache_value = m_instance.send(cache_name)
cache_value.send(:value=,marshal_value)
value
else
Marshal.load(cache_value.to_s)
end
end
end # ClassMethods
module InstanceMethods
def update_cache_read
key = "#{self.class.name}:#{id}"
$redis.set key,Marshal.dump(self)
end
def clear_cache_read
key = "#{self.class.name}:#{id}"
$redis.del key
end
end # InstanceMethods
end
Something went wrong with that request. Please try again.