question with memcached_get() in libmemcached

2019-07-26 05:03发布

问题:

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

Why need to pass the params "size_t key_length" and "size_t *value_length" here ? I think the value can be fetched by key directly.

who can help to tell me the reason, thanks.

回答1:

The key parameter is a pointer to an identifier for the information you want to get from memcache. The key_length tells the memcached_get() function how long your identifier data is.

If the libmemcache API assumed that the data pointed to by the key parameter was a NULL-terminated string of characters, then we wouldn't need to also pass in the key_length parameter. But by not making that assumption the API allows us to use data other than NULL-terminated char strings as keys (for example, UTF-16 strings, or binary numbers).