memcache.h
author Tero Marttila <terom@fixme.fi>
Wed, 27 Aug 2008 22:42:27 +0300
changeset 42 0e503189af2f
parent 41 540737bf6bac
child 43 e5b714190dee
permissions -rw-r--r--
more reply-receiving code, but still incomplete
#ifndef MEMCACHE_H
#define MEMCACHE_H

/*
 * A libevent based memcached client that aims for high performance, concurrency and low latency.
 */

#include "config.h"

/*
 * Used to store the global information for a memcache context. A context contains both servers and active connections.
 */
struct memcache;

/*
 * A transaction.
 */
struct memcache_req;

/*
 * Keys used
 */
struct memcache_key {
    char *buf;
    size_t len;
};

/*
 * Object attributes
 */
struct memcache_obj {
    unsigned int flags;
    time_t exptime;
    size_t bytes;
    unsigned long long cas;
};

/*
 * Object data
 */
struct memcache_buf {
    char *data;
    size_t len;
    size_t offset;
};

/*
 * Available commands
 */
enum memcache_command {
    MEMCACHE_CMD_INVALID,

    MEMCACHE_CMD_FETCH_GET,
    MEMCACHE_CMD_STORE_SET,
    MEMCACHE_CMD_STORE_ADD,
    MEMCACHE_CMD_STORE_REPLACE,
    MEMCACHE_CMD_STORE_APPEND,
    MEMCACHE_CMD_STORE_PREPEND,
    MEMCACHE_CMD_STORE_CAS,

    MEMCACHE_CMD_MAX,
};

enum memcache_reply {
    MEMCACHE_RPL_INVALID,

    MEMCACHE_RPL_ERROR,
    MEMCACHE_RPL_CLIENT_ERROR,
    MEMCACHE_RPL_SERVER_ERROR,
    
    // MEMCACHE_CMD_FETCH_*
    MEMCACHE_RPL_VALUE,
    MEMCACHE_RPL_END,
    
    // MEMCACHE_CMD_STORE_*
    MEMCACHE_RPL_STORED,
    MEMCACHE_RPL_NOT_STORED,
    MEMCACHE_RPL_EXISTS,
    MEMCACHE_RPL_NOT_FOUND,

    MEMCACHE_RPL_MAX,
};

enum memcache_req_state {
    MEMCACHE_STATE_INVALID,

    MEMCACHE_STATE_QUEUED,
    MEMCACHE_STATE_SEND,
    MEMCACHE_STATE_REPLY,

    MEMCACHE_STATE_ERROR,
};

/*
 * Callback used
 */
typedef int (*memcache_cb) (struct memcache_req *, void*);

/*
 * Allocate a new memcache context for use with other methods.
 */
struct memcache *memcache_alloc (memcache_cb cb_fn);

/*
 * Add a server to the pool of available servers.
 */
int memcache_add_server (struct memcache *mc, struct config_endpoint *endpoint, int max_connections);

/*
 * Attempt to fetch a key from the cache.
 */
struct memcache_req *memcache_fetch (struct memcache *mc, const struct memcache_key *key, void *cb_arg);

/*
 * Attempt to store a key into the cache
 */
struct memcache_req *memcache_store (struct memcache *mc, enum memcache_command cmd, const struct memcache_key *key, const struct memcache_obj *obj, void *cb_arg);

/*
 * Request state
 */ 
enum memcache_req_state memcache_req_state (struct memcache_req *req);

/*
 * Request key
 */
int memcache_req_key (struct memcache_req *req, const struct memcache_key *key);

/*
 * Request data
 */
int memcache_req_obj (struct memcache_req *req, const struct memcache_obj *obj);

#endif /* MEMCACHE_H */