5 Examples for libmemcached
12 For full examples, test cases are found in tests/\*.c in the main
13 distribution. These are always up to date, and are used for each test run of
16 ------------------------------
17 Creating and Freeing structure
18 ------------------------------
24 memcached_return_t rc;
26 memc= memcached_create(NULL);
31 The above code would create a connection and then free the connection when
43 const char *config_string= "--SERVER=host10.example.com --SERVER=host11.example.com --SERVER=host10.example.com"
44 memcached_st *memc= memcached_create_with_options(config_string, strlen(config_string);
51 In the above code you create a \ ``memcached_st``\ object with three server by making use of :manpage:`memcached_create_with_options(3)`.
54 ----------------------------
55 Adding a value to the server
56 ----------------------------
64 size_t value_length= 8191;
67 value = (char*)malloc(value_length);
70 for (x= 0; x < value_length; x++)
71 value[x] = (char) (x % 127);
73 for (x= 0; x < 1; x++)
75 rc= memcached_set(memc, key, strlen(key),
77 (time_t)0, (uint32_t)0);
78 assert(rc == MEMCACHED_SUCCESS);
84 It is best practice to always look at the return value of any operation.
87 ------------------------
88 Fetching multiple values
89 ------------------------
95 memcached_return_t rc;
96 char *keys[]= {"fudge", "son", "food"};
97 size_t key_length[]= {5, 3, 4};
101 char return_key[MEMCACHED_MAX_KEY];
102 size_t return_key_length;
104 size_t return_value_length;
106 rc= memcached_mget(memc, keys, key_length, 3);
109 while ((return_value= memcached_fetch(memc, return_key, &return_key_length,
110 &return_value_length, &flags, &rc)))
117 Notice that you freed values returned from memcached_fetch(). The define
118 \ ``MEMCACHED_MAX_KEY``\ is provided for usage.
127 To find out more information please check:
128 `https://launchpad.net/libmemcached <https://launchpad.net/libmemcached>`_
136 :manpage:`memcached(1)`