This repository has been archived by the owner on Dec 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding some initial functionality to properly read/write to the cache
- Loading branch information
Showing
4 changed files
with
61 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,15 @@ | ||
module RenderCaching | ||
module ControllerAdditions | ||
private | ||
|
||
def render_with_cache(key = nil) | ||
key ||= request.request_uri | ||
result = Rails.cache.fetch(key) { response.body } | ||
render :text => Rails.cache.read(key) | ||
end | ||
end | ||
end | ||
|
||
class ActionController::Base | ||
include RenderCaching::ControllerAdditions | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,49 @@ | ||
require File.dirname(__FILE__) + '/../spec_helper' | ||
|
||
# stub the Rails module functionality | ||
RAILS_CACHE = ActiveSupport::Cache.lookup_store(:memory_store) | ||
module Rails | ||
def self.cache | ||
RAILS_CACHE | ||
end | ||
end | ||
|
||
describe ActionController::Base do | ||
it "should have render_with_cache private method" do | ||
ActionController::Base.new.private_methods.should include('render_with_cache') | ||
end | ||
end | ||
|
||
|
||
describe RenderCaching::ControllerAdditions do | ||
it "should add render_with_cache method to controller" | ||
include RenderCaching::ControllerAdditions | ||
|
||
before(:each) do | ||
Rails.cache.clear | ||
@request = stub | ||
@response = stub | ||
stubs(:request).returns(@request) | ||
stubs(:response).returns(@response) | ||
stubs(:render) | ||
end | ||
|
||
it "should read from the cache with request uri as key and render that text" do | ||
@request.stubs(:request_uri).returns('/foo/bar') | ||
Rails.cache.write('/foo/bar', 'page content') | ||
expects(:render).with(:text => 'page content') | ||
render_with_cache | ||
end | ||
|
||
it "should read from the cache with custom passed key and render that text" do | ||
@request.stubs(:request_uri).returns('my_key') | ||
Rails.cache.write('my_key', 'page content') | ||
expects(:render).with(:text => 'page content') | ||
render_with_cache 'my_key' | ||
end | ||
|
||
it "should save response.body to cache as key when not specified" do | ||
@response.stubs(:body).returns('content') | ||
render_with_cache 'some_key' | ||
Rails.cache.read('some_key').should == 'content' | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters