Skip to content

Commit

Permalink
don't want to setup a seperate cache? just use a Hash
Browse files Browse the repository at this point in the history
  • Loading branch information
jduff committed Jul 3, 2009
1 parent e16ce4a commit f477181
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/handlers/handlers.rb
Expand Up @@ -33,11 +33,11 @@ def cache_class(name = nil)
end
end

%w(redis_handler memcache_handler).each do |handler|
%w(redis_handler memcache_handler hash_handler).each do |handler|
require File.expand_path(File.dirname(__FILE__) + "/#{handler}")
end

HANDLERS = [RedisHandler, MemCacheHandler]
HANDLERS = [RedisHandler, MemCacheHandler, HashHandler]

def self.cache_handler_for(info)
HANDLERS.detect{|handler| handler.handles?(info)}
Expand Down
13 changes: 13 additions & 0 deletions lib/handlers/hash_handler.rb
@@ -0,0 +1,13 @@
module Handlers
class HashHandler < Handler
cache_class "Hash"

def increment(key)
@cache[key] = (get(key)||0).to_i+1
end

def get(key)
@cache[key]
end
end
end
23 changes: 23 additions & 0 deletions test/test_api_throttling_hash.rb
@@ -0,0 +1,23 @@
require File.expand_path(File.dirname(__FILE__) + '/test_helper')

class TestApiThrottlingHash < Test::Unit::TestCase
include Rack::Test::Methods
include BasicTests
HASH = Hash.new

def app
app = Rack::Builder.new {
use ApiThrottling, :requests_per_hour => 3, :cache => HASH, :read_method=>"get", :write_method=>"add"
run lambda {|env| [200, {'Content-Type' => 'text/plain', 'Content-Length' => '12'}, ["Hello World!"] ] }
}
end

def setup
HASH.replace({})
end

def test_cache_handler_should_be_memcache
assert_equal "Handlers::HashHandler", app.to_app.instance_variable_get(:@handler).to_s
end

end

0 comments on commit f477181

Please sign in to comment.