Merge pull request #3593 from tgxworld/cache_results_on_about_page

Cache results on about page
This commit is contained in:
Sam
2015-07-21 10:04:51 +10:00
12 changed files with 121 additions and 19 deletions

View File

@@ -0,0 +1,10 @@
require 'spec_helper'
describe Jobs::AboutStats do
it 'caches the stats' do
stats = { "visited" => 10 }
About.any_instance.expects(:stats).returns(stats)
$redis.expects(:setex).with(About.stats_cache_key, 35.minutes, stats.to_json)
expect(described_class.new.execute({})).to eq(stats)
end
end

View File

@@ -0,0 +1,10 @@
require 'spec_helper'
describe Jobs::DashboardStats do
it 'caches the stats' do
json = { "visited" => 10 }
AdminDashboardData.any_instance.expects(:as_json).returns(json)
$redis.expects(:setex).with(AdminDashboardData.stats_cache_key, 35.minutes, json.to_json)
expect(described_class.new.execute({})).to eq(json)
end
end

View File

@@ -0,0 +1,9 @@
require 'spec_helper'
describe About do
describe 'stats cache' do
include_examples 'stats cachable'
end
end

View File

@@ -244,4 +244,8 @@ describe AdminDashboardData do
end
end
describe 'stats cache' do
include_examples 'stats cachable'
end
end

View File

@@ -0,0 +1,23 @@
shared_examples_for 'stats cachable' do
describe 'fetch_cached_stats' do
it 'returns the cached stats' do
begin
stats = { "visits" => 10 }
$redis.set(described_class.stats_cache_key, stats.to_json)
expect(described_class.fetch_cached_stats).to eq(stats)
ensure
$redis.del(described_class.stats_cache_key)
end
end
it 'returns nil if no stats has been cached' do
expect(described_class.fetch_cached_stats).to eq(nil)
end
end
describe 'fetch_stats' do
it 'has been implemented' do
expect{ described_class.fetch_stats }.to_not raise_error
end
end
end