Skip to content

Instantly share code, notes, and snippets.

@brianmario
Last active January 12, 2017 23:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brianmario/c18ef7bea80d4651e245c32ef457ce5a to your computer and use it in GitHub Desktop.
Save brianmario/c18ef7bea80d4651e245c32ef457ce5a to your computer and use it in GitHub Desktop.
diff --git a/ext/memcached/memcached.c b/ext/memcached/memcached.c
index e4a0449..cec0731 100644
--- a/ext/memcached/memcached.c
+++ b/ext/memcached/memcached.c
@@ -197,6 +197,62 @@ rb_connection_set(VALUE self, VALUE rb_key, VALUE rb_value, VALUE rb_ttl, VALUE
}
static VALUE
+rb_connection_set_multi(VALUE self, VALUE rb_values)
+{
+ memcached_st *mc;
+ memcached_return_t rc;
+
+ UnwrapMemcached(self, mc);
+
+ Check_Type(rb_values, T_ARRAY);
+
+ // TODO: peform range check here to make sure the string's length doesn't
+ // overflow a uint32_t
+ uint32_t list_size = (uint32_t)RARRAY_LEN(rb_values);
+
+ rc = memcached_behavior_set(mc, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS, 1);
+
+ rb_memcached_error_check(rc);
+
+ char* last_key;
+ size_t last_key_len;
+
+ for(uint32_t i=0; i<list_size; i++) {
+ VALUE item = rb_ary_entry(rb_values, i);
+
+ Check_Type(item, T_HASH);
+
+ VALUE rb_key, rb_value;
+
+ // TODO: also grab other things the user can set here like cas, flags, etc.
+ rb_key = rb_hash_aref(item, ID2SYM(rb_intern("key")));
+ rb_value = rb_hash_aref(item, ID2SYM(rb_intern("value")));
+
+ StringValue(rb_key);
+ StringValue(rb_value);
+
+ last_key = RSTRING_PTR(rb_key);
+ last_key_len = RSTRING_LEN(rb_key);
+
+ rc = memcached_set(mc,
+ RSTRING_PTR(rb_key), RSTRING_LEN(rb_key),
+ RSTRING_PTR(rb_value), RSTRING_LEN(rb_value),
+ 0, 0
+ );
+ }
+
+ rc = memcached_behavior_set(mc, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS, 0);
+
+ rb_memcached_error_check(rc);
+
+ rc = memcached_exist(mc, last_key, last_key_len);
+
+ rb_memcached_error_check(rc);
+
+ return Qtrue;
+}
+
+static VALUE
rb_connection_get(VALUE self, VALUE rb_key)
{
VALUE rb_val;
@@ -528,6 +584,7 @@ void Init_memcached(void)
rb_define_method(rb_cConnection, "servers", rb_connection_servers, 0);
rb_define_method(rb_cConnection, "flush", rb_connection_flush, 0);
rb_define_method(rb_cConnection, "set", rb_connection_set, 4);
+ rb_define_method(rb_cConnection, "set_multi", rb_connection_set_multi, 1);
rb_define_method(rb_cConnection, "get", rb_connection_get, 1);
rb_define_method(rb_cConnection, "get_multi", rb_connection_get_multi, 1);
rb_define_method(rb_cConnection, "delete", rb_connection_delete, 1);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment