138 lines
4.3 KiB
Ruby
138 lines
4.3 KiB
Ruby
class ProjectCache
|
|
class<<self
|
|
#-----------------------------数据统计--------------
|
|
|
|
#--------------------项目动态列表---------------------
|
|
def project_acts_cache(project_id)
|
|
key = 'project_acts:'+project_id.to_s
|
|
data = Rails.cache.read(key)
|
|
if data.nil?
|
|
data = []
|
|
forge_acts = ForgeActivity.where("project_id = ?", project_id)
|
|
forge_acts.each do |act|
|
|
data << {
|
|
:id=>act.id,
|
|
:user_id=>act.user_id,
|
|
:forge_act_id=>act.forge_act_id,
|
|
:forge_act_type=>act.forge_act_type,
|
|
:updated_at=>act.updated_at.to_i,
|
|
:created_at=>act.created_at.to_i
|
|
}
|
|
end
|
|
data = data.to_json
|
|
Rails.cache.write(key,data)
|
|
end
|
|
JSON.parse(data)
|
|
end
|
|
|
|
#更新内容
|
|
def update_project_acts_cache(project_id,forge_act_id,forge_act_type,opt={})
|
|
key = 'project_acts:'+project_id.to_s
|
|
data = Rails.cache.read(key)
|
|
if data.nil?
|
|
# data = []
|
|
# forge_acts = ForgeActivity.where("project_id = ?", project_id)
|
|
# forge_acts.each do |act|
|
|
# data << {
|
|
# :id=>act.id,
|
|
# :user_id=>act.user_id,
|
|
# :forge_act_id=>act.forge_act_id,
|
|
# :forge_act_type=>act.forge_act_type,
|
|
# :updated_at=>act.updated_at.to_i,
|
|
# :created_at=>act.created_at.to_i
|
|
# }
|
|
# end
|
|
# data = data.to_json
|
|
# Rails.cache.write(key,data)
|
|
else
|
|
data = JSON.parse(data)
|
|
data.each_with_index do |act,index|
|
|
if act['forge_act_id'] == forge_act_id && act['forge_act_type'] == forge_act_type
|
|
data[index] = data[index].merge(opt)
|
|
break
|
|
end
|
|
end
|
|
data = data.to_json
|
|
Rails.cache.write(key,data)
|
|
end
|
|
end
|
|
|
|
#更新条数
|
|
def update_project_acts_nums_cache(project_id,project_act,type)
|
|
key = 'project_acts:'+project_id.to_s
|
|
data = Rails.cache.read(key)
|
|
if data.nil?
|
|
data = []
|
|
forge_acts = ForgeActivity.where("project_id = ?", project_id)
|
|
forge_acts.each do |act|
|
|
data << {
|
|
:id=>act.id,
|
|
:user_id=>act.user_id,
|
|
:forge_act_id=>act.forge_act_id,
|
|
:forge_act_type=>act.forge_act_type,
|
|
:updated_at=>act.updated_at.to_i,
|
|
:created_at=>act.created_at.to_i
|
|
}
|
|
end
|
|
data = data.to_json
|
|
Rails.cache.write(key,data)
|
|
else
|
|
data = JSON.parse(data)
|
|
if type == 1
|
|
#删除
|
|
data.each do |act|
|
|
if act['id'] == project_act.id
|
|
data.delete(act)
|
|
break
|
|
end
|
|
end
|
|
else
|
|
#增加
|
|
data << {
|
|
:id=>project_act.id,
|
|
:user_id=>project_act.user_id,
|
|
:forge_act_id=>project_act.forge_act_id,
|
|
:forge_act_type=>project_act.forge_act_type,
|
|
:updated_at=>project_act.updated_at.to_i,
|
|
:created_at=>project_act.created_at.to_i
|
|
}
|
|
end
|
|
data = data.to_json
|
|
Rails.cache.write(key,data)
|
|
end
|
|
end
|
|
|
|
#-----------------------------项目最新动态时间--------------
|
|
def project_act_updated_at_cache(project_id)
|
|
key = "project_act_updated_at"
|
|
data = Rails.cache.read(key) || []
|
|
if data[project_id].nil?
|
|
sql = "SELECT max(updated_at) updated_at FROM forge_activities where project_id =#{project_id}"
|
|
updated_at = Project.find_by_sql(sql).first.updated_at
|
|
data[project_id] = updated_at
|
|
Rails.cache.write(key,data)
|
|
end
|
|
data[project_id]
|
|
end
|
|
|
|
#更新
|
|
def update_project_act_updated_at_cache(project_id)
|
|
key = "project_act_updated_at"
|
|
data = Rails.cache.read(key) || []
|
|
sql = "SELECT max(updated_at) updated_at FROM forge_activities where project_id =#{project_id}"
|
|
updated_at = Project.find_by_sql(sql).first.updated_at
|
|
data[project_id] = updated_at
|
|
Rails.cache.write(key,data)
|
|
end
|
|
|
|
#更新
|
|
def update_project_act_updated_at_to_0_cache(project_id)
|
|
key = "project_act_updated_at"
|
|
data = Rails.cache.read(key) || []
|
|
data[project_id] = 0
|
|
Rails.cache.write(key,data)
|
|
end
|
|
|
|
end
|
|
|
|
end |