Skip to content

Commit

Permalink
Changes to the controller methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Neighman committed Jun 22, 2008
1 parent e925df5 commit 5c997f8
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 70 deletions.
8 changes: 4 additions & 4 deletions lib/merb-cache/controller.rb
Expand Up @@ -39,25 +39,25 @@ module ControllerInstanceMethods

# Get from cache stores
# Cache stores will all return data or nil
def cache_get(key, store = :default)
def get(key, store = :default)
cached_data = Merb::Cache[store].get(key)
Merb.logger.info("cache: #{(cached_data.nil?) ? "miss" : "true" } (#{key})")
return cached_data
end

# Put, like a HTTP request
# Its the web kids
def cache_put(key, data, expiry, store = :default)
def put(key, data, expiry, store = :default)
expiry = expiry * 60 # expiry = 1 becomes 60
Merb::Cache[store].put(key, data, expiry)
Merb.logger.info("cache: set (#{key})")
end

def cache_cached?(key, store = :default)
def cached?(key, store = :default)
Merb::Cache[store].cached?(key)
end

def cache_expire!(key, store = :default)
def expire!(key, store = :default)
Merb::Cache[store].expire!(key)
end

Expand Down
14 changes: 7 additions & 7 deletions spec/cache_store_spec.rb
Expand Up @@ -5,26 +5,26 @@
end

it "should put a cache" do
@cache.cache_put("a-key", "some data", 10)
@cache.put("a-key", "some data", 10)
end

it "should get a cache" do
@cache.cache_get("a-key").should eql("some data")
@cache.get("a-key").should eql("some data")
end

it "should know when a cache exists" do
@cache.cache_cached?("a-key").should be_true
@cache.cache_cached?("b-key").should_not be_true
@cache.cached?("a-key").should be_true
@cache.cached?("b-key").should_not be_true
end

it "should expire a cache" do
@cache.cache_expire!("a-key")
@cache.expire!("a-key")
end

it "should set validity in minutes" do
@cache.cache_put("c-key", "data", 1)
@cache.put("c-key", "data", 1)
sleep 1 # Seconds
@cache.cache_cached?("c-key").should be_true
@cache.cached?("c-key").should be_true
end

it "should allow a cache store to register itself" do
Expand Down
34 changes: 17 additions & 17 deletions spec/controller_spec.rb
Expand Up @@ -29,68 +29,68 @@
Merb::Cache.remove_active_cache!(:custom_cache)
end

describe "cache_get method" do
it{ @controller.should respond_to(:cache_get)}
describe "get method" do
it{ @controller.should respond_to(:get)}

it "should pass the call through to te cache 'get'" do
@cache.should_receive(:get).with("key").and_return "key"
@controller.cache_get("key")
@controller.get("key")
end

it "should pass the call through to the custom cache 'get'" do
@custom_cache.should_receive(:get).with("key").and_return "key"
@cache.should_not_receive(:get)
@controller.cache_get("key", :custom_cache)
@controller.get("key", :custom_cache)
end
end

describe "cache_put method" do
it{ @controller.should respond_to(:cache_put)}
describe "put method" do
it{ @controller.should respond_to(:put)}

it "should pass the put method on to the default cache" do
@cache.should_receive(:put).with("key", "value", an_instance_of(Integer))
@controller.cache_put("key", "value", 1)
@controller.put("key", "value", 1)
end

it "should pass the put method to the custom cache" do
@custom_cache.should_receive(:put).with("key", "value", an_instance_of(Integer))
@controller.cache_put("key", "value", 1, :custom_cache)
@controller.put("key", "value", 1, :custom_cache)
end

it "should cahnge the expiry from minutes to seconds" do
expiry = mock("expiry")
expiry.should_receive(:*).with(60).and_return(120)
@cache.should_receive(:put).with("key", "value", 120)
@controller.cache_put("key", "value", expiry)
@controller.put("key", "value", expiry)
end

end

describe "cache_cached? method" do
it{ @controller.should respond_to(:cache_cached?) }
describe "cached? method" do
it{ @controller.should respond_to(:cached?) }

