Mercurial > zeropaste
comparison app/controllers/pastes_controller.rb @ 320:612bc7c32324
Use memcached for caching.
author | edogawaconan <me@myconan.net> |
---|---|
date | Sun, 06 Apr 2014 03:04:43 +0900 |
parents | 61f7f258a6fb |
children | fec39456dcbe |
comparison
equal
deleted
inserted
replaced
319:4944db0f98d8 | 320:612bc7c32324 |
---|---|
1 class PastesController < ApplicationController | 1 class PastesController < ApplicationController |
2 caches_page :show | 2 before_action :lowercase_path, :only => :show |
3 | 3 |
4 # GET /1 | 4 # GET /1 |
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 head :not_found and return unless @paste | 8 head :not_found and return unless @paste |
9 | 9 |
10 expires_in 1.year, :public => true | |
11 respond_to do |format| | 10 respond_to do |format| |
12 format.html # show.html.erb | 11 format.html { cache } |
13 format.txt # show.txt.erb | 12 format.txt { cache } |
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 expire_page :controller => 'pastes', :action => 'show', :id => @paste.id | 59 uncache |
61 redirect_to root_path, :notice => "Paste ##{params[:id]} deleted" | 60 redirect_to root_path, :notice => "Paste ##{params[:id]} deleted" |
62 else | 61 else |
63 flash.now[:alert] = @paste.errors.full_messages.to_sentence | 62 flash.now[:alert] = @paste.errors.full_messages.to_sentence |
64 render :show | 63 render :show |
65 end | 64 end |
66 end | 65 end |
67 | 66 |
68 private | 67 private |
68 | |
69 def lowercase_path | |
70 correct_path = request.fullpath.downcase | |
71 redirect_to correct_path, :status => :moved_permanently unless | |
72 correct_path == request.fullpath | |
73 end | |
74 | |
75 def cache_key(format = request.format) | |
76 ext = case format | |
77 when Mime::TXT | |
78 ".txt" | |
79 when Mime::HTML | |
80 "" | |
81 else | |
82 ".unknown" | |
83 end | |
84 "pastes:#{@paste.to_param}#{ext}" | |
85 end | |
86 | |
87 def cache | |
88 Rails.cache.fetch cache_key, :raw => true do | |
89 render_to_string :show | |
90 end | |
91 end | |
92 | |
93 def uncache | |
94 [Mime::TXT, Mime::HTML, "others"].each do |format| | |
95 Rails.cache.delete cache_key(format) | |
96 end | |
97 end | |
98 | |
69 def paste_params | 99 def paste_params |
70 params.require(:paste).permit(:paste, :paste_gzip, :paste_gzip_base64, :is_private, :key) | 100 params.require(:paste).permit(:paste, :paste_gzip, :paste_gzip_base64, :is_private, :key) |
71 end | 101 end |
72 end | 102 end |