Skip to content

Instantly share code, notes, and snippets.

@dchentech
Created November 30, 2012 09:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dchentech/4174674 to your computer and use it in GitHub Desktop.
Save dchentech/4174674 to your computer and use it in GitHub Desktop.
Rails动态文件添加ETag缓存
# 项目提供动态的文件下载,由于文件系统里的位置因加了自定义的目录分级,而与和URL有偏差,所以需要经过控制器来重写文件路径来读取,在此也保证了当前请求静态文件在磁盘上只有一份以节约空间。
# 为了避免单个客户端多次请求同一个文件,在该控制器方法里加上HTTP头部的ETag来进行304缓存。
def file
response.headers['Etag'] = Digest::SHA1.hexdigest(@your_file_instance)
send_file @your_file_instance.path
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment