Skip to content

Commit 654545d

Browse files
authored
Merge pull request #70 from asalant/fix-action-cache-expiry
Bugfix: expire homepage cache on configured schedule
2 parents bd6194b + dbd5d67 commit 654545d

5 files changed

Lines changed: 60 additions & 3 deletions

File tree

Gemfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,13 @@ gem "acts-as-taggable-on", "2.0.6"
1919
gem 'airbrake', '~> 3.1'
2020
gem 'validates_email_format_of'
2121
gem 'rdoc'
22+
gem 'expiring_memory_store'
2223

2324
group :development, :test do
2425
gem 'annotate'
2526
gem 'test-unit'
2627
gem 'thoughtbot-shoulda'
28+
gem 'timecop'
2729
end
2830

2931
group :production do

Gemfile.lock

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ GEM
4444
rake (>= 0.8.7)
4545
builder (3.3.0)
4646
calendar_date_select (1.16.1)
47+
expiring_memory_store (0.1.2)
4748
haml (3.0.25)
4849
json (1.7.7)
4950
kgio (2.11.2)
@@ -57,6 +58,7 @@ GEM
5758
test-unit (3.1.9)
5859
power_assert
5960
thoughtbot-shoulda (2.11.1)
61+
timecop (0.9.10)
6062
tzinfo (0.3.61)
6163
unicorn (5.5.1)
6264
kgio (~> 2.6)
@@ -77,6 +79,7 @@ DEPENDENCIES
7779
annotate
7880
authorization!
7981
calendar_date_select (= 1.16.1)
82+
expiring_memory_store
8083
haml (= 3.0.25)
8184
json (= 1.7.7)
8285
mysql
@@ -85,6 +88,7 @@ DEPENDENCIES
8588
rdoc
8689
test-unit
8790
thoughtbot-shoulda
91+
timecop
8892
tzinfo (~> 0.3.61)
8993
unicorn
9094
validates_email_format_of

config/environments/production.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
# Use a different cache store in production
1919
# config.cache_store = :mem_cache_store
20+
require 'active_support/cache/expiring_memory_store'
21+
config.cache_store = :expiring_memory_store
2022

2123
# Enable serving of images, stylesheets, and javascripts from an asset server
2224
# config.action_controller.asset_host = "http://assets.example.com"

config/environments/test.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@
99
# Log error messages when you accidentally call methods on nil.
1010
config.whiny_nils = true
1111

12-
# Show full error reports and disable caching
12+
# Show full error reports and enable caching with ExpiringMemoryStore
1313
config.action_controller.consider_all_requests_local = true
14-
config.action_controller.perform_caching = false
14+
config.action_controller.perform_caching = true
15+
require 'active_support/cache/expiring_memory_store'
16+
config.cache_store = :expiring_memory_store
1517

1618
# Disable request forgery protection in test environment
1719
config.action_controller.allow_forgery_protection = false

test/functional/organizations_controller_test.rb

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,54 @@
11
require 'test_helper'
2+
require 'timecop'
23

34
class OrganizationsControllerTest < ActionController::TestCase
4-
5+
6+
def setup
7+
ActionController::Base.cache_store.clear
8+
end
9+
10+
def test_index_is_cached_for_anonymous_users
11+
get :index
12+
first_body = @response.body
13+
14+
User.current_user = nil
15+
Visit.create!(:person => people(:daryl), :arrived_at => Time.zone.now)
16+
17+
get :index
18+
second_body = @response.body
19+
20+
assert_equal first_body, second_body, "Expected cached response for anonymous user"
21+
end
22+
23+
def test_index_cache_is_not_used_for_logged_in_users
24+
login_as 'sfbk'
25+
26+
get :index
27+
first_body = @response.body
28+
29+
Visit.create!(:person => people(:daryl), :arrived_at => Time.zone.now)
30+
31+
get :index
32+
second_body = @response.body
33+
34+
assert_not_equal first_body, second_body, "Expected fresh response for logged-in user"
35+
end
36+
37+
def test_index_cache_expires
38+
get :index
39+
first_body = @response.body
40+
41+
User.current_user = nil
42+
Visit.create!(:person => people(:daryl), :arrived_at => Time.zone.now)
43+
44+
Timecop.travel(25.hours.from_now) do
45+
get :index
46+
second_body = @response.body
47+
48+
assert_not_equal first_body, second_body, "Expected cache to expire after 25 hours"
49+
end
50+
end
51+
552
def test_should_get_index
653
get :index
754
assert_response :success

0 commit comments

Comments
 (0)