Blame include/apr_memcache.h

Packit 383869
/* Licensed to the Apache Software Foundation (ASF) under one or more
Packit 383869
 * contributor license agreements.  See the NOTICE file distributed with
Packit 383869
 * this work for additional information regarding copyright ownership.
Packit 383869
 * The ASF licenses this file to You under the Apache License, Version 2.0
Packit 383869
 * (the "License"); you may not use this file except in compliance with
Packit 383869
 * the License.  You may obtain a copy of the License at
Packit 383869
 *
Packit 383869
 *     http://www.apache.org/licenses/LICENSE-2.0
Packit 383869
 *
Packit 383869
 * Unless required by applicable law or agreed to in writing, software
Packit 383869
 * distributed under the License is distributed on an "AS IS" BASIS,
Packit 383869
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Packit 383869
 * See the License for the specific language governing permissions and
Packit 383869
 * limitations under the License.
Packit 383869
 */
Packit 383869
Packit 383869
#ifndef APR_MEMCACHE_H
Packit 383869
#define APR_MEMCACHE_H
Packit 383869
Packit 383869
/**
Packit 383869
 * @file apr_memcache.h
Packit 383869
 * @brief Client interface for memcached
Packit 383869
 * @remark To use this interface you must have a separate memcached
Packit 383869
 * server running. See the memcached website at http://www.danga.com/memcached/
Packit 383869
 * for more information.
Packit 383869
 */
Packit 383869
Packit 383869
#include "apr.h"
Packit 383869
#include "apr_pools.h"
Packit 383869
#include "apr_time.h"
Packit 383869
#include "apr_strings.h"
Packit 383869
#include "apr_network_io.h"
Packit 383869
#include "apr_ring.h"
Packit 383869
#include "apr_buckets.h"
Packit 383869
#include "apr_reslist.h"
Packit 383869
#include "apr_hash.h"
Packit 383869
Packit 383869
#ifdef __cplusplus
Packit 383869
extern "C" {
Packit 383869
#endif /* __cplusplus */
Packit 383869
Packit 383869
/**
Packit 383869
 * @defgroup APR_Util_MC Memcached Client Routines
Packit 383869
 * @ingroup APR_Util
Packit 383869
 * @{
Packit 383869
 */
Packit 383869
Packit 383869
/** Specifies the status of a memcached server */
Packit 383869
typedef enum
Packit 383869
{
Packit 383869
    APR_MC_SERVER_LIVE, /**< Server is alive and responding to requests */
Packit 383869
    APR_MC_SERVER_DEAD  /**< Server is not responding to requests */
Packit 383869
} apr_memcache_server_status_t;
Packit 383869
Packit 383869
/** Opaque memcache client connection object */
Packit 383869
typedef struct apr_memcache_conn_t apr_memcache_conn_t;
Packit 383869
Packit 383869
/** Memcache Server Info Object */
Packit 383869
typedef struct apr_memcache_server_t apr_memcache_server_t;
Packit 383869
struct apr_memcache_server_t
Packit 383869
{
Packit 383869
    const char *host; /**< Hostname of this Server */
Packit 383869
    apr_port_t port; /**< Port of this Server */
Packit 383869
    apr_memcache_server_status_t status; /**< @see apr_memcache_server_status_t */
Packit 383869
#if APR_HAS_THREADS || defined(DOXYGEN)
Packit 383869
    apr_reslist_t *conns; /**< Resource list of actual client connections */
Packit 383869
#else
Packit 383869
    apr_memcache_conn_t *conn;
Packit 383869
#endif
Packit 383869
    apr_pool_t *p; /** Pool to use for private allocations */
Packit 383869
#if APR_HAS_THREADS
Packit 383869
    apr_thread_mutex_t *lock;
Packit 383869
#endif
Packit 383869
    apr_time_t btime;
Packit 383869
};
Packit 383869
Packit 383869
/* Custom hash callback function prototype, user for server selection.
Packit 383869
* @param baton user selected baton
Packit 383869
* @param data data to hash
Packit 383869
* @param data_len length of data
Packit 383869
*/
Packit 383869
typedef apr_uint32_t (*apr_memcache_hash_func)(void *baton,
Packit 383869
                                               const char *data,
Packit 383869
                                               const apr_size_t data_len);
Packit 383869
Packit 383869
typedef struct apr_memcache_t apr_memcache_t;
Packit 383869
Packit 383869
/* Custom Server Select callback function prototype.
Packit 383869
* @param baton user selected baton
Packit 383869
* @param mc memcache instance, use mc->live_servers to select a node
Packit 383869
* @param hash hash of the selected key.
Packit 383869
*/
Packit 383869
typedef apr_memcache_server_t* (*apr_memcache_server_func)(void *baton,
Packit 383869
                                                 apr_memcache_t *mc,
Packit 383869
                                                 const apr_uint32_t hash);
Packit 383869
Packit 383869
/** Container for a set of memcached servers */
Packit 383869
struct apr_memcache_t
Packit 383869
{
Packit 383869
    apr_uint32_t flags; /**< Flags, Not currently used */
Packit 383869
    apr_uint16_t nalloc; /**< Number of Servers Allocated */
Packit 383869
    apr_uint16_t ntotal; /**< Number of Servers Added */
Packit 383869
    apr_memcache_server_t **live_servers; /**< Array of Servers */
Packit 383869
    apr_pool_t *p; /** Pool to use for allocations */
Packit 383869
    void *hash_baton;
Packit 383869
    apr_memcache_hash_func hash_func;
Packit 383869
    void *server_baton;
Packit 383869
    apr_memcache_server_func server_func;
Packit 383869
};
Packit 383869
Packit 383869
/** Returned Data from a multiple get */
Packit 383869
typedef struct
Packit 383869
{
Packit 383869
    apr_status_t status;
Packit 383869
    const char* key;
Packit 383869
    apr_size_t len;
Packit 383869
    char *data;
Packit 383869
    apr_uint16_t flags;
Packit 383869
} apr_memcache_value_t;
Packit 383869
Packit 383869
/**
Packit 383869
 * Creates a crc32 hash used to split keys between servers
Packit 383869
 * @param mc The memcache client object to use
Packit 383869
 * @param data Data to be hashed
Packit 383869
 * @param data_len Length of the data to use
Packit 383869
 * @return crc32 hash of data
Packit 383869
 * @remark The crc32 hash is not compatible with old memcached clients.
Packit 383869
 */
Packit 383869
APU_DECLARE(apr_uint32_t) apr_memcache_hash(apr_memcache_t *mc,
Packit 383869
                                            const char *data,
Packit 383869
                                            const apr_size_t data_len);
Packit 383869
Packit 383869
/**
Packit 383869
 * Pure CRC32 Hash. Used by some clients.
Packit 383869
 */
Packit 383869
APU_DECLARE(apr_uint32_t) apr_memcache_hash_crc32(void *baton,
Packit 383869
                                                  const char *data,
Packit 383869
                                                  const apr_size_t data_len);
Packit 383869
Packit 383869
/**
Packit 383869
 * hash compatible with the standard Perl Client.
Packit 383869
 */
Packit 383869
APU_DECLARE(apr_uint32_t) apr_memcache_hash_default(void *baton,
Packit 383869
                                                    const char *data,
Packit 383869
                                                    const apr_size_t data_len);
Packit 383869
Packit 383869
/**
Packit 383869
 * Picks a server based on a hash
Packit 383869
 * @param mc The memcache client object to use
Packit 383869
 * @param hash Hashed value of a Key
Packit 383869
 * @return server that controls specified hash
Packit 383869
 * @see apr_memcache_hash
Packit 383869
 */
Packit 383869
APU_DECLARE(apr_memcache_server_t *) apr_memcache_find_server_hash(apr_memcache_t *mc,
Packit 383869
                                                                   const apr_uint32_t hash);
Packit 383869
Packit 383869
/**
Packit 383869
 * server selection compatible with the standard Perl Client.
Packit 383869
 */
Packit 383869
APU_DECLARE(apr_memcache_server_t *) apr_memcache_find_server_hash_default(void *baton,
Packit 383869
                                                                           apr_memcache_t *mc, 
Packit 383869
                                                                           const apr_uint32_t hash);
Packit 383869
Packit 383869
/**
Packit 383869
 * Adds a server to a client object
Packit 383869
 * @param mc The memcache client object to use
Packit 383869
 * @param server Server to add
Packit 383869
 * @remark Adding servers is not thread safe, and should be done once at startup.
Packit 383869
 * @warning Changing servers after startup may cause keys to go to
Packit 383869
 * different servers.
Packit 383869
 */
Packit 383869
APU_DECLARE(apr_status_t) apr_memcache_add_server(apr_memcache_t *mc,
Packit 383869
                                                  apr_memcache_server_t *server);
Packit 383869
Packit 383869
Packit 383869
/**
Packit 383869
 * Finds a Server object based on a hostname/port pair
Packit 383869
 * @param mc The memcache client object to use
Packit 383869
 * @param host Hostname of the server
Packit 383869
 * @param port Port of the server
Packit 383869
 * @return Server with matching Hostname and Port, or NULL if none was found.
Packit 383869
 */
Packit 383869
APU_DECLARE(apr_memcache_server_t *) apr_memcache_find_server(apr_memcache_t *mc,
Packit 383869
                                                              const char *host,
Packit 383869
                                                              apr_port_t port);
Packit 383869
Packit 383869
/**
Packit 383869
 * Enables a Server for use again
Packit 383869
 * @param mc The memcache client object to use
Packit 383869
 * @param ms Server to Activate
Packit 383869
 */
Packit 383869
APU_DECLARE(apr_status_t) apr_memcache_enable_server(apr_memcache_t *mc,
Packit 383869
                                                     apr_memcache_server_t *ms);
Packit 383869
Packit 383869
Packit 383869
/**
Packit 383869
 * Disable a Server
Packit 383869
 * @param mc The memcache client object to use
Packit 383869
 * @param ms Server to Disable
Packit 383869
 */
Packit 383869
APU_DECLARE(apr_status_t) apr_memcache_disable_server(apr_memcache_t *mc,
Packit 383869
                                                      apr_memcache_server_t *ms);
Packit 383869
Packit 383869
/**
Packit 383869
 * Creates a new Server Object
Packit 383869
 * @param p Pool to use
Packit 383869
 * @param host hostname of the server
Packit 383869
 * @param port port of the server
Packit 383869
 * @param min  minimum number of client sockets to open
Packit 383869
 * @param smax soft maximum number of client connections to open
Packit 383869
 * @param max  hard maximum number of client connections
Packit 383869
 * @param ttl  time to live in microseconds of a client connection
Packit 383869
 * @param ns   location of the new server object
Packit 383869
 * @see apr_reslist_create
Packit 383869
 * @remark min, smax, and max are only used when APR_HAS_THREADS
Packit 383869
 */
Packit 383869
APU_DECLARE(apr_status_t) apr_memcache_server_create(apr_pool_t *p,
Packit 383869
                                                     const char *host,
Packit 383869
                                                     apr_port_t port,
Packit 383869
                                                     apr_uint32_t min,
Packit 383869
                                                     apr_uint32_t smax,
Packit 383869
                                                     apr_uint32_t max,
Packit 383869
                                                     apr_uint32_t ttl,
Packit 383869
                                                     apr_memcache_server_t **ns);
Packit 383869
/**
Packit 383869
 * Creates a new memcached client object
Packit 383869
 * @param p Pool to use
Packit 383869
 * @param max_servers maximum number of servers
Packit 383869
 * @param flags Not currently used
Packit 383869
 * @param mc   location of the new memcache client object
Packit 383869
 */
Packit 383869
APU_DECLARE(apr_status_t) apr_memcache_create(apr_pool_t *p,
Packit 383869
                                              apr_uint16_t max_servers,
Packit 383869
                                              apr_uint32_t flags,
Packit 383869
                                              apr_memcache_t **mc);
Packit 383869
Packit 383869
/**
Packit 383869
 * Gets a value from the server, allocating the value out of p
Packit 383869
 * @param mc client to use
Packit 383869
 * @param p Pool to use
Packit 383869
 * @param key null terminated string containing the key
Packit 383869
 * @param baton location of the allocated value
Packit 383869
 * @param len   length of data at baton
Packit 383869
 * @param flags any flags set by the client for this key
Packit 383869
 * @return 
Packit 383869
 */
Packit 383869
APU_DECLARE(apr_status_t) apr_memcache_getp(apr_memcache_t *mc, 
Packit 383869
                                            apr_pool_t *p,
Packit 383869
                                            const char* key,
Packit 383869
                                            char **baton,
Packit 383869
                                            apr_size_t *len,
Packit 383869
                                            apr_uint16_t *flags);
Packit 383869
Packit 383869
Packit 383869
/**
Packit 383869
 * Add a key to a hash for a multiget query
Packit 383869
 *  if the hash (*value) is NULL it will be created
Packit 383869
 * @param data_pool pool from where the hash and their items are created from
Packit 383869
 * @param key null terminated string containing the key
Packit 383869
 * @param values hash of keys and values that this key will be added to
Packit 383869
 * @return
Packit 383869
 */
Packit 383869
APU_DECLARE(void) apr_memcache_add_multget_key(apr_pool_t *data_pool,
Packit 383869
                                               const char* key,
Packit 383869
                                               apr_hash_t **values);
Packit 383869
Packit 383869
/**
Packit 383869
 * Gets multiple values from the server, allocating the values out of p
Packit 383869
 * @param mc client to use
Packit 383869
 * @param temp_pool Pool used for temporary allocations. May be cleared inside this
Packit 383869
 *        call.
Packit 383869
 * @param data_pool Pool used to allocate data for the returned values.
Packit 383869
 * @param values hash of apr_memcache_value_t keyed by strings, contains the
Packit 383869
 *        result of the multiget call.
Packit 383869
 * @return
Packit 383869
 */
Packit 383869
APU_DECLARE(apr_status_t) apr_memcache_multgetp(apr_memcache_t *mc,
Packit 383869
                                                apr_pool_t *temp_pool,
Packit 383869
                                                apr_pool_t *data_pool,
Packit 383869
                                                apr_hash_t *values);
Packit 383869
Packit 383869
/**
Packit 383869
 * Sets a value by key on the server
Packit 383869
 * @param mc client to use
Packit 383869
 * @param key   null terminated string containing the key
Packit 383869
 * @param baton data to store on the server
Packit 383869
 * @param data_size   length of data at baton
Packit 383869
 * @param timeout time in seconds for the data to live on the server
Packit 383869
 * @param flags any flags set by the client for this key
Packit 383869
 */
Packit 383869
APU_DECLARE(apr_status_t) apr_memcache_set(apr_memcache_t *mc,
Packit 383869
                                           const char *key,
Packit 383869
                                           char *baton,
Packit 383869
                                           const apr_size_t data_size,
Packit 383869
                                           apr_uint32_t timeout,
Packit 383869
                                           apr_uint16_t flags);
Packit 383869
Packit 383869
/**
Packit 383869
 * Adds value by key on the server
Packit 383869
 * @param mc client to use
Packit 383869
 * @param key   null terminated string containing the key
Packit 383869
 * @param baton data to store on the server
Packit 383869
 * @param data_size   length of data at baton
Packit 383869
 * @param timeout time for the data to live on the server
Packit 383869
 * @param flags any flags set by the client for this key
Packit 383869
 * @return APR_SUCCESS if the key was added, APR_EEXIST if the key 
Packit 383869
 * already exists on the server.
Packit 383869
 */
Packit 383869
APU_DECLARE(apr_status_t) apr_memcache_add(apr_memcache_t *mc,
Packit 383869
                                           const char *key,
Packit 383869
                                           char *baton,
Packit 383869
                                           const apr_size_t data_size,
Packit 383869
                                           apr_uint32_t timeout,
Packit 383869
                                           apr_uint16_t flags);
Packit 383869
Packit 383869
/**
Packit 383869
 * Replaces value by key on the server
Packit 383869
 * @param mc client to use
Packit 383869
 * @param key   null terminated string containing the key
Packit 383869
 * @param baton data to store on the server
Packit 383869
 * @param data_size   length of data at baton
Packit 383869
 * @param timeout time for the data to live on the server
Packit 383869
 * @param flags any flags set by the client for this key
Packit 383869
 * @return APR_SUCCESS if the key was added, APR_EEXIST if the key 
Packit 383869
 * did not exist on the server.
Packit 383869
 */
Packit 383869
APU_DECLARE(apr_status_t) apr_memcache_replace(apr_memcache_t *mc,
Packit 383869
                                               const char *key,
Packit 383869
                                               char *baton,
Packit 383869
                                               const apr_size_t data_size,
Packit 383869
                                               apr_uint32_t timeout,
Packit 383869
                                               apr_uint16_t flags);
Packit 383869
/**
Packit 383869
 * Deletes a key from a server
Packit 383869
 * @param mc client to use
Packit 383869
 * @param key   null terminated string containing the key
Packit 383869
 * @param timeout time for the delete to stop other clients from adding
Packit 383869
 */
Packit 383869
APU_DECLARE(apr_status_t) apr_memcache_delete(apr_memcache_t *mc,
Packit 383869
                                              const char *key,
Packit 383869
                                              apr_uint32_t timeout);
Packit 383869
Packit 383869
/**
Packit 383869
 * Increments a value
Packit 383869
 * @param mc client to use
Packit 383869
 * @param key   null terminated string containing the key
Packit 383869
 * @param n     number to increment by
Packit 383869
 * @param nv    new value after incrementing
Packit 383869
 */
Packit 383869
APU_DECLARE(apr_status_t) apr_memcache_incr(apr_memcache_t *mc, 
Packit 383869
                                            const char *key,
Packit 383869
                                            apr_int32_t n,
Packit 383869
                                            apr_uint32_t *nv);
Packit 383869
Packit 383869
/**
Packit 383869
 * Decrements a value
Packit 383869
 * @param mc client to use
Packit 383869
 * @param key   null terminated string containing the key
Packit 383869
 * @param n     number to decrement by
Packit 383869
 * @param new_value    new value after decrementing
Packit 383869
 */
Packit 383869
APU_DECLARE(apr_status_t) apr_memcache_decr(apr_memcache_t *mc, 
Packit 383869
                                            const char *key,
Packit 383869
                                            apr_int32_t n,
Packit 383869
                                            apr_uint32_t *new_value);
Packit 383869
Packit 383869
/**
Packit 383869
 * Query a server's version
Packit 383869
 * @param ms    server to query
Packit 383869
 * @param p     Pool to allocate answer from
Packit 383869
 * @param baton location to store server version string
Packit 383869
 * @param len   length of the server version string
Packit 383869
 */
Packit 383869
APU_DECLARE(apr_status_t) apr_memcache_version(apr_memcache_server_t *ms,
Packit 383869
                                               apr_pool_t *p,
Packit 383869
                                               char **baton);
Packit 383869
Packit 383869
typedef struct
Packit 383869
{
Packit 383869
    /** Version string of this server */
Packit 383869
    const char *version;
Packit 383869
    /** Process id of this server process */
Packit 383869
    apr_uint32_t pid;
Packit 383869
    /** Number of seconds this server has been running */
Packit 383869
    apr_uint32_t uptime;
Packit 383869
    /** current UNIX time according to the server */
Packit 383869
    apr_time_t time;
Packit 383869
    /** The size of a pointer on the current machine */
Packit 383869
    apr_uint32_t pointer_size;
Packit 383869
    /** Accumulated user time for this process */
Packit 383869
    apr_time_t rusage_user;
Packit 383869
    /** Accumulated system time for this process */
Packit 383869
    apr_time_t rusage_system;
Packit 383869
    /** Current number of items stored by the server */
Packit 383869
    apr_uint32_t curr_items;
Packit 383869
    /** Total number of items stored by this server */
Packit 383869
    apr_uint32_t total_items;
Packit 383869
    /** Current number of bytes used by this server to store items */
Packit 383869
    apr_uint64_t bytes;
Packit 383869
    /** Number of open connections */
Packit 383869
    apr_uint32_t curr_connections;
Packit 383869
    /** Total number of connections opened since the server started running */
Packit 383869
    apr_uint32_t total_connections;
Packit 383869
    /** Number of connection structures allocated by the server */
Packit 383869
    apr_uint32_t connection_structures;
Packit 383869
    /** Cumulative number of retrieval requests */
Packit 383869
    apr_uint32_t cmd_get;
Packit 383869
    /** Cumulative number of storage requests */
Packit 383869
    apr_uint32_t cmd_set;
Packit 383869
    /** Number of keys that have been requested and found present */
Packit 383869
    apr_uint32_t get_hits;
Packit 383869
    /** Number of items that have been requested and not found */
Packit 383869
    apr_uint32_t get_misses;
Packit 383869
    /** Number of items removed from cache because they passed their
Packit 383869
        expiration time */
Packit 383869
    apr_uint64_t evictions;
Packit 383869
    /** Total number of bytes read by this server */
Packit 383869
    apr_uint64_t bytes_read;
Packit 383869
    /** Total number of bytes sent by this server */
Packit 383869
    apr_uint64_t bytes_written;
Packit 383869
    /** Number of bytes this server is allowed to use for storage. */
Packit 383869
    apr_uint32_t limit_maxbytes;
Packit 383869
    /** Number of threads the server is running (if built with threading) */
Packit 383869
    apr_uint32_t threads; 
Packit 383869
} apr_memcache_stats_t;
Packit 383869
Packit 383869
/**
Packit 383869
 * Query a server for statistics
Packit 383869
 * @param ms    server to query
Packit 383869
 * @param p     Pool to allocate answer from
Packit 383869
 * @param stats location of the new statistics structure
Packit 383869
 */
Packit 383869
APU_DECLARE(apr_status_t) apr_memcache_stats(apr_memcache_server_t *ms, 
Packit 383869
                                             apr_pool_t *p,
Packit 383869
                                             apr_memcache_stats_t **stats);
Packit 383869
Packit 383869
Packit 383869
/** @} */
Packit 383869
Packit 383869
#ifdef __cplusplus
Packit 383869
}
Packit 383869
#endif
Packit 383869
Packit 383869
#endif /* APR_MEMCACHE_H */