2 * Copyright (c) 2005 Michael Bushkov <bushman@rsu.ru>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #ifndef __NSCD_CACHELIB_H__
30 #define __NSCD_CACHELIB_H__
32 #include "hashtable.h"
33 #include "cacheplcs.h"
36 CET_COMMON = 0, /* cache item is atomic */
37 CET_MULTIPART /* cache item is formed part by part */
40 enum cache_transformation_t {
41 CTT_FLUSH = 0, /* flush the cache - delete all obsolete items */
42 CTT_CLEAR = 1 /* delete all items in the cache */
45 /* cache deletion policy type enum */
47 CPT_FIFO = 0, /* first-in first-out */
48 CPT_LRU = 1, /* least recently used */
49 CPT_LFU = 2 /* least frequently used */
52 /* multipart sessions can be used for reading and writing */
53 enum cache_mp_session_t {
59 * When doing partial transformations of entries (which are applied for
60 * elements with keys, that contain specified buffer in its left or
61 * right part), this enum will show the needed position of the key part.
63 enum part_position_t {
68 /* num_levels attribute is obsolete, i think - user can always emulate it
70 * get_time_func is needed to have the clocks-independent counter
73 void (*get_time_func)(struct timeval *);
77 * base structure - normal_cache_entry_params and multipart_cache_entry_params
78 * are "inherited" from it
80 struct cache_entry_params {
81 enum cache_entry_t entry_type;
85 /* params, used for most entries */
86 struct common_cache_entry_params {
87 struct cache_entry_params cep;
89 size_t cache_entries_size;
91 size_t max_elemsize; /* if 0 then no check is made */
92 size_t satisf_elemsize; /* if entry size is exceeded,
93 * this number of elements will be left,
94 * others will be deleted */
95 struct timeval max_lifetime; /* if 0 then no check is made */
96 enum cache_policy_t policy; /* policy used for transformations */
99 /* params, used for multipart entries */
100 struct mp_cache_entry_params {
101 struct cache_entry_params cep;
104 size_t max_elemsize; /* if 0 then no check is made */
105 size_t max_sessions; /* maximum number of active sessions */
107 struct timeval max_lifetime; /* maximum elements lifetime */
110 struct cache_ht_item_data_ {
111 /* key is the bytes sequence only - not the null-terminated string */
118 struct cache_policy_item_ *fifo_policy_item;
121 struct cache_ht_item_ {
122 HASHTABLE_ENTRY_HEAD(ht_item_, struct cache_ht_item_data_) data;
125 struct cache_entry_ {
127 struct cache_entry_params *params;
130 struct cache_common_entry_ {
132 struct cache_entry_params *params;
134 struct common_cache_entry_params common_params;
136 HASHTABLE_HEAD(cache_ht_, cache_ht_item_) items;
140 * Entry always has the FIFO policy, that is used to eliminate old
141 * elements (the ones, with lifetime more than max_lifetime). Besides,
142 * user can specify another policy to be applied, when there are too
143 * many elements in the entry. So policies_size can be 1 or 2.
145 struct cache_policy_ **policies;
146 size_t policies_size;
148 void (*get_time_func)(struct timeval *);
151 struct cache_mp_data_item_ {
155 TAILQ_ENTRY(cache_mp_data_item_) entries;
158 struct cache_mp_write_session_ {
159 struct cache_mp_entry_ *parent_entry;
162 * All items are accumulated in this queue. When the session is
163 * committed, they all will be copied to the multipart entry.
165 TAILQ_HEAD(cache_mp_data_item_head, cache_mp_data_item_) items;
168 TAILQ_ENTRY(cache_mp_write_session_) entries;
171 struct cache_mp_read_session_ {
172 struct cache_mp_entry_ *parent_entry;
173 struct cache_mp_data_item_ *current_item;
175 TAILQ_ENTRY(cache_mp_read_session_) entries;
178 struct cache_mp_entry_ {
180 struct cache_entry_params *params;
182 struct mp_cache_entry_params mp_params;
184 /* All opened write sessions */
185 TAILQ_HEAD(write_sessions_head, cache_mp_write_session_) ws_head;
188 /* All opened read sessions */
189 TAILQ_HEAD(read_sessions_head, cache_mp_read_session_) rs_head;
193 * completed_write_session is the committed write sessions. All read
194 * sessions use data from it. If the completed_write_session is out of
195 * date, but still in use by some of the read sessions, the newly
196 * committed write session is stored in the pending_write_session.
197 * In such a case, completed_write_session will be substituted with
198 * pending_write_session as soon as it won't be used by any of
201 struct cache_mp_write_session_ *completed_write_session;
202 struct cache_mp_write_session_ *pending_write_session;
203 struct timeval creation_time;
204 struct timeval last_request_time;
206 void (*get_time_func)(struct timeval *);
210 struct cache_params params;
212 struct cache_entry_ **entries;
213 size_t entries_capacity;
217 /* simple abstractions - for not to write "struct" every time */
218 typedef struct cache_ *cache;
219 typedef struct cache_entry_ *cache_entry;
220 typedef struct cache_mp_write_session_ *cache_mp_write_session;
221 typedef struct cache_mp_read_session_ *cache_mp_read_session;
223 #define INVALID_CACHE (NULL)
224 #define INVALID_CACHE_ENTRY (NULL)
225 #define INVALID_CACHE_MP_WRITE_SESSION (NULL)
226 #define INVALID_CACHE_MP_READ_SESSION (NULL)
229 * NOTE: all cache operations are thread-unsafe. You must ensure thread-safety
230 * externally, by yourself.
233 /* cache initialization/destruction routines */
234 cache init_cache(struct cache_params const *);
235 void destroy_cache(cache);
237 /* cache entries manipulation routines */
238 int register_cache_entry(cache, struct cache_entry_params const *);
239 int unregister_cache_entry(cache, const char *);
240 cache_entry find_cache_entry(cache, const char *);
242 /* read/write operations used on common entries */
243 int cache_read(cache_entry, const char *, size_t, char *, size_t *);
244 int cache_write(cache_entry, const char *, size_t, char const *, size_t);
246 /* read/write operations used on multipart entries */
247 cache_mp_write_session open_cache_mp_write_session(cache_entry);
248 int cache_mp_write(cache_mp_write_session, char *, size_t);
249 void abandon_cache_mp_write_session(cache_mp_write_session);
250 void close_cache_mp_write_session(cache_mp_write_session);
252 cache_mp_read_session open_cache_mp_read_session(cache_entry);
253 int cache_mp_read(cache_mp_read_session, char *, size_t *);
254 void close_cache_mp_read_session(cache_mp_read_session);
256 /* transformation routines */
257 int transform_cache_entry(cache_entry, enum cache_transformation_t);
258 int transform_cache_entry_part(cache_entry, enum cache_transformation_t,
259 const char *, size_t, enum part_position_t);