This commit is contained in:
Arthur Neves 2016-03-05 18:45:23 -05:00
Родитель 997dadee3f
Коммит 6d2ebab6ad
4 изменённых файлов: 87 добавлений и 46 удалений

Просмотреть файл

@ -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);

Просмотреть файл

@ -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

Просмотреть файл

@ -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

Просмотреть файл

@ -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