Apache Portable Runtime
apr_memcache.h
Go to the documentation of this file.
1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2  * contributor license agreements. See the NOTICE file distributed with
3  * this work for additional information regarding copyright ownership.
4  * The ASF licenses this file to You under the Apache License, Version 2.0
5  * (the "License"); you may not use this file except in compliance with
6  * the License. You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef APR_MEMCACHE_H
18 #define APR_MEMCACHE_H
19 
20 /**
21  * @file apr_memcache.h
22  * @brief Client interface for memcached
23  * @remark To use this interface you must have a separate memcached
24  * server running. See the memcached website at http://www.danga.com/memcached/
25  * for more information.
26  */
27 
28 #include "apr.h"
29 #include "apr_pools.h"
30 #include "apr_time.h"
31 #include "apr_strings.h"
32 #include "apr_network_io.h"
33 #include "apr_buckets.h"
34 #include "apr_ring.h"
35 #include "apr_reslist.h"
36 #include "apr_hash.h"
37 
38 #ifdef __cplusplus
39 extern "C" {
40 #endif /* __cplusplus */
41 
42 /**
43  * @defgroup APR_Util_MC Memcached Client Routines
44  * @ingroup APR
45  * @{
46  */
47 
48 /** Specifies the status of a memcached server */
49 typedef enum
50 {
51  APR_MC_SERVER_LIVE, /**< Server is alive and responding to requests */
52  APR_MC_SERVER_DEAD /**< Server is not responding to requests */
54 
55 /** Opaque memcache client connection object */
57 
58 /** Memcache Server Info Object */
61 {
62  const char *host; /**< Hostname of this Server */
63  apr_port_t port; /**< Port of this Server */
64  apr_memcache_server_status_t status; /**< @see apr_memcache_server_status_t */
65 #if APR_HAS_THREADS || defined(DOXYGEN)
66  apr_reslist_t *conns; /**< Resource list of actual client connections */
67 #else
68  apr_memcache_conn_t *conn;
69 #endif
70  apr_pool_t *p; /** Pool to use for private allocations */
71 #if APR_HAS_THREADS
73 #endif
74  apr_time_t btime;
75 };
76 
77 /* Custom hash callback function prototype, user for server selection.
78 * @param baton user selected baton
79 * @param data data to hash
80 * @param data_len length of data
81 */
82 typedef apr_uint32_t (*apr_memcache_hash_func)(void *baton,
83  const char *data,
84  const apr_size_t data_len);
85 
86 typedef struct apr_memcache_t apr_memcache_t;
87 
88 /* Custom Server Select callback function prototype.
89 * @param baton user selected baton
90 * @param mc memcache instance, use mc->live_servers to select a node
91 * @param hash hash of the selected key.
92 */
93 typedef apr_memcache_server_t* (*apr_memcache_server_func)(void *baton,
94  apr_memcache_t *mc,
95  const apr_uint32_t hash);
96 
97 /** Container for a set of memcached servers */
99 {
100  apr_uint32_t flags; /**< Flags, Not currently used */
101  apr_uint16_t nalloc; /**< Number of Servers Allocated */
102  apr_uint16_t ntotal; /**< Number of Servers Added */
103  apr_memcache_server_t **live_servers; /**< Array of Servers */
104  apr_pool_t *p; /** Pool to use for allocations */
105  void *hash_baton;
106  apr_memcache_hash_func hash_func;
107  void *server_baton;
108  apr_memcache_server_func server_func;
109 };
110 
111 /** Returned Data from a multiple get */
112 typedef struct
113 {
114  apr_status_t status;
115  const char* key;
116  apr_size_t len;
117  char *data;
118  apr_uint16_t flags;
120 
121 /**
122  * Creates a crc32 hash used to split keys between servers
123  * @param mc The memcache client object to use
124  * @param data Data to be hashed
125  * @param data_len Length of the data to use
126  * @return crc32 hash of data
127  * @remark The crc32 hash is not compatible with old memcached clients.
128  */
130  const char *data,
131  const apr_size_t data_len);
132 
133 /**
134  * Pure CRC32 Hash. Used by some clients.
135  */
136 APR_DECLARE(apr_uint32_t) apr_memcache_hash_crc32(void *baton,
137  const char *data,
138  const apr_size_t data_len);
139 
140 /**
141  * hash compatible with the standard Perl Client.
142  */
143 APR_DECLARE(apr_uint32_t) apr_memcache_hash_default(void *baton,
144  const char *data,
145  const apr_size_t data_len);
146 
147 /**
148  * Picks a server based on a hash
149  * @param mc The memcache client object to use
150  * @param hash Hashed value of a Key
151  * @return server that controls specified hash
152  * @see apr_memcache_hash
153  */
155  const apr_uint32_t hash);
156 
157 /**
158  * server selection compatible with the standard Perl Client.
159  */
162  apr_memcache_t *mc,
163  const apr_uint32_t hash);
164 
165 /**
166  * Adds a server to a client object
167  * @param mc The memcache client object to use
168  * @param server Server to add
169  * @remark Adding servers is not thread safe, and should be done once at startup.
170  * @warning Changing servers after startup may cause keys to go to
171  * different servers.
172  */
174  apr_memcache_server_t *server);
175 
176 
177 /**
178  * Finds a Server object based on a hostname/port pair
179  * @param mc The memcache client object to use
180  * @param host Hostname of the server
181  * @param port Port of the server
182  * @return Server with matching Hostname and Port, or NULL if none was found.
183  */
185  const char *host,
186  apr_port_t port);
187 
188 /**
189  * Enables a Server for use again
190  * @param mc The memcache client object to use
191  * @param ms Server to Activate
192  */
195 
196 
197 /**
198  * Disable a Server
199  * @param mc The memcache client object to use
200  * @param ms Server to Disable
201  */
204 
205 /**
206  * Creates a new Server Object
207  * @param p Pool to use
208  * @param host hostname of the server
209  * @param port port of the server
210  * @param min minimum number of client sockets to open
211  * @param smax soft maximum number of client connections to open
212  * @param max hard maximum number of client connections
213  * @param ttl time to live in microseconds of a client connection
214  * @param ns location of the new server object
215  * @see apr_reslist_create
216  * @remark min, smax, and max are only used when APR_HAS_THREADS
217  */
219  const char *host,
220  apr_port_t port,
221  apr_uint32_t min,
222  apr_uint32_t smax,
223  apr_uint32_t max,
224  apr_uint32_t ttl,
225  apr_memcache_server_t **ns);
226 /**
227  * Creates a new memcached client object
228  * @param p Pool to use
229  * @param max_servers maximum number of servers
230  * @param flags Not currently used
231  * @param mc location of the new memcache client object
232  */
234  apr_uint16_t max_servers,
235  apr_uint32_t flags,
236  apr_memcache_t **mc);
237 
238 /**
239  * Gets a value from the server, allocating the value out of p
240  * @param mc client to use
241  * @param p Pool to use
242  * @param key null terminated string containing the key
243  * @param baton location of the allocated value
244  * @param len length of data at baton
245  * @param flags any flags set by the client for this key
246  * @return
247  */
249  apr_pool_t *p,
250  const char* key,
251  char **baton,
252  apr_size_t *len,
253  apr_uint16_t *flags);
254 
255 
256 /**
257  * Add a key to a hash for a multiget query
258  * if the hash (*value) is NULL it will be created
259  * @param data_pool pool from where the hash and their items are created from
260  * @param key null terminated string containing the key
261  * @param values hash of keys and values that this key will be added to
262  * @return
263  */
265  const char* key,
266  apr_hash_t **values);
267 
268 /**
269  * Gets multiple values from the server, allocating the values out of p
270  * @param mc client to use
271  * @param temp_pool Pool used for temporary allocations. May be cleared inside this
272  * call.
273  * @param data_pool Pool used to allocate data for the returned values.
274  * @param values hash of apr_memcache_value_t keyed by strings, contains the
275  * result of the multiget call.
276  * @return
277  */
279  apr_pool_t *temp_pool,
280  apr_pool_t *data_pool,
281  apr_hash_t *values);
282 
283 /**
284  * Sets a value by key on the server
285  * @param mc client to use
286  * @param key null terminated string containing the key
287  * @param baton data to store on the server
288  * @param data_size length of data at baton
289  * @param timeout time in seconds for the data to live on the server
290  * @param flags any flags set by the client for this key
291  */
293  const char *key,
294  char *baton,
295  const apr_size_t data_size,
296  apr_uint32_t timeout,
297  apr_uint16_t flags);
298 
299 /**
300  * Adds value by key on the server
301  * @param mc client to use
302  * @param key null terminated string containing the key
303  * @param baton data to store on the server
304  * @param data_size length of data at baton
305  * @param timeout time for the data to live on the server
306  * @param flags any flags set by the client for this key
307  * @return APR_SUCCESS if the key was added, APR_EEXIST if the key
308  * already exists on the server.
309  */
311  const char *key,
312  char *baton,
313  const apr_size_t data_size,
314  apr_uint32_t timeout,
315  apr_uint16_t flags);
316 
317 /**
318  * Replaces value by key on the server
319  * @param mc client to use
320  * @param key null terminated string containing the key
321  * @param baton data to store on the server
322  * @param data_size length of data at baton
323  * @param timeout time for the data to live on the server
324  * @param flags any flags set by the client for this key
325  * @return APR_SUCCESS if the key was added, APR_EEXIST if the key
326  * did not exist on the server.
327  */
329  const char *key,
330  char *baton,
331  const apr_size_t data_size,
332  apr_uint32_t timeout,
333  apr_uint16_t flags);
334 /**
335  * Deletes a key from a server
336  * @param mc client to use
337  * @param key null terminated string containing the key
338  * @param timeout time for the delete to stop other clients from adding
339  */
341  const char *key,
342  apr_uint32_t timeout);
343 
344 /**
345  * Increments a value
346  * @param mc client to use
347  * @param key null terminated string containing the key
348  * @param n number to increment by
349  * @param nv new value after incrementing
350  */
352  const char *key,
353  apr_int32_t n,
354  apr_uint32_t *nv);
355 
356 /**
357  * Decrements a value
358  * @param mc client to use
359  * @param key null terminated string containing the key
360  * @param n number to decrement by
361  * @param new_value new value after decrementing
362  */
364  const char *key,
365  apr_int32_t n,
366  apr_uint32_t *new_value);
367 
368 /**
369  * Query a server's version
370  * @param ms server to query
371  * @param p Pool to allocate answer from
372  * @param baton location to store server version string
373  */
375  apr_pool_t *p,
376  char **baton);
377 
378 typedef struct
379 {
380  /** Version string of this server */
381  const char *version;
382  /** Process id of this server process */
383  apr_uint32_t pid;
384  /** Number of seconds this server has been running */
385  apr_uint32_t uptime;
386  /** current UNIX time according to the server */
388  /** The size of a pointer on the current machine */
389  apr_uint32_t pointer_size;
390  /** Accumulated user time for this process */
392  /** Accumulated system time for this process */
394  /** Current number of items stored by the server */
395  apr_uint32_t curr_items;
396  /** Total number of items stored by this server */
397  apr_uint32_t total_items;
398  /** Current number of bytes used by this server to store items */
399  apr_uint64_t bytes;
400  /** Number of open connections */
401  apr_uint32_t curr_connections;
402  /** Total number of connections opened since the server started running */
403  apr_uint32_t total_connections;
404  /** Number of connection structures allocated by the server */
405  apr_uint32_t connection_structures;
406  /** Cumulative number of retrieval requests */
407  apr_uint32_t cmd_get;
408  /** Cumulative number of storage requests */
409  apr_uint32_t cmd_set;
410  /** Number of keys that have been requested and found present */
411  apr_uint32_t get_hits;
412  /** Number of items that have been requested and not found */
413  apr_uint32_t get_misses;
414  /** Number of items removed from cache because they passed their
415  expiration time */
416  apr_uint64_t evictions;
417  /** Total number of bytes read by this server */
418  apr_uint64_t bytes_read;
419  /** Total number of bytes sent by this server */
420  apr_uint64_t bytes_written;
421  /** Number of bytes this server is allowed to use for storage. */
422  apr_uint32_t limit_maxbytes;
423  /** Number of threads the server is running (if built with threading) */
424  apr_uint32_t threads;
426 
427 /**
428  * Query a server for statistics
429  * @param ms server to query
430  * @param p Pool to allocate answer from
431  * @param stats location of the new statistics structure
432  */
434  apr_pool_t *p,
435  apr_memcache_stats_t **stats);
436 
437 
438 /** @} */
439 
440 #ifdef __cplusplus
441 }
442 #endif
443 
444 #endif /* APR_MEMCACHE_H */
apr_memcache_server_status_t
Definition: apr_memcache.h:49
APR Network library.
apr_uint16_t ntotal
Definition: apr_memcache.h:102
Definition: apr_memcache.h:98
apr_uint32_t total_connections
Definition: apr_memcache.h:403
apr_status_t apr_memcache_multgetp(apr_memcache_t *mc, apr_pool_t *temp_pool, apr_pool_t *data_pool, apr_hash_t *values)
apr_uint32_t flags
Definition: apr_memcache.h:100
apr_uint32_t apr_memcache_hash(apr_memcache_t *mc, const char *data, const apr_size_t data_len)
struct apr_thread_mutex_t apr_thread_mutex_t
Definition: apr_thread_mutex.h:41
apr_memcache_server_t * apr_memcache_find_server(apr_memcache_t *mc, const char *host, apr_port_t port)
apr_uint32_t apr_memcache_hash_default(void *baton, const char *data, const apr_size_t data_len)
Definition: apr_memcache.h:51
apr_uint16_t nalloc
Definition: apr_memcache.h:101
apr_uint64_t evictions
Definition: apr_memcache.h:416
void * hash_baton
Definition: apr_memcache.h:105
apr_uint32_t connection_structures
Definition: apr_memcache.h:405
apr_uint32_t limit_maxbytes
Definition: apr_memcache.h:422
apr_uint32_t apr_memcache_hash_crc32(void *baton, const char *data, const apr_size_t data_len)
apr_uint32_t threads
Definition: apr_memcache.h:424
Definition: apr_memcache.h:60
APR Hash Tables.
struct apr_reslist_t apr_reslist_t
Definition: apr_reslist.h:42
apr_status_t apr_memcache_incr(apr_memcache_t *mc, const char *key, apr_int32_t n, apr_uint32_t *nv)
apr_memcache_server_t * apr_memcache_find_server_hash_default(void *baton, apr_memcache_t *mc, const apr_uint32_t hash)
apr_uint32_t pointer_size
Definition: apr_memcache.h:389
apr_status_t apr_memcache_delete(apr_memcache_t *mc, const char *key, apr_uint32_t timeout)
apr_uint32_t get_hits
Definition: apr_memcache.h:411
APR-UTIL Resource List Routines.
apr_uint32_t get_misses
Definition: apr_memcache.h:413
apr_status_t apr_memcache_decr(apr_memcache_t *mc, const char *key, apr_int32_t n, apr_uint32_t *new_value)
apr_uint32_t curr_items
Definition: apr_memcache.h:395
apr_int64_t apr_time_t
Definition: apr_time.h:45
APR memory allocation.
const char * version
Definition: apr_memcache.h:381
apr_status_t apr_memcache_version(apr_memcache_server_t *ms, apr_pool_t *p, char **baton)
apr_time_t rusage_system
Definition: apr_memcache.h:393
void apr_memcache_add_multget_key(apr_pool_t *data_pool, const char *key, apr_hash_t **values)
apr_status_t apr_memcache_set(apr_memcache_t *mc, const char *key, char *baton, const apr_size_t data_size, apr_uint32_t timeout, apr_uint16_t flags)
apr_uint32_t curr_connections
Definition: apr_memcache.h:401
apr_status_t apr_memcache_disable_server(apr_memcache_t *mc, apr_memcache_server_t *ms)
apr_status_t apr_memcache_stats(apr_memcache_server_t *ms, apr_pool_t *p, apr_memcache_stats_t **stats)
#define APR_DECLARE(type)
Definition: apr.h:500
Definition: apr_memcache.h:52
APR-UTIL Buckets/Bucket Brigades.
apr_status_t apr_memcache_enable_server(apr_memcache_t *mc, apr_memcache_server_t *ms)
apr_status_t apr_memcache_getp(apr_memcache_t *mc, apr_pool_t *p, const char *key, char **baton, apr_size_t *len, apr_uint16_t *flags)
apr_uint64_t bytes_written
Definition: apr_memcache.h:420
APR Platform Definitions.
struct apr_hash_t apr_hash_t
Definition: apr_hash.h:52
apr_port_t port
Definition: apr_memcache.h:63
apr_memcache_server_t ** live_servers
Definition: apr_memcache.h:103
apr_status_t apr_memcache_create(apr_pool_t *p, apr_uint16_t max_servers, apr_uint32_t flags, apr_memcache_t **mc)
apr_uint32_t cmd_get
Definition: apr_memcache.h:407
Definition: apr_memcache.h:112
apr_uint32_t total_items
Definition: apr_memcache.h:397
apr_time_t time
Definition: apr_memcache.h:387
apr_status_t apr_memcache_add_server(apr_memcache_t *mc, apr_memcache_server_t *server)
struct apr_memcache_conn_t apr_memcache_conn_t
Definition: apr_memcache.h:56
Definition: apr_memcache.h:378
APR Strings library.
apr_time_t rusage_user
Definition: apr_memcache.h:391
apr_status_t apr_memcache_server_create(apr_pool_t *p, const char *host, apr_port_t port, apr_uint32_t min, apr_uint32_t smax, apr_uint32_t max, apr_uint32_t ttl, apr_memcache_server_t **ns)
apr_status_t apr_memcache_add(apr_memcache_t *mc, const char *key, char *baton, const apr_size_t data_size, apr_uint32_t timeout, apr_uint16_t flags)
apr_memcache_server_status_t status
Definition: apr_memcache.h:64
struct apr_pool_t apr_pool_t
Definition: apr_pools.h:60
apr_memcache_server_t * apr_memcache_find_server_hash(apr_memcache_t *mc, const apr_uint32_t hash)
int apr_status_t
Definition: apr_errno.h:44
apr_uint16_t apr_port_t
Definition: apr_network_io.h:257
APR Time Library.
apr_uint32_t cmd_set
Definition: apr_memcache.h:409
apr_uint64_t bytes_read
Definition: apr_memcache.h:418
apr_thread_mutex_t * lock
Definition: apr_memcache.h:72
apr_uint64_t bytes
Definition: apr_memcache.h:399
const char * host
Definition: apr_memcache.h:62
apr_uint32_t uptime
Definition: apr_memcache.h:385
apr_reslist_t * conns
Definition: apr_memcache.h:66
apr_uint32_t pid
Definition: apr_memcache.h:383
APR Rings.
apr_status_t apr_memcache_replace(apr_memcache_t *mc, const char *key, char *baton, const apr_size_t data_size, apr_uint32_t timeout, apr_uint16_t flags)