# HG changeset patch # User edogawaconan # Date 1396721083 -32400 # Node ID 612bc7c32324786ad2e4314ff5d2643a55f4c19e # Parent 4944db0f98d8ceaf656ece7d387c01158fc7b56f Use memcached for caching. diff -r 4944db0f98d8 -r 612bc7c32324 Gemfile --- a/Gemfile Sun Apr 06 01:23:15 2014 +0900 +++ b/Gemfile Sun Apr 06 03:04:43 2014 +0900 @@ -9,7 +9,7 @@ gem "pg", :platforms => :ruby gem "activerecord-jdbcpostgresql-adapter", :platforms => :jruby gem "jruby-openssl", :platforms => :jruby -gem "actionpack-page_caching" +gem "dalli" gem "sass-rails", "~> 4.0.0.rc1" diff -r 4944db0f98d8 -r 612bc7c32324 Gemfile.lock --- a/Gemfile.lock Sun Apr 06 01:23:15 2014 +0900 +++ b/Gemfile.lock Sun Apr 06 03:04:43 2014 +0900 @@ -10,8 +10,6 @@ activesupport (= 4.1.0.rc2) rack (~> 1.5.2) rack-test (~> 0.6.2) - actionpack-page_caching (1.0.2) - actionpack (>= 4.0.0, < 5) actionview (4.1.0.rc2) activesupport (= 4.1.0.rc2) builder (~> 3.1) @@ -40,6 +38,7 @@ arel (5.0.0) bouncy-castle-java (1.5.0147) builder (3.2.2) + dalli (2.7.0) erubis (2.7.0) execjs (2.0.2) hike (1.2.3) @@ -130,9 +129,9 @@ ruby DEPENDENCIES - actionpack-page_caching activerecord-jdbcpostgresql-adapter anjlab-bootstrap-rails + dalli hooves jquery-rails jruby-openssl diff -r 4944db0f98d8 -r 612bc7c32324 app/controllers/pastes_controller.rb --- a/app/controllers/pastes_controller.rb Sun Apr 06 01:23:15 2014 +0900 +++ b/app/controllers/pastes_controller.rb Sun Apr 06 03:04:43 2014 +0900 @@ -1,5 +1,5 @@ class PastesController < ApplicationController - caches_page :show + before_action :lowercase_path, :only => :show # GET /1 # GET /1.txt @@ -7,10 +7,9 @@ @paste = Paste.safe_find(params[:id]) head :not_found and return unless @paste - expires_in 1.year, :public => true respond_to do |format| - format.html # show.html.erb - format.txt # show.txt.erb + format.html { cache } + format.txt { cache } end end @@ -57,7 +56,7 @@ def destroy @paste = Paste.safe_find(params[:id]) if @paste.safe_destroy(params[:paste][:key]) - expire_page :controller => 'pastes', :action => 'show', :id => @paste.id + uncache redirect_to root_path, :notice => "Paste ##{params[:id]} deleted" else flash.now[:alert] = @paste.errors.full_messages.to_sentence @@ -66,6 +65,37 @@ end private + + def lowercase_path + correct_path = request.fullpath.downcase + redirect_to correct_path, :status => :moved_permanently unless + correct_path == request.fullpath + end + + def cache_key(format = request.format) + ext = case format + when Mime::TXT + ".txt" + when Mime::HTML + "" + else + ".unknown" + end + "pastes:#{@paste.to_param}#{ext}" + end + + def cache + Rails.cache.fetch cache_key, :raw => true do + render_to_string :show + end + end + + def uncache + [Mime::TXT, Mime::HTML, "others"].each do |format| + Rails.cache.delete cache_key(format) + end + end + def paste_params params.require(:paste).permit(:paste, :paste_gzip, :paste_gzip_base64, :is_private, :key) end diff -r 4944db0f98d8 -r 612bc7c32324 config/application.rb --- a/config/application.rb Sun Apr 06 01:23:15 2014 +0900 +++ b/config/application.rb Sun Apr 06 03:04:43 2014 +0900 @@ -63,13 +63,13 @@ # Version of your assets, change this if you want to expire all your assets config.assets.version = '1.0' - # Save cache in different location to avoid collision. - config.action_controller.page_cache_directory = config.root.join('public', 'cache') - - # Enable compression for page caches. - config.action_controller.page_cache_compression = :best_compression - # No need to initialize application when precompiling assets config.assets.initialize_on_precompile = false + + if ENV["ZP_MEMCACHE"] + config.cache_store = :dalli_store, ENV["ZP_MEMCACHE"].split(","), { :namespace => "zeropaste" } + else + config.cache_store = :null_store + end end end