trustieforge/app/caches/comment_cache.rb

35 lines
825 B
Ruby

class CommentCache
class<<self
#-----------------------------新闻回复
def comment_cache(id)
key = "comment:"+id.to_s
data = Rails.cache.read(key) rescue nil
if data.nil?
#直接存数据库对象
data = Comment.find(id) rescue nil
if data
Rails.cache.write(key,data,{ expires_in: 90.minutes }) rescue Rails.cache.delete(key)
end
end
data
end
#更新
def update_comment_cache(id,comment)
key = "comment:"+id.to_s
data = Rails.cache.read(key)
if data.nil?
else
Rails.cache.write(key,comment,{ expires_in: 90.minutes }) rescue Rails.cache.delete(key)
end
end
#删除
def delete_comment_cache(id)
key = "comment:"+id.to_s
Rails.cache.delete(key)
end
end
end