Broke library up into multiple files.
[awesomized/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 send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
26 "%s %.*s %u %u %u\r\n", verb,
27 key_length, key, flags, expiration, value_length);
28 if ((send(ptr->fd, buffer, send_length, 0) == -1))
29 {
30 fprintf(stderr, "failed set on %.*s TCP\n", key_length+1, key);
31
32 return MEMCACHED_WRITE_FAILURE;
33 }
34
35 send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
36 "%.*s\r\n",
37 value_length, value);
38 if ((send(ptr->fd, buffer, send_length, 0) == -1))
39 {
40 fprintf(stderr, "failed set on %.*s TCP\n", key_length+1, key);
41
42 return MEMCACHED_WRITE_FAILURE;
43 }
44
45 send_length= read(ptr->fd, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE);
46
47 if (send_length && buffer[0] == 'S') /* STORED */
48 return MEMCACHED_SUCCESS;
49 else if (send_length && buffer[0] == 'N') /* NOT_STORED */
50 return MEMCACHED_NOTSTORED;
51 else
52 return MEMCACHED_READ_FAILURE;
53 }
54
55 memcached_return memcached_set(memcached_st *ptr, char *key, size_t key_length,
56 char *value, size_t value_length,
57 time_t expiration,
58 uint16_t flags)
59 {
60 return memcached_send(ptr, key, key_length, value, value_length,
61 expiration, flags, "set");
62 }
63
64 memcached_return memcached_add(memcached_st *ptr, char *key, size_t key_length,
65 char *value, size_t value_length,
66 time_t expiration,
67 uint16_t flags)
68 {
69 return memcached_send(ptr, key, key_length, value, value_length,
70 expiration, flags, "add");
71 }
72
73 memcached_return memcached_replace(memcached_st *ptr, char *key, size_t key_length,
74 char *value, size_t value_length,
75 time_t expiration,
76 uint16_t flags)
77 {
78 return memcached_send(ptr, key, key_length, value, value_length,
79 expiration, flags, "replace");
80 }