Skip to content
This repository has been archived by the owner on Dec 12, 2021. It is now read-only.

Commit

Permalink
adding some initial functionality to properly read/write to the cache
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanb committed Jun 19, 2008
1 parent e59cfea commit cc47b82
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 2 deletions.
3 changes: 2 additions & 1 deletion Manifest
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
CHANGELOG
lib/render_caching/controller_additions.rb
lib/render_caching.rb
LICENSE
Manifest
README
render-caching.gemspec
spec/render-caching/controller_additions_spec.rb
spec/render_caching/controller_additions_spec.rb
spec/spec_helper.rb
tasks/deployment.rake
tasks/spec.rake
Expand Down
10 changes: 10 additions & 0 deletions lib/render_caching/controller_additions.rb
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
46 changes: 45 additions & 1 deletion spec/render_caching/controller_additions_spec.rb
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
4 changes: 4 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
require 'rubygems'
require 'spec'
require 'active_support'
require 'active_record'
require 'action_controller'
require 'action_view'
require File.dirname(__FILE__) + '/../lib/render_caching.rb'

Spec::Runner.configure do |config|
Expand Down

0 comments on commit cc47b82

Please sign in to comment.