trustieforge/app/caches/attachments_cache.rb

101 lines
3.3 KiB
Ruby

#资源缓存
class AttachmentsCache
class << self
#------------班级资源缓存---------------------
def course_attachments_cache(course_id)
key = 'course_attachments:'+course_id.to_s
data = Rails.cache.read(key)
if data.nil?
data = []
course = Course.find(course_id)
course.attachments.each do |attachment|
data << {
:id=>attachment.id,
:filesize=>attachment.filesize,
:downloads=>attachment.downloads,
:author_id=>attachment.author_id,
:created_on=>attachment.created_on,
:is_public=>attachment.is_public,
:is_publish=>attachment.is_publish,
:container_id=>attachment.container_id,
:container_type=>attachment.container_type,
}
end
Rails.cache.write(key,data)
end
data
end
#增加删除type
def update_course_attachments_cache(course_id,attachemnt_id,type,opt={})
key = 'course_attachments:'+course_id.to_s
data = Rails.cache.read(key)
if data.nil?
# data = []
# course = Course.find(course_id)
# course.attachments.each do |attachment|
# data << {
# :id=>attachment.id,
# :filesize=>attachment.filesize,
# :downloads=>attachment.downloads,
# :author_id=>attachment.author_id,
# :created_on=>attachment.created_on,
# :is_public=>attachment.is_public,
# :is_publish=>attachment.is_publish,
# :container_id=>attachment.container_id,
# :container_type=>attachment.container_type,
# }
# end
#
# Rails.cache.write(key,data)
else
if type == 0
#增加
attachment = Attachment.find(attachemnt_id)
data << {
:id=>attachment.id,
:filesize=>attachment.filesize,
:downloads=>attachment.downloads,
:author_id=>attachment.author_id,
:created_on=>attachment.created_on,
:is_public=>attachment.is_public,
:is_publish=>attachment.is_publish,
:container_id=>attachment.container_id,
:container_type=>attachment.container_type,
}
elsif type == 1
#删除
data.each do |atta|
if atta[:id] == attachemnt_id
data.delete(atta)
break
end
end
else
#修改
attachment = Attachment.find(attachemnt_id)
data.each_with_index do |atta,index|
if atta[:id] == attachemnt_id
data[index] = {
:id=>attachment.id,
:filesize=>attachment.filesize,
:downloads=>attachment.downloads,
:author_id=>attachment.author_id,
:created_on=>attachment.created_on,
:is_public=>attachment.is_public,
:is_publish=>attachment.is_publish,
:container_id=>attachment.container_id,
:container_type=>attachment.container_type,
}
break
end
end
end
Rails.cache.write(key,data)
end
end
end
end