using memcached_get() in libmemcached without value_length

2.3k views Asked by At

so I got a question very much related to: question with memcached_get() in libmemcached

it doesn't seem apparent or possible to me from the documentation. is it possible to get a value based on a key without knowing the length of the data? i plan on having multiple threads changing the value of the keys and can't therefore save what the previous length of a value was. I tried setting *value_length to NULL, 0 or an arbitrary high number without effect (eg. program crashed). I was hoping this could be achived with null-terminated strings, but it don't know the internals of libmemcached. any suggestions, pointers? thnx.

 char *memcached_get (memcached_st *ptr,
                 const char *key, size_t key_length,
                 size_t *value_length,
                 uint32_t *flags,
                 memcached_return *error);

some better documentation: http://dev.mysql.com/doc/refman/5.1/en/ha-memcached-interfaces-libmemcached.html#ha-memcached-interfaces-libmemcached-get

2

There are 2 answers

0
jcfrei On

ok, thanks to the terrific [/sarcasm] documentation or maybe my inexperience in C++, there has been a misunderstanding regarding the memcached_get function. according to

http://docs.oracle.com/cd/E17952_01/refman-5.0-en/ha-memcached-interfaces-libmemcached.html#ha-memcached-interfaces-libmemcached-get

the function uses size_t *value_length to store the size of the returned value. so write your function like this:

    memcached_st *memc;
    memcached_return rc;
    memcached_return error;
    memc= memcached_create(NULL);
    servers= memcached_server_list_append(servers, "localhost", 11211, &rc);
    rc= memcached_server_push(memc, servers);
    uint32_t flags;
    size_t return_value_length;
    const char *key = "123";

 const char *response  = memcached_get(memc,
                 key, strlen (key),
                 &return_value_length,
                 &flags,
                 &error);
0
user2548100 On

Memcached is a key-value machine. You hand it a key, of strlen(key), and it returns to you a pointer (response) to the object it paired with your key. It malloc()s storage to put the object in, and gives you the size of that object via return_value_length, whose address you passed to memcached as &return_value_len.