66 lines
2.1 KiB
Ruby
66 lines
2.1 KiB
Ruby
#资源缓存
|
|
class PraiseCache
|
|
class << self
|
|
#------------帖子赞数缓存---------------------
|
|
def praise_cache(object_id,object_type)
|
|
object_type = object_type.to_s
|
|
key = 'praise:'+object_type+":"+object_id.to_s
|
|
data = Rails.cache.read(key)
|
|
if data.nil?
|
|
data = {}
|
|
praise = PraiseTreadCache.where("object_id=#{object_id} and object_type='#{object_type}'").first
|
|
praise_num = praise.nil? ? 0 : praise.praise_num
|
|
data = {
|
|
:object_id=>object_id,
|
|
:object_type=>object_type,
|
|
:praise_num=>praise_num
|
|
}
|
|
Rails.cache.write(key,data)
|
|
end
|
|
data
|
|
end
|
|
|
|
def update_praise_cache(object_id,object_type,opt={})
|
|
object_type = object_type.to_s
|
|
key = 'praise:'+object_type+":"+object_id.to_s
|
|
data = Rails.cache.read(key)
|
|
if data.nil?
|
|
# data = {}
|
|
# praise = PraiseTreadCache.where("object_id=#{object_id} and object_type='#{object_type}'")
|
|
# praise_num = praise.nil? ? 0 : praise.praise_num
|
|
# data = {
|
|
# :object_id=>object_id,
|
|
# :object_type=>object_type,
|
|
# :praise_num=>praise_num
|
|
# }
|
|
# Rails.cache.write(key,data)
|
|
else
|
|
data = data.merge(opt)
|
|
Rails.cache.write(key,data)
|
|
end
|
|
end
|
|
|
|
#------------帖子已赞、未赞缓存---------------------
|
|
def ispraise_cache(user_id,object_id,object_type)
|
|
object_type = object_type.to_s
|
|
key = 'ispraise:'+object_type+":"+object_id.to_s+":"+user_id.to_s
|
|
data = Rails.cache.read(key)
|
|
if data.nil?
|
|
data = false
|
|
unless PraiseTread.where(praise_tread_object_id: object_id,praise_tread_object_type: object_type,user_id: user_id).empty?
|
|
data = true
|
|
end
|
|
Rails.cache.write(key,data)
|
|
end
|
|
data
|
|
end
|
|
|
|
def update_ispraise_cache(user_id,object_id,object_type,flag)
|
|
object_type = object_type.to_s
|
|
key = 'ispraise:'+object_type+":"+object_id.to_s+":"+user_id.to_s
|
|
Rails.cache.write(key,flag)
|
|
end
|
|
|
|
end
|
|
end
|