Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Revert "remove cache"

This reverts commit ad2eb23.
  • Loading branch information...
commit 42e737bbd4f21d4d57dc75e90c6d746b1b11899d 1 parent ad2eb23
@siuying authored
Showing with 19 additions and 0 deletions.
  1. +2 −0  Gemfile
  2. +5 −0 Gemfile.lock
  3. +12 −0 lib/seinfeld/app.rb
View
2  Gemfile
@@ -2,6 +2,7 @@ source "http://rubygems.org"
gem "sinatra", "~> 1.2.0"
gem "haml", "~> 3.0.23"
+gem "dalli"
gem "yajl-ruby", "~> 0.7.8"
gem "activerecord", "~> 3.0.1"
@@ -11,6 +12,7 @@ gem "oauth2", "~> 0.1.0"
gem "typhoeus", "~> 0.1.31"
gem "rack-rewrite"
+gem "rack-cache"
group :production do
gem "pg"
View
5 Gemfile.lock
@@ -15,6 +15,7 @@ GEM
arel (1.0.1)
activesupport (~> 3.0.0)
builder (2.1.2)
+ dalli (2.0.3)
faraday (0.5.3)
addressable (~> 2.2.2)
multipart-post (~> 1.0.1)
@@ -29,6 +30,8 @@ GEM
multi_json (~> 0.0.4)
pg (0.13.2)
rack (1.2.1)
+ rack-cache (1.2)
+ rack (>= 0.4)
rack-rewrite (1.2.1)
running_man (0.2.1)
sinatra (1.2.8)
@@ -46,11 +49,13 @@ PLATFORMS
DEPENDENCIES
activerecord (~> 3.0.1)
+ dalli
faraday (~> 0.5.3)
haml (~> 3.0.23)
mustache (~> 0.11.2)
oauth2 (~> 0.1.0)
pg
+ rack-cache
rack-rewrite
running_man (~> 0.2.1)
sinatra (~> 1.2.0)
View
12 lib/seinfeld/app.rb
@@ -1,4 +1,5 @@
require 'sinatra/base'
+require "rack/cache"
require 'mustache/sinatra'
require 'oauth2'
require 'yajl'
@@ -13,6 +14,7 @@ module Views
class App < Sinatra::Base
register Mustache::Sinatra
+ use Rack::Cache
use Rack::Rewrite do
r301 %r{.*}, 'http://opensource.reality.hk$&', :if => Proc.new {|rack_env|
rack_env['SERVER_NAME'] != 'opensource.reality.hk'
@@ -49,6 +51,7 @@ class App < Sinatra::Base
end
get '/' do
+ cache_for 5.minutes
@recent_users = Seinfeld::User.best_current_streak
@alltime_users = Seinfeld::User.best_alltime_streak
haml :index
@@ -63,6 +66,7 @@ class App < Sinatra::Base
@user = Seinfeld::User.find_by_login(params[:name].downcase)
end
if @user
+ cache_for 1.hour
haml :widget
else
redirect '/'
@@ -152,6 +156,8 @@ def get_user_and_progressions(extra = 0, name = params[:name])
def show_user_calendar
@progressions = get_user_and_progressions(6)
if @user
+ etag @user.etag
+ cache_for 5.minutes
haml :show
else
redirect "/"
@@ -159,6 +165,7 @@ def show_user_calendar
end
def show_group_calendar
+ cache_for 5.minutes
@progressions = Set.new
@users = params[:names].split(',')
@users.each do |name|
@@ -168,6 +175,7 @@ def show_group_calendar
end
def show_user_json
+ cache_for 5.minutes
@progressions = get_user_and_progressions
json = {:days => @progressions.map { |p| p.to_s }.sort!, :longest_streak => @user.longest_streak, :current_streak => @user.current_streak}.to_json
if params[:callback]
@@ -210,6 +218,10 @@ def group_seinfeld
end
end
end
+
+ def cache_for(time)
+ cache_control :public, :max_age => time.to_i
+ end
end
def oauth
Please sign in to comment.
Something went wrong with that request. Please try again.