apr_memcache.h 17 KB

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