Merge pull request #140 from hussainnaqvee/patch-1
[awesomized/libmemcached] / libmemcached / memcached_set.rst
1 Storing data on the server
2 ==========================
3
4 .. index:: object: memcached_st
5
6 SYNOPSIS
7 --------
8
9 #include <libmemcached/memcached.h>
10 Compile and link with -lmemcached
11
12 .. function:: memcached_return_t memcached_set(memcached_st *ptr, const char *key, size_t key_length, const char *value, size_t value_length, time_t expiration, uint32_t flags)
13
14 .. function:: memcached_return_t memcached_add(memcached_st *ptr, const char *key, size_t key_length, const char *value, size_t value_length, time_t expiration, uint32_t flags)
15
16 .. function:: memcached_return_t memcached_replace(memcached_st *ptr, const char *key, size_t key_length, const char *value, size_t value_length, time_t expiration, uint32_t flags)
17
18 .. function:: memcached_return_t memcached_set_by_key(memcached_st *ptr, const char *group_key, size_t group_key_length, const char *key, size_t key_length, const char *value, size_t value_length, time_t expiration, uint32_t flags)
19
20 .. function:: memcached_return_t memcached_add_by_key(memcached_st *ptr, const char *group_key, size_t group_key_length, const char *key, size_t key_length, const char *value, size_t value_length, time_t expiration, uint32_t flags)
21
22 .. function:: memcached_return_t memcached_replace_by_key(memcached_st *ptr, const char *group_key, size_t group_key_length, const char *key, size_t key_length, const char *value, size_t value_length, time_t expiration, uint32_t flags)
23
24 DESCRIPTION
25 -----------
26
27 :func:`memcached_set`, :func:`memcached_add`, and :func:`memcached_replace` are
28 all used to store information on the server. All methods take a key, and its
29 length to store the object. Keys are currently limited to 250 characters when
30 using either a version of :manpage:`memcached(1)` which is 1.4 or below, or when
31 using the text protocol. You must supply both a value and a length. Optionally
32 you store the object. Keys are currently limited to 250 characters by the
33 memcached(1) server. You must supply both a value and a length. Optionally you
34 may test an expiration time for the object and a 16 byte value (it is meant to
35 be used as a bitmap). "flags" is a 4byte space that is stored alongside of the
36 main value. Many sub libraries make use of this field, so in most cases users
37 should avoid making use of it.
38
39 :func:`memcached_set` will write an object to the server. If an object
40 already exists it will overwrite what is in the server. If the object does not
41 exist it will be written. If you are using the non-blocking mode this function
42 will always return true unless a network error occurs.
43
44 :func:`memcached_replace` replaces an object on the server. If the object is not
45 found on the server an error occurs.
46
47 :func:`memcached_add` adds an object to the server. If the object is found on
48 the server an error occurs, otherwise the value is stored.
49
50 :func:`memcached_set_by_key`, :func:`memcached_add_by_key`, and
51 :func:`memcached_replace_by_key` methods all behave in a similar method as the non
52 key methods. The difference is that they use their group_key parameter to map
53 objects to particular servers.
54
55 If you are looking for performance, :func:`memcached_set` with non-blocking IO
56 is the fastest way to store data on the server.
57
58 All of the above functions are tested with the `MEMCACHED_BEHAVIOR_USE_UDP`
59 behavior enabled. However, when using these operations with this behavior
60 on, there are limits to the size of the payload being sent to the server.
61 The reason for these limits is that the Memcached Server does not allow
62 multi-datagram requests and the current server implementation sets a datagram
63 size to 1400 bytes. Due to protocol overhead, the actual limit of the user
64 supplied data is less than 1400 bytes and depends on the protocol in use, as
65 well as the operation being executed. When running with the binary protocol,
66 `MEMCACHED_BEHAVIOR_BINARY_PROTOCOL`, the size of the key,value, flags and
67 expiry combined may not exceed 1368 bytes. When running with the ASCII protocol,
68 the exact limit fluctuates depending on which function is being executed and
69 whether the function is a cas operation or not. For non-cas ASCII set operations,
70 there are at least 1335 bytes available to split among the key, key_prefix, and
71 value; for cas ASCII operations there are at least 1318 bytes available to split
72 among the key, key_prefix and value. If the total size of the command, including
73 overhead, exceeds 1400 bytes, a `MEMCACHED_WRITE_FAILURE` will be returned.
74
75 RETURN VALUE
76 ------------
77
78 All methods return a value of type `memcached_return_t`.
79
80 On success the value will be `MEMCACHED_SUCCESS`.
81 Use :func:`memcached_strerror` to translate this value to a printable string.
82
83 For :func:`memcached_replace` and :func:`memcached_add`, `MEMCACHED_NOTSTORED`
84 is a legitimate error in the case of a collision.
85
86 SEE ALSO
87 --------
88
89 .. only:: man
90
91 :manpage:`memcached(1)`
92 :manpage:`libmemcached(3)`
93 :manpage:`memcached_strerror(3)`
94 :manpage:`memcached_prepend(3)`
95 :manpage:`memcached_append(3)`
96 :manpage:`memcached_cas(3)`
97
98 .. only:: html
99
100 * :manpage:`memcached(1)`
101 * :doc:`../libmemcached`
102 * :doc:`memcached_strerror`
103 * :doc:`memcached_auto`
104 * :doc:`memcached_cas`