From f477181bcc8a24681fad07190c26d135e339a22f Mon Sep 17 00:00:00 2001 From: John Duff Date: Fri, 3 Jul 2009 15:27:12 -0400 Subject: [PATCH] don't want to setup a seperate cache? just use a Hash --- lib/handlers/handlers.rb | 4 ++-- lib/handlers/hash_handler.rb | 13 +++++++++++++ test/test_api_throttling_hash.rb | 23 +++++++++++++++++++++++ 3 files changed, 38 insertions(+), 2 deletions(-) create mode 100644 lib/handlers/hash_handler.rb create mode 100644 test/test_api_throttling_hash.rb diff --git a/lib/handlers/handlers.rb b/lib/handlers/handlers.rb index 98eb29f..61524b7 100644 --- a/lib/handlers/handlers.rb +++ b/lib/handlers/handlers.rb @@ -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)} diff --git a/lib/handlers/hash_handler.rb b/lib/handlers/hash_handler.rb new file mode 100644 index 0000000..83c5a9f --- /dev/null +++ b/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 \ No newline at end of file diff --git a/test/test_api_throttling_hash.rb b/test/test_api_throttling_hash.rb new file mode 100644 index 0000000..670f109 --- /dev/null +++ b/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