Skip to content

Commit

Permalink
cache full pages for logged-out visitors without tag filters
Browse files Browse the repository at this point in the history
  • Loading branch information
pushcx committed May 17, 2018
1 parent a59cf79 commit f278565
Show file tree
Hide file tree
Showing 10 changed files with 30 additions and 7 deletions.
18 changes: 11 additions & 7 deletions Gemfile
@@ -1,34 +1,38 @@
source "https://rubygems.org"

gem "json"
gem "rails", "~> 5.1"

gem "unicorn"

gem "mysql2", "~> 0.3.20"

# uncomment to use PostgreSQL
# gem "pg"

# rails
gem 'scenic'
gem 'scenic-mysql'
gem "activerecord-typedstore"

gem "uglifier", ">= 1.3.0"
gem "jquery-rails", "~> 4.3"
# js
gem "dynamic_form"
gem "jquery-rails", "~> 4.3"
gem "json"
gem "uglifier", ">= 1.3.0"

# deployment
gem "actionpack-page_caching"
gem "exception_notification"
gem "unicorn"

# security
gem "bcrypt", "~> 3.1.2"
gem "rotp"
gem "rqrcode"

# parsing
gem "nokogiri", ">= 1.7.2"
gem "htmlentities"
gem "commonmarker", "~> 0.14"

gem "activerecord-typedstore"

# for twitter-posting bot
gem "oauth"

Expand Down
3 changes: 3 additions & 0 deletions Gemfile.lock
Expand Up @@ -18,6 +18,8 @@ GEM
rack-test (>= 0.6.3)
rails-dom-testing (~> 2.0)
rails-html-sanitizer (~> 1.0, >= 1.0.2)
actionpack-page_caching (1.1.0)
actionpack (>= 4.0.0, < 6)
actionview (5.1.5)
activesupport (= 5.1.5)
builder (~> 3.1)
Expand Down Expand Up @@ -194,6 +196,7 @@ PLATFORMS
ruby

DEPENDENCIES
actionpack-page_caching
activerecord-typedstore
bcrypt (~> 3.1.2)
bullet
Expand Down
3 changes: 3 additions & 0 deletions app/controllers/application_controller.rb
Expand Up @@ -5,8 +5,11 @@ class ApplicationController < ActionController::Base

TRAFFIC_DECREMENTER = 0.50

# match this in your nginx config for bypassing the file cache
TAG_FILTER_COOKIE = :tag_filters

CACHE_PAGE = proc { @user.blank? && cookies[TAG_FILTER_COOKIE].blank? }

def authenticate_user
# eagerly evaluate, in case this triggers an IpSpoofAttackError
request.remote_ip
Expand Down
2 changes: 2 additions & 0 deletions app/controllers/comments_controller.rb
@@ -1,6 +1,8 @@
class CommentsController < ApplicationController
COMMENTS_PER_PAGE = 20

caches_page :index, :threads, if: CACHE_PAGE

# for rss feeds, load the user's tag filters if a token is passed
before_action :find_user_from_rss_token, :only => [:index]
before_action :require_logged_in_user_or_400,
Expand Down
2 changes: 2 additions & 0 deletions app/controllers/home_controller.rb
@@ -1,4 +1,6 @@
class HomeController < ApplicationController
caches_page :about, :chat, :index, :newest, :newest_by_user, :recent, :top, if: CACHE_PAGE

# for rss feeds, load the user's tag filters if a token is passed
before_action :find_user_from_rss_token, :only => [:index, :newest, :saved]
before_action { @page = page }
Expand Down
2 changes: 2 additions & 0 deletions app/controllers/stories_controller.rb
@@ -1,4 +1,6 @@
class StoriesController < ApplicationController
caches_page :show, if: CACHE_PAGE

before_action :require_logged_in_user_or_400,
:only => [:upvote, :downvote, :unvote, :hide, :unhide, :preview, :save, :unsave]
before_action :require_logged_in_user,
Expand Down
2 changes: 2 additions & 0 deletions config/environments/production.rb
Expand Up @@ -75,6 +75,8 @@

# Do not dump schema after migrations.
config.active_record.dump_schema_after_migration = false

config.action_controller.page_cache_directory = "#{Rails.root}/public/cache"
end

%w{render_template render_partial render_collection}.each do |event|
Expand Down
1 change: 1 addition & 0 deletions config/initializers/session_store.rb
@@ -1,5 +1,6 @@
# Be sure to restart your server when you modify this file.

# match this in your nginx config for bypassing the file cache
Lobsters::Application.config.session_store :cookie_store,
key: 'lobster_trap',
expire_after: 1.month
Empty file added public/cache/.gitkeep
Empty file.
4 changes: 4 additions & 0 deletions script/expire_page_cache
@@ -0,0 +1,4 @@
#!/bin/sh

find /srv/lobste.rs/http/public/cache/ -type f -not -mmin 2 -not -name ".gitkeep" -delete
find /srv/lobste.rs/http/public/cache/ -type d -empty -delete

0 comments on commit f278565

Please sign in to comment.