memcached_server_add() now works so you can connect to host other then
[m6w6/libmemcached] / lib / memcached_storage.c
1 /*
2 Memcached library
3
4 memcached_set()
5 memcached_replace()
6 memcached_add()
7
8 */
9
10 #include <memcached.h>
11
12 static memcached_return memcached_send(memcached_st *ptr,
13 char *key, size_t key_length,
14 char *value, size_t value_length,
15 time_t expiration,
16 uint16_t flags,
17 char *verb)
18 {
19 size_t send_length;
20 memcached_return rc;
21 char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
22
23 rc= memcached_connect(ptr);
24
25 if (rc != MEMCACHED_SUCCESS)
26 return rc;
27
28 send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
29 "%s %.*s %u %u %u\r\n", verb,
30 key_length, key, flags, expiration, value_length);
31 if ((send(ptr->fd, buffer, send_length, 0) == -1))
32 {
33 fprintf(stderr, "failed set on %.*s TCP\n", key_length+1, key);
34
35 return MEMCACHED_WRITE_FAILURE;
36 }
37
38 send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
39 "%.*s\r\n",
40 value_length, value);
41 if ((send(ptr->fd, buffer, send_length, 0) == -1))
42 {
43 fprintf(stderr, "failed set on %.*s TCP\n", key_length+1, key);
44
45 return MEMCACHED_WRITE_FAILURE;
46 }
47
48 send_length= read(ptr->fd, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE);
49
50 if (send_length && buffer[0] == 'S') /* STORED */
51 return MEMCACHED_SUCCESS;
52 else if (send_length && buffer[0] == 'N') /* NOT_STORED */
53 return MEMCACHED_NOTSTORED;
54 else
55 return MEMCACHED_READ_FAILURE;
56 }
57
58 memcached_return memcached_set(memcached_st *ptr, char *key, size_t key_length,
59 char *value, size_t value_length,
60 time_t expiration,
61 uint16_t flags)
62 {
63 return memcached_send(ptr, key, key_length, value, value_length,
64 expiration, flags, "set");
65 }
66
67 memcached_return memcached_add(memcached_st *ptr, char *key, size_t key_length,
68 char *value, size_t value_length,
69 time_t expiration,
70 uint16_t flags)
71 {
72 return memcached_send(ptr, key, key_length, value, value_length,
73 expiration, flags, "add");
74 }
75
76 memcached_return memcached_replace(memcached_st *ptr, char *key, size_t key_length,
77 char *value, size_t value_length,
78 time_t expiration,
79 uint16_t flags)
80 {
81 return memcached_send(ptr, key, key_length, value, value_length,
82 expiration, flags, "replace");
83 }