comparison app/controllers/pastes_controller.rb @ 366:26545fe719ca

Remove caching system. Too much complexity for something that is really simple.
author edogawaconan <me@myconan.net>
date Sat, 14 Feb 2015 01:35:45 +0900
parents cd1c3a28b89a
children 6e3e1e7b0212
comparison
equal deleted inserted replaced
365:9d467113d715 366:26545fe719ca
5 # GET /1.txt 5 # GET /1.txt
6 def show 6 def show
7 @paste = Paste.safe_find(params[:id]) 7 @paste = Paste.safe_find(params[:id])
8 return head :not_found unless @paste 8 return head :not_found unless @paste
9 9
10 expires_in 1.month, :public => true
11 respond_to do |format| 10 respond_to do |format|
12 format.html { cache } 11 format.html
13 format.txt { cache } 12 format.txt
14 end 13 end
15 end 14 end
16 15
17 # GET / 16 # GET /
18 def new 17 def new
55 end 54 end
56 55
57 def destroy 56 def destroy
58 @paste = Paste.safe_find(params[:id]) 57 @paste = Paste.safe_find(params[:id])
59 if @paste.safe_destroy(params[:paste][:key]) 58 if @paste.safe_destroy(params[:paste][:key])
60 uncache
61 redirect_to root_path, :notice => "Paste ##{params[:id]} deleted" 59 redirect_to root_path, :notice => "Paste ##{params[:id]} deleted"
62 else 60 else
63 flash.now[:alert] = @paste.errors.full_messages.to_sentence 61 flash.now[:alert] = @paste.errors.full_messages.to_sentence
64 render :show 62 render :show
65 end 63 end
72 unless correct_path == request.fullpath 70 unless correct_path == request.fullpath
73 redirect_to correct_path, :status => :moved_permanently 71 redirect_to correct_path, :status => :moved_permanently
74 end 72 end
75 end 73 end
76 74
77 def cache_key(format = request.format.symbol)
78 ext = case format
79 when :txt
80 ".txt"
81 when :html, nil
82 ""
83 else
84 ".unknown"
85 end
86 "pastes:#{@paste.to_param}#{ext}"
87 end
88
89 def cache
90 Rails.cache.fetch cache_key, :raw => true do
91 render_to_string :show
92 end
93 end
94
95 def uncache
96 [:txt, :html, :unknown].each do |format|
97 Rails.cache.delete cache_key(format)
98 end
99 end
100
101 def paste_params 75 def paste_params
102 params.require(:paste).permit(:paste, :paste_gzip, :paste_gzip_base64, :is_private, :key) 76 params.require(:paste).permit(:paste, :paste_gzip, :paste_gzip_base64, :is_private, :key)
103 end 77 end
104 end 78 end