1 /* 2 * Copyright (C) 2004-2008 Christos Tsantilas 3 * 4 * This program is free software; you can redistribute it and/or 5 * modify it under the terms of the GNU Lesser General Public 6 * License as published by the Free Software Foundation; either 7 * version 2.1 of the License, or (at your option) any later version. 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 * Lesser General Public License for more details. 13 * 14 * You should have received a copy of the GNU Lesser General Public 15 * License along with this library; if not, write to the Free Software 16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 17 * MA 02110-1301 USA. 18 */ 19 20 #ifndef __CACHE_H 21 #define __CACHE_H 22 #include "hash.h" 23 24 #ifdef __cplusplus 25 extern "C" 26 { 27 #endif 28 29 /** 30 \defgroup CACHE cache api 31 \ingroup API 32 \brief Macros, functions and structures used to implement and use c-icap cache. 33 */ 34 35 struct ci_cache; 36 37 /** 38 \ingroup CACHE 39 \brief A struct which implements a cache type. 40 * Modules implement a cache type, needs to implement members of this structure. 41 */ 42 typedef struct ci_cache_type { 43 int (*init)(struct ci_cache *cache, const char *name); 44 const void *(*search)(struct ci_cache *cache, const void *key, void **val, void *data, void *(*dup_from_cache)(const void *stored_val, size_t stored_val_size, void *data)); 45 int (*update)(struct ci_cache *cache, const void *key, const void *val, size_t val_size, void *(*copy_to_cache)(void *cache_buf, const void *val, size_t cache_buf_size)); 46 void (*destroy)(struct ci_cache *cache); 47 const char *name; 48 } ci_cache_type_t; 49 50 /** 51 * Register a cache type to c-icap server. 52 \ingroup CACHE 53 */ 54 CI_DECLARE_FUNC(void) ci_cache_type_register(const struct ci_cache_type *type); 55 56 /** 57 * The ci_cache_t struct 58 \ingroup CACHE 59 */ 60 typedef struct ci_cache { 61 int (*init)(struct ci_cache *cache, const char *name); 62 63 // If dup_from_cache is NULL return a ci_buffer object 64 const void * (*search)(struct ci_cache *cache, const void *key, void **val, void *data, void *(*dup_from_cache)(const void *stored_val, size_t stored_val_size, void *data)); 65 66 // buf is of size val_size and buf_size == val_size 67 int (*update)(struct ci_cache *cache, const void *key, const void *val, size_t val_size, void *(*copy_to_cache)(void *buf, const void *val, size_t buf_size)); 68 void (*destroy)(struct ci_cache *cache); 69 70 time_t ttl; 71 unsigned int mem_size; 72 unsigned int max_object_size; 73 unsigned int flags; 74 const ci_type_ops_t *key_ops; 75 const ci_cache_type_t *_cache_type; 76 void *cache_data; 77 } ci_cache_t; 78 79 /** 80 * Builds a cache and return a pointer to the ci_cache_t object 81 \ingroup CACHE 82 \param cache_type The cache type to use. If the cache type not found return 83 * a cache object of type "local" 84 \param cache_size The size of the cache 85 \param max_object_size The maximum object size to store in cache 86 \param ttl The ttl value for cached items in this cache 87 \param key_ops If not null, the ci_types_ops_t object to use for comparing 88 * keys. By default keys are considered as c strings. 89 */ 90 CI_DECLARE_FUNC(ci_cache_t *) ci_cache_build( const char *name, 91 const char *cache_type, 92 unsigned int cache_size, 93 unsigned int max_object_size, 94 int ttl, 95 const ci_type_ops_t *key_ops 96 ); 97 98 /** 99 * Searchs a cache for a stored object 100 * If the dup_from_cache parameter is NULL, the returned value must be 101 * released using the ci_buffer_free function. 102 \ingroup CACHE 103 \param cache Pointer to the ci_cache_t object 104 \param key Pointer to the key to search for 105 \param val Pointer to store the pointer of returned value 106 \param data Pointer to void object which will be passed to dup_from_cache 107 * function 108 \param dup_from_cache Pointer to function which will be used to allocate 109 * memory and copy the stored value. 110 */ 111 CI_DECLARE_FUNC(const void *) ci_cache_search(ci_cache_t *cache, const void *key, void **val, void *data, void *(*dup_from_cache)(const void *stored_val, size_t stored_val_size, void *data)); 112 113 /** 114 * Stores an object to cache 115 \ingroup CACHE 116 \param cache Pointer to the ci_cache_t object 117 \param key The key of the stored object 118 \param val Pointer to the object to be stored 119 \param val_size The size of the object to be stored 120 \param copy_to_cache The function to use to copy object to cache. 121 * If it is NULL the memcpy is used. 122 */ 123 CI_DECLARE_FUNC(int) ci_cache_update(ci_cache_t *cache, const void *key, const void *val, size_t val_size, void *(*copy_to_cache)(void *buf, const void *val, size_t buf_size)); 124 125 /** 126 * Destroy a cache_t object 127 \ingroup CACHE 128 */ 129 CI_DECLARE_FUNC(void) ci_cache_destroy(ci_cache_t *cache); 130 131 132 /* 133 Only for internal use only: 134 cb functions to store/retrieve vectors from cache.... 135 */ 136 CI_DECLARE_FUNC(size_t) ci_cache_store_vector_size(ci_vector_t *v); 137 CI_DECLARE_FUNC(void *) ci_cache_store_vector_val(void *buf, const void *val, size_t buf_size); 138 CI_DECLARE_FUNC(void *) ci_cache_read_vector_val(const void *val, size_t val_size, void *); 139 140 141 #ifdef __cplusplus 142 } 143 #endif 144 145 #endif 146