|
- class UserCache
- class<<self
-
- def user_cache(id)
- key = "user:"+id.to_s
- data = Rails.cache.read(key)
- if data.nil?
- #直接存数据库对象
- data = User.find(id) rescue nil
- if data
- Rails.cache.write(key,data)
- end
- end
- data
- end
-
- #更新
- def update_user_cache(id,user)
- key = "user:"+id.to_s
- data = Rails.cache.read(key)
- if data.nil?
- #直接存数据库对象
- data = User.find(id) rescue nil
- if data
- Rails.cache.write(key,data)
- end
- else
- Rails.cache.write(key,user)
- end
- end
-
- #-----------------------------收藏的班级
- def user_favorite_courses_cache(user_id)
- key = "user_favorite_courses:"+user_id.to_s
- data = Rails.cache.read(key)
- if data.nil?
- data = []
- user = User.find(user_id)
- courses = user.favorite_courses.visible.where("is_delete =?", 0).select("courses.*,(SELECT MAX(updated_at) FROM `course_activities` WHERE course_activities.course_id = courses.id) AS a").order("a desc")
- courses.each do |course|
- data << {
- :id=>course.id,
- #:updated_at=>course.a
- }
- end
-
- Rails.cache.write(key,data)
- end
- data
- end
-
- #更新
- def update_user_favorite_courses_cache(user_id,course_id,type)
- key = "user_favorite_courses:"+user_id.to_s
- data = Rails.cache.read(key)
- if data.nil?
- # data = []
- # user = User.find(user_id)
- # courses = user.favorite_courses.visible.where("is_delete =?", 0).select("courses.*,(SELECT MAX(updated_at) FROM `course_activities` WHERE course_activities.course_id = courses.id) AS a").order("a desc")
- # courses.each do |course|
- # data << {
- # :id=>course.id,
- # #:updated_at=>course.a
- # }
- # end
- #
- # Rails.cache.write(key,data)
- else
- if type ==0
- #增加
- data << {
- :id=>course_id,
- #:updated_at=>info.updated_at
- }
- elsif type == 1
- #删除
- data.each do |course|
- if course[:id] == course_id
- data.delete(course)
- break
- end
- end
- else
- # data.each_with_index do |course,index|
- # if course[:id] == course_id
- # data[index] = data[index].merge(opt)
- # break
- # end
- # end
- end
-
- Rails.cache.write(key,data)
- end
- end
-
- #-----------------------------收藏的项目
- def user_favorite_projects_cache(user_id)
- key = "user_favorite_projects:"+user_id.to_s
- data = Rails.cache.read(key)
- if data.nil?
- data = []
- user = User.find(user_id)
- projects = user.favorite_projects.visible.select("projects.*, (SELECT MAX(updated_at) FROM `forge_activities` WHERE forge_activities.project_id = projects.id) AS a").order("a desc")
- projects.each do |project|
- data << {
- :id=>project.id,
- #:updated_at=>project.a
- }
- end
-
- Rails.cache.write(key,data)
- end
- data
- end
-
- #更新
- def update_user_favorite_projects_cache(user_id,project_id,type,opt={})
- key = "user_favorite_projects:"+user_id.to_s
- data = Rails.cache.read(key)
- if data.nil?
- # data = []
- # user = User.find(user_id)
- # projects = user.favorite_projects.visible.select("projects.*, (SELECT MAX(updated_at) FROM `forge_activities` WHERE forge_activities.project_id = projects.id) AS a").order("a desc")
- # projects.each do |project|
- # data << {
- # :id=>project.id,
- # #:updated_at=>project.a
- # }
- # end
- #
- # Rails.cache.write(key,data)
- else
- if type == 0
- #增加
- data << {
- :id=>project_id,
- #:updated_at=>info.updated_at
- }
- elsif type == 1
- #删除
- data.each do |project|
- if project[:id] == project_id
- data.delete(project)
- break
- end
- end
-
- else
- # data.each_with_index do |project,index|
- # if project[:id] == project_id
- # data[index] = data[index].merge(opt)
- # break
- # end
- # end
- end
- Rails.cache.write(key,data)
- end
- end
-
- end
- end
|