it "should pass the onto the cached? method of the default store" do
@cache.should_receive(:cached?).with("key").and_return true
@controller.cache_cached?("key")
@controller.cached?("key")
end

it "should pass the onto the cached? method of the custom store" do
@custom_cache.should_receive(:cached?).with("key").and_return true
@controller.cache_cached?("key", :custom_cache)
@controller.cached?("key", :custom_cache)
end
end

describe "cache_expire! method" do
it{ @controller.should respond_to(:cache_expire!)}
describe "expire! method" do
it{ @controller.should respond_to(:expire!)}

it "should pass the call the default stores expire! method" do
@cache.should_receive(:expire!).with("key").and_return true
@controller.cache_expire!("key")
@controller.expire!("key")
end

it "should pass the call the custom stores expire! method" do
@custom_cache.should_receive(:expire!).with("key").and_return true
@controller.cache_expire!("key", :custom_cache)
@controller.expire!("key", :custom_cache)
end
end

Expand Down
90 changes: 48 additions & 42 deletions spec/merb_cache_spec.rb
@@ -1,53 +1,59 @@
require File.dirname(__FILE__) + '/spec_helper'

# I refuse to spell initialisation with a z, this is me; taking it back for the rest of us.
describe "merb-cache initialisation" do
it "should default to memcached cache store" do
@cache = Merb::Cache::Store.new
@cache.config[:store].should eql(:memcached)
end
describe "Merb::Cache::Controller" do

# I refuse to spell initialisation with a z, this is me; taking it back for the rest of us.
describe "merb-cache initialisation" do
it "should default to memcached cache store" do
@cache = Merb::Cache::Store.new
@cache.config[:store].should eql(:memcached)
end

it "should raise Merb::Cache:Store::NotFound for named cache stores that do not exist" do
Merb::Plugins.config[:merb_cache] = {
:store => 'fail'
}
lambda { Merb::Cache::Store.new }.should raise_error(Merb::Cache::Store::NotFound)
end
it "should raise Merb::Cache:Store::NotFound for named cache stores that do not exist" do
Merb::Plugins.config[:merb_cache] = {
:store => 'fail'
}
lambda { Merb::Cache::Store.new }.should raise_error(Merb::Cache::Store::NotFound)
end

it "should merge the default options when the default options are missing" do
Merb::Plugins.config[:merb_cache] = {
:fail => 'badconfigoptions'
}
it "should merge the default options when the default options are missing" do
Merb::Plugins.config[:merb_cache] = {
:fail => 'badconfigoptions'
}

end
end

it "should setup the default" do
Merb::Cache.remove_active_cache!(:default)
lambda do
Merb::Cache[:default]
end.should raise_error(Merb::Cache::Store::NotFound)
Merb::Cache.setup_default
Merb::Cache[:default].should be_a_kind_of(Merb::Cache::Store)
end
it "should setup the default" do
Merb::Cache.remove_active_cache!(:default)
lambda do
Merb::Cache[:default]
end.should raise_error(Merb::Cache::Store::NotFound)
Merb::Cache.setup_default
Merb::Cache[:default].should be_a_kind_of(Merb::Cache::Store)
end

it "should not overwrite the existing object" do
Merb::Cache.setup_default
obj = Merb::Cache[:default]
Merb::Cache.setup_default
Merb::Cache[:default].should equal(obj)
end
it "should not overwrite the existing object" do
Merb::Cache.setup_default
obj = Merb::Cache[:default]
Merb::Cache.setup_default
Merb::Cache[:default].should equal(obj)
end

it "should overwrite the existing cache with the ! version" do
Merb::Cache.setup_default
obj = Merb::Cache[:default]
Merb::Cache.setup_default!
Merb::Cache[:default].should_not equal(obj)
it "should overwrite the existing cache with the ! version" do
Merb::Cache.setup_default
obj = Merb::Cache[:default]
Merb::Cache.setup_default!
Merb::Cache[:default].should_not equal(obj)
end

end

describe "cache_path" do

end


end

describe "returned caches" do
it "should return the format it was stored in"
it "should only cache 200 responses"
describe "returned caches" do
it "should return the format it was stored in"
it "should only cache 200 responses"
end
end

0 comments on commit 5c997f8

Please sign in to comment.