forked from luccastera/api-throttling
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
don't want to setup a seperate cache? just use a Hash
- Loading branch information
Showing
3 changed files
with
38 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 |
---|---|---|
@@ -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 |
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 |
---|---|---|
@@ -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 |