You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #ifndef MEMCACHED_H
  2. #define MEMCACHED_H
  3. #include <sys/types.h>
  4. #include <netinet/in.h>
  5. #include <sys/time.h>
  6. #include <time.h>
  7. #define MAXKEYLEN 250
  8. #define MEMC_OPT_DEBUG 0x1
  9. struct event;
  10. typedef enum memc_error {
  11. OK,
  12. BAD_COMMAND,
  13. CLIENT_ERROR,
  14. SERVER_ERROR,
  15. SERVER_TIMEOUT,
  16. NOT_EXISTS,
  17. EXISTS,
  18. WRONG_LENGTH
  19. } memc_error_t;
  20. /* XXX: Only UDP_TEXT is supported at present */
  21. typedef enum memc_proto {
  22. UDP_TEXT,
  23. TCP_TEXT,
  24. UDP_BIN,
  25. TCP_BIN
  26. } memc_proto_t;
  27. typedef enum memc_op {
  28. CMD_NULL,
  29. CMD_CONNECT,
  30. CMD_READ,
  31. CMD_WRITE,
  32. CMD_DELETE,
  33. } memc_opt_t;
  34. typedef struct memcached_param_s {
  35. char key[MAXKEYLEN];
  36. u_char *buf;
  37. size_t bufsize;
  38. size_t bufpos;
  39. int expire;
  40. } memcached_param_t;
  41. /* Port must be in network byte order */
  42. typedef struct memcached_ctx_s {
  43. memc_proto_t protocol;
  44. struct in_addr addr;
  45. uint16_t port;
  46. int sock;
  47. struct timeval timeout;
  48. /* Counter that is used for memcached operations in network byte order */
  49. uint16_t count;
  50. /* Flag that signalize that this memcached is alive */
  51. short alive;
  52. /* Options that can be specified for memcached connection */
  53. short options;
  54. /* Current operation */
  55. memc_opt_t op;
  56. /* Current command */
  57. const char *cmd;
  58. /* Current param */
  59. memcached_param_t *param;
  60. /* Callback for current operation */
  61. void (*callback) (struct memcached_ctx_s *ctx, memc_error_t error, void *data);
  62. /* Data for callback function */
  63. void *callback_data;
  64. /* Event structure */
  65. struct event mem_ev;
  66. } memcached_ctx_t;
  67. typedef void (*memcached_callback_t) (memcached_ctx_t *ctx, memc_error_t error, void *data);
  68. /*
  69. * Initialize connection to memcached server:
  70. * addr, port and timeout fields in ctx must be filled with valid values
  71. * Return:
  72. * 0 - success
  73. * -1 - error (error is stored in errno)
  74. */
  75. int memc_init_ctx (memcached_ctx_t *ctx);
  76. int memc_init_ctx_mirror (memcached_ctx_t *ctx, size_t memcached_num);
  77. /*
  78. * Memcached function for getting, setting, adding values to memcached server
  79. * ctx - valid memcached context
  80. * key - key to extract (max 250 characters as it specified in memcached API)
  81. * buf, elemsize, nelem - allocated buffer of length nelem structures each of elemsize
  82. * that would contain extracted data (NOT NULL TERMINATED)
  83. * Return:
  84. * memc_error_t
  85. * nelem is changed according to actual number of extracted data
  86. *
  87. * "set" means "store this data".
  88. *
  89. * "add" means "store this data, but only if the server *doesn't* already
  90. * hold data for this key".
  91. * "replace" means "store this data, but only if the server *does*
  92. * already hold data for this key".
  93. * "append" means "add this data to an existing key after existing data".
  94. * "prepend" means "add this data to an existing key before existing data".
  95. */
  96. #define memc_get(ctx, param) memc_read(ctx, "get", param)
  97. #define memc_set(ctx, param, expire) memc_write(ctx, "set", param, expire)
  98. #define memc_add(ctx, param, expire) memc_write(ctx, "add", param, expire)
  99. #define memc_replace(ctx, param, expire) memc_write(ctx, "replace", param, expire)
  100. #define memc_append(ctx, param, expire) memc_write(ctx, "append", param, expire)
  101. #define memc_prepend(ctx, param, expire) memc_write(ctx, "prepend", param, expire)
  102. /* Functions that works with mirror of memcached servers */
  103. #define memc_get_mirror(ctx, num, param) memc_read_mirror(ctx, num, "get", param)
  104. #define memc_set_mirror(ctx, num, param, expire) memc_write_mirror(ctx, num, "set", param, expire)
  105. #define memc_add_mirror(ctx, num, param, expire) memc_write_mirror(ctx, num, "add", param, expire)
  106. #define memc_replace_mirror(ctx, num, param, expire) memc_write_mirror(ctx, num, "replace", param, expire)
  107. #define memc_append_mirror(ctx, num, param, expire) memc_write_mirror(ctx, num, "append", param, expire)
  108. #define memc_prepend_mirror(ctx, num, param, expire) memc_write_mirror(ctx, num, "prepend", param, expire)
  109. memc_error_t memc_read (memcached_ctx_t *ctx, const char *cmd, memcached_param_t *param);
  110. memc_error_t memc_write (memcached_ctx_t *ctx, const char *cmd, memcached_param_t *param, int expire);
  111. memc_error_t memc_delete (memcached_ctx_t *ctx, memcached_param_t *params);
  112. memc_error_t memc_write_mirror (memcached_ctx_t *ctx, size_t memcached_num, const char *cmd, memcached_param_t *param, int expire);
  113. memc_error_t memc_read_mirror (memcached_ctx_t *ctx, size_t memcached_num, const char *cmd, memcached_param_t *param);
  114. memc_error_t memc_delete_mirror (memcached_ctx_t *ctx, size_t memcached_num, const char *cmd, memcached_param_t *param);
  115. /* Return symbolic name of memcached error*/
  116. const char * memc_strerror (memc_error_t err);
  117. /* Destroy socket from ctx */
  118. int memc_close_ctx (memcached_ctx_t *ctx);
  119. int memc_close_ctx_mirror (memcached_ctx_t *ctx, size_t memcached_num);
  120. #endif