|
- class ReplyCache
- class<<self
- #------------讨论区帖子的回复---------------------
- def message_replys_cache(id)
- key = 'message_reply:'+id.to_s
- data = Rails.cache.read(key)
- if data.nil?
- data = []
- replys = Message.where("root_id = #{id}").reorder("created_on desc")
-
- replys.each do |reply|
- data << {
- :id=>reply.id,
- :parent_id=>reply.parent_id,
- :created_on=>reply.created_on
- }
- end
- Rails.cache.write(key,data)
- end
- data
- end
-
- def get_message_reply_parents_cache(id,reply_id)
- replys = message_replys_cache(id)
-
- results = []
-
- #本身已排好序只需一级级往下找就是,先找到初始点
- parent_id = 0
- replys.each do |reply|
- if reply[:id] == reply_id
- if reply[:parent_id].nil?
- break
- else
- parent_id = reply[:parent_id]
- end
- elsif parent_id != 0
- if reply[:id] == parent_id
- results << reply
- if reply[:parent_id].nil?
- break
- else
- parent_id = reply[:parent_id]
- end
- end
- end
- end
-
- results
- end
-
- #更新就是 删除/增加回复/数据库更新之后再调用!!!!!!!!! /新增的插到第一条!!!!.insert(0,m) .delete(m)
- def update_message_replys_cache(id,message_reply,type)
- key = 'message_reply:'+id.to_s
- data = Rails.cache.read(key)
- if data.nil?
- # data = []
- # replys = Message.where("root_id = #{id}").reorder("created_on desc")
- #
- # replys.each do |reply|
- # data << {
- # :id=>reply.id,
- # :parent_id=>reply.parent_id,
- # :created_on=>reply.created_on
- # }
- # end
- # Rails.cache.write(key,data)
- else
- if type == 1
- #删除
- data.each do |reply|
- if reply[:id] == message_reply.id
- data.delete(reply)
- break
- end
- end
- else
- #增加
- opt = {
- :id=>message_reply.id,
- :parent_id=>message_reply.parent_id,
- :created_on=>message_reply.created_on
- }
-
- data.insert(0,opt)
- end
- Rails.cache.write(key,data)
- end
- end
-
- def delete_message_replys_cache(id)
- key = 'message_reply:'+id.to_s
- Rails.cache.delete(key)
- end
-
- #------------通知新闻帖子的回复---------------------
- def news_replys_cache(id)
- key = 'news_reply:'+id.to_s
- data = Rails.cache.read(key)
- if data.nil?
- data = []
- news = News.find(id)
- replys = news.comments.reorder("created_on desc")
-
- replys.each do |reply|
- data << {
- :id=>reply.id,
- :parent_id=>reply.parent_id,
- :created_on=>reply.created_on
- }
- end
- Rails.cache.write(key,data)
- end
- data
- end
-
- def get_news_reply_parents_cache(id,reply_id)
- replys = news_replys_cache(id)
-
- results = []
-
- #本身已排好序只需一级级往下找就是,先找到初始点
- parent_id = 0
- replys.each do |reply|
- if reply[:id] == reply_id
- if reply[:parent_id].nil?
- break
- else
- parent_id = reply[:parent_id]
- end
- elsif parent_id != 0
- if reply[:id] == parent_id
- results << reply
- if reply[:parent_id].nil?
- break
- else
- parent_id = reply[:parent_id]
- end
- end
- end
- end
-
- results
- end
-
- #更新就是 删除/增加回复/数据库更新之后再调用!!!!!!!!! /新增的插到第一条!!!!.insert(0,m) .delete(m)
- def update_news_replys_cache(id,news_reply,type)
- key = 'news_reply:'+id.to_s
- data = Rails.cache.read(key)
- if data.nil?
- # data = []
- # news = News.find(id)
- # replys = news.comments.reorder("created_on desc")
- #
- # replys.each do |reply|
- # data << {
- # :id=>reply.id,
- # :parent_id=>reply.parent_id,
- # :created_on=>reply.created_on
- # }
- # end
- # Rails.cache.write(key,data)
- else
- if type == 1
- #删除
- data.each do |reply|
- if reply[:id] == news_reply.id
- data.delete(reply)
- break
- end
- end
- else
- #增加
- opt = {
- :id=>news_reply.id,
- :parent_id=>news_reply.parent_id,
- :created_on=>news_reply.created_on
- }
-
- data.insert(0,opt)
- end
- Rails.cache.write(key,data)
- end
- end
-
- def delete_news_replys_cache(id)
- key = 'news_reply:'+id.to_s
- Rails.cache.delete(key)
- end
-
- #------------作业帖子的回复---------------------
- def homework_replys_cache(id)
- key = 'homework_reply:'+id.to_s
- data = Rails.cache.read(key)
- if data.nil?
- data = []
- homework = HomeworkCommon.find(id)
- replys = homework.journals_for_messages.reorder("created_on desc")
-
- replys.each do |reply|
- data << {
- :id=>reply.id,
- :parent_id=>reply.m_parent_id,
- :created_on=>reply.created_on
- }
- end
- Rails.cache.write(key,data)
- end
- data
- end
-
- def get_homework_reply_parents_cache(id,reply_id)
- replys = homework_replys_cache(id)
-
- results = []
-
- #本身已排好序只需一级级往下找就是,先找到初始点
- parent_id = 0
- replys.each do |reply|
- if reply[:id] == reply_id
- if reply[:parent_id].nil?
- break
- else
- parent_id = reply[:parent_id]
- end
- elsif parent_id != 0
- if reply[:id] == parent_id
- results << reply
- if reply[:parent_id].nil?
- break
- else
- parent_id = reply[:parent_id]
- end
- end
- end
- end
-
- results
- end
-
- #更新就是 删除/增加回复/数据库更新之后再调用!!!!!!!!! /新增的插到第一条!!!!.insert(0,m) .delete(m)
- def update_homework_replys_cache(id,homework_reply,type)
- key = 'homework_reply:'+id.to_s
- data = Rails.cache.read(key)
- if data.nil?
- # data = []
- # homework = HomeworkCommon.find(id)
- # replys = homework.journals_for_messages.reorder("created_on desc")
- #
- # replys.each do |reply|
- # data << {
- # :id=>reply.id,
- # :parent_id=>reply.m_parent_id,
- # :created_on=>reply.created_on
- # }
- # end
- # Rails.cache.write(key,data)
- else
- if type == 1
- #删除
- data.each do |reply|
- if reply[:id] == homework_reply.id
- data.delete(reply)
- break
- end
- end
- else
- #增加
- opt = {
- :id=>homework_reply.id,
- :parent_id=>homework_reply.m_parent_id,
- :created_on=>homework_reply.created_on
- }
-
- data.insert(0,opt)
- end
- Rails.cache.write(key,data)
- end
- end
-
- def delete_homework_replys_cache(id)
- key = 'homework_reply:'+id.to_s
- Rails.cache.delete(key)
- end
-
- #------------留言的回复---------------------
- def leave_replys_cache(id)
- key = 'leave_reply:'+id.to_s
- data = Rails.cache.read(key)
- if data.nil?
- data = []
- replys = JournalsForMessage.where("root_id = #{id}").reorder("created_on desc")
- replys.each do |reply|
- data << {
- :id=>reply.id,
- :parent_id=>reply.m_parent_id,
- :created_on=>reply.created_on
- }
- end
- Rails.cache.write(key,data)
- end
- data
- end
-
- def get_leave_reply_parents_cache(id,reply_id)
- replys = leave_replys_cache(id)
-
- results = []
-
- #本身已排好序只需一级级往下找就是,先找到初始点
- parent_id = 0
- replys.each do |reply|
- if reply[:id] == reply_id
- if reply[:parent_id].nil?
- break
- else
- parent_id = reply[:parent_id]
- end
- elsif parent_id != 0
- if reply[:id] == parent_id
- results << reply
- if reply[:parent_id].nil?
- break
- else
- parent_id = reply[:parent_id]
- end
- end
- end
- end
-
- results
- end
-
- #更新就是 删除/增加回复/数据库更新之后再调用!!!!!!!!! /新增的插到第一条!!!!.insert(0,m) .delete(m)
- def update_leave_replys_cache(id,leave_reply,type)
- key = 'leave_reply:'+id.to_s
- data = Rails.cache.read(key)
- if data.nil?
- # data = []
- # replys = JournalsForMessage.where("root_id = #{id}").reorder("created_on desc")
- # replys.each do |reply|
- # data << {
- # :id=>reply.id,
- # :parent_id=>reply.m_parent_id,
- # :created_on=>reply.created_on
- # }
- # end
- # Rails.cache.write(key,data)
- else
- if type == 1
- #删除
- data.each do |reply|
- if reply[:id] == leave_reply.id
- data.delete(reply)
- break
- end
- end
- else
- #增加
- opt = {
- :id=>leave_reply.id,
- :parent_id=>leave_reply.m_parent_id,
- :created_on=>leave_reply.created_on
- }
-
- data.insert(0,opt)
- end
- Rails.cache.write(key,data)
- end
- end
-
- def delete_leave_replys_cache(id)
- key = 'leave_reply:'+id.to_s
- Rails.cache.delete(key)
- end
-
- #------------博客的回复---------------------
- def blog_replys_cache(id)
- key = 'blog_reply:'+id.to_s
- data = Rails.cache.read(key)
- if data.nil?
- data = []
- replys = BlogComment.where("root_id = #{id}").reorder("created_on desc")
- replys.each do |reply|
- data << {
- :id=>reply.id,
- :parent_id=>reply.parent_id,
- :created_on=>reply.created_on
- }
- end
- Rails.cache.write(key,data)
- end
- data
- end
-
- def get_blog_reply_parents_cache(id,reply_id)
- replys = blog_replys_cache(id)
-
- results = []
-
- #本身已排好序只需一级级往下找就是,先找到初始点
- parent_id = 0
- replys.each do |reply|
- if reply[:id] == reply_id
- if reply[:parent_id].nil?
- break
- else
- parent_id = reply[:parent_id]
- end
- elsif parent_id != 0
- if reply[:id] == parent_id
- results << reply
- if reply[:parent_id].nil?
- break
- else
- parent_id = reply[:parent_id]
- end
- end
- end
- end
-
- results
- end
-
- #更新就是 删除/增加回复/数据库更新之后再调用!!!!!!!!! /新增的插到第一条!!!!.insert(0,m) .delete(m)
- def update_blog_replys_cache(id,blog_reply,type)
- key = 'blog_reply:'+id.to_s
- data = Rails.cache.read(key)
- if data.nil?
- # data = []
- # replys = BlogComment.where("root_id = #{id}").reorder("created_on desc")
- # replys.each do |reply|
- # data << {
- # :id=>reply.id,
- # :parent_id=>reply.parent_id,
- # :created_on=>reply.created_on
- # }
- # end
- # Rails.cache.write(key,data)
- else
- if type == 1
- #删除
- data.each do |reply|
- if reply[:id] == blog_reply.id
- data.delete(reply)
- break
- end
- end
- else
- #增加
- opt = {
- :id=>blog_reply.id,
- :parent_id=>blog_reply.parent_id,
- :created_on=>blog_reply.created_on
- }
-
- data.insert(0,opt)
- end
- Rails.cache.write(key,data)
- end
- end
-
- def delete_blog_replys_cache(id)
- key = 'blog_reply:'+id.to_s
- Rails.cache.delete(key)
- end
-
- end
- end
|