Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Adds key expiration support for Redis client #1

Merged
merged 1 commit into from

2 participants

@carlosmoutinho
Collaborator

No description provided.

@fguillen fguillen merged commit 90be4c5 into master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 25, 2012
  1. @carlosmoutinho
This page is out of date. Refresh to see the latest.
View
4 lib/time_window_drop_collector/config.rb
@@ -5,8 +5,8 @@ def self.extract( block )
@opts
end
- def self.client( type, *opts )
- @opts[:client] = type
+ def self.client( type, opts=nil )
+ @opts[:client] = type
@opts[:client_opts] = opts
end
View
2  lib/time_window_drop_collector/wrappers/memcache.rb
@@ -4,7 +4,7 @@ class Memcache
attr_reader :client
def initialize( opts )
- @client = Dalli::Client.new( *opts )
+ @client = Dalli::Client.new( opts )
end
def incr( key, expire_time )
View
7 lib/time_window_drop_collector/wrappers/redis.rb
@@ -4,11 +4,14 @@ class Redis
attr_reader :client
def initialize( opts )
- @client = ::Redis.new( *opts )
+ @client = ::Redis.new( opts )
end
def incr( key, expire_time )
- client.incr( key )
+ client.multi do
+ client.incr( key )
+ client.expire( key, expire_time )
+ end
end
def values_for( keys )
View
10 test/config_test.rb
@@ -8,16 +8,16 @@ def setup
def test_config
proc =
Proc.new do
- client "client", "client_opt1", "client_opt2"
+ client "client", "client_opt1"
window "window"
slices "slices"
end
config = TimeWindowDropCollector::Config.extract( proc )
- assert_equal( "client", config[:client] )
- assert_equal( ["client_opt1", "client_opt2"], config[:client_opts] )
- assert_equal( "window", config[:window] )
- assert_equal( "slices", config[:slices] )
+ assert_equal( "client", config[:client] )
+ assert_equal( "client_opt1", config[:client_opts] )
+ assert_equal( "window", config[:window] )
+ assert_equal( "slices", config[:slices] )
end
end
View
33 test/redis_wrapper_test.rb
@@ -1,26 +1,47 @@
require_relative "test_helper"
-class Redis
- def initialize( *opts )
+class RedisMock
+ def multi(&block)
+ yield
end
+ def incr(key);end
+ def expire(key);end
+ def mget(keys);end
end
-class MemcacheWrapperTest < Test::Unit::TestCase
+class RedisWrapperTest < Test::Unit::TestCase
def test_initialize
- Redis.expects( :new ).with( "arg1", "arg2" ).returns( "client" )
+ Redis.expects( :new ).with( ["arg1", "arg2"] ).returns( "client" )
wrapper = TimeWindowDropCollector::Wrappers::Redis.new( ["arg1", "arg2"] )
+
assert_equal( "client", wrapper.client )
end
def test_incr
- wrapper = TimeWindowDropCollector::Wrappers::Redis.new( ["arg1"] )
+ Redis.expects( :new ).returns( RedisMock.new )
+ wrapper = TimeWindowDropCollector::Wrappers::Redis.new( nil )
+
wrapper.client.expects( :incr ).with( "key" )
+ wrapper.client.expects( :expire ).with( "key", "expire_time" )
+
wrapper.incr( "key", "expire_time" )
end
+ def test_incr_agregates_commands_under_multi
+ Redis.expects( :new ).returns( RedisMock.new )
+ wrapper = TimeWindowDropCollector::Wrappers::Redis.new( nil )
+
+ wrapper.client.expects( :multi )
+
+ wrapper.incr( nil, nil )
+ end
+
def test_values_for
- wrapper = TimeWindowDropCollector::Wrappers::Redis.new( ["arg1"] )
+ Redis.expects( :new ).returns( RedisMock.new )
+ wrapper = TimeWindowDropCollector::Wrappers::Redis.new( nil )
+
wrapper.client.expects( :mget ).with( "key1", "key2" ).returns( [1, 2] )
+
assert_equal( [1, 2], wrapper.values_for( ["key1", "key2"] ))
end
end
View
7 test/test_helper.rb
@@ -2,6 +2,7 @@
require "mocha"
require "delorean"
require "memcache_mock"
+require "redis"
require_relative "../lib/time_window_drop_collector.rb"
@@ -9,8 +10,4 @@
class Rails
def self.cache
end
-end
-
-class Redis
-end
-
+end
View
1  test/wrapper_test.rb
@@ -12,6 +12,7 @@ def test_instance_when_mock
end
def test_instance_when_redis
+ Redis.stubs( :new )
instance = TimeWindowDropCollector::Wrapper.instance( :redis, ["server", "port"] )
assert( instance.is_a? TimeWindowDropCollector::Wrappers::Redis )
end
View
1  time_window_drop_collector.gemspec
@@ -23,6 +23,7 @@ Gem::Specification.new do |s|
s.add_development_dependency "mocha", "0.10.0"
s.add_development_dependency "delorean", "1.2.0"
s.add_development_dependency "memcache_mock", "0.0.1"
+ s.add_development_dependency "redis", "2.2.2"
s.add_dependency "dalli"
end
Something went wrong with that request. Please try again.