diff --git a/ext/taj/taj.c b/ext/taj/taj.c index 621f8c7..1731365 100644 --- a/ext/taj/taj.c +++ b/ext/taj/taj.c @@ -192,6 +192,36 @@ rb_connection_add(VALUE self, VALUE rb_key, VALUE rb_value, VALUE ttl, VALUE fla return (rc == MEMCACHED_SUCCESS); } +static VALUE +rb_connection_inc(VALUE self, VALUE rb_key, VALUE rb_value) +{ + Taj_ctx *ctx = get_ctx(self); + char *mkey = StringValuePtr(rb_key); + uint32_t offset = NUM2UINT(rb_value); + uint64_t new_number; + + memcached_increment(ctx->memc, mkey, strlen(mkey), offset, &new_number); + // handle rc not success + // (rc == MEMCACHED_SUCCESS); + + return INT2NUM(new_number); +} + +static VALUE +rb_connection_dec(VALUE self, VALUE rb_key, VALUE rb_value) +{ + Taj_ctx *ctx = get_ctx(self); + char *mkey = StringValuePtr(rb_key); + uint32_t offset = NUM2UINT(rb_value); + uint64_t new_number; + + memcached_decrement(ctx->memc, mkey, strlen(mkey), offset, &new_number); + // handle rc not success + // (rc == MEMCACHED_SUCCESS); + + return INT2NUM(new_number); +} + void Init_taj(void) { rb_mTaj = rb_define_module("Taj"); @@ -207,6 +237,8 @@ void Init_taj(void) rb_define_method(cConnection, "get_multi", rb_connection_get_multi, 1); rb_define_method(cConnection, "delete", rb_connection_delete, 1); rb_define_method(cConnection, "add", rb_connection_add, 4); + rb_define_method(cConnection, "increment", rb_connection_inc, 2); + rb_define_method(cConnection, "decrement", rb_connection_dec, 2); Taj_Server = rb_define_class_under(rb_mTaj, "Server", rb_cObject); diff --git a/lib/memcached/client.rb b/lib/memcached/client.rb index 66489fd..d86789c 100644 --- a/lib/memcached/client.rb +++ b/lib/memcached/client.rb @@ -57,6 +57,14 @@ module Memcached connection.add(key, value, ttl, flags) end + def increment(key, offset = 1) + connection.increment(key, offset) + end + + def decrement(key, offset = 1) + connection.decrement(key, offset) + end + def connection @connection ||= Taj::Connection.new(@servers) end diff --git a/test/unit/client_inc_dec_test.rb b/test/unit/client_inc_dec_test.rb new file mode 100644 index 0000000..a19f48f --- /dev/null +++ b/test/unit/client_inc_dec_test.rb @@ -0,0 +1,47 @@ +require 'test_helper' + +class ClientIncDecTest < BaseTest + + def test_increment_simple + cache.set key, "10", raw: true + assert_equal 11, cache.increment(key) + end + +# def test_increment_binary +# @binary_protocol_cache.set key, "10", 0, false +# assert_equal 11, @binary_protocol_cache.increment(key) +# end + + def test_increment_offset + cache.set key, "10", raw: true + assert_equal 15, cache.increment(key, 5) + end + + def test_missing_increment + assert_equal(-1, cache.increment(key)) + end + + def test_decrement_simple + cache.set key, "10", raw: true + assert_equal 9, cache.decrement(key) + end + +# def test_decrement_binary +# @binary_protocol_cache.set key, "10", 0, false +# assert_equal 9, @binary_protocol_cache.decrement(key) +# end + + def test_decrement_offset + cache.set key, "10", raw: true + assert_equal 5, cache.decrement(key, 5) + end + + def test_decrement_with_negative + cache.set key, "10", raw: true + assert_equal 0, cache.decrement(key, 15) + end + + def test_missing_decrement + assert_equal(-1, cache.decrement(key)) + end +end diff --git a/test/unit/memcached_test.rb b/test/unit/memcached_test.rb index 03772af..34e1521 100644 --- a/test/unit/memcached_test.rb +++ b/test/unit/memcached_test.rb @@ -60,52 +60,6 @@ class MemcachedTest # TODO @marshalled_value = Marshal.dump(@value) end - # Increment and decrement - - def test_increment - @cache.set key, "10", 0, false - assert_equal 11, @cache.increment(key) - end - - def test_increment_binary - @binary_protocol_cache.set key, "10", 0, false - assert_equal 11, @binary_protocol_cache.increment(key) - end - - def test_increment_offset - @cache.set key, "10", 0, false - assert_equal 15, @cache.increment(key, 5) - end - - def test_missing_increment - @cache.delete key rescue nil - assert_raise(Memcached::NotFound) do - @cache.increment key - end - end - - def test_decrement - @cache.set key, "10", 0, false - assert_equal 9, @cache.decrement(key) - end - - def test_decrement_binary - @binary_protocol_cache.set key, "10", 0, false - assert_equal 9, @binary_protocol_cache.decrement(key) - end - - def test_decrement_offset - @cache.set key, "10", 0, false - assert_equal 5, @cache.decrement(key, 5) - end - - def test_missing_decrement - @cache.delete key rescue nil - assert_raise(Memcached::NotFound) do - @cache.decrement key - end - end - # Exist def test_missing_exist