3 * ====================================================================
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
20 * ====================================================================
24 * @brief In-memory cache implementation.
31 #include <apr_pools.h>
34 #include "svn_types.h"
35 #include "svn_error.h"
37 #include "svn_config.h"
38 #include "svn_string.h"
42 #endif /* __cplusplus */
47 * @defgroup svn_cache__support In-memory caching
52 * A function type for deserializing an object @a *out from the string
53 * @a data of length @a data_len into @a result_pool. It is legal and
54 * generally suggested that the deserialization will be done in-place,
55 * i.e. modify @a data directly and return it in @a *out.
57 typedef svn_error_t *(*svn_cache__deserialize_func_t)(void **out,
60 apr_pool_t *result_pool);
63 * A function type for deserializing an object @a *out from the string
64 * @a data of length @a data_len into @a result_pool. The extra information
65 * @a baton passed into can be used to deserialize only a specific part or
66 * sub-structure or to perform any other non-modifying operation that may
67 * not require the whole structure to be processed.
69 typedef svn_error_t *(*svn_cache__partial_getter_func_t)(void **out,
73 apr_pool_t *result_pool);
76 * A function type for modifying an already deserialized in the @a *data
77 * buffer of length @a *data_len. Additional information of the modification
78 * to do will be provided in @a baton. The function may change the size of
79 * data buffer and may re-allocate it if necessary. In that case, the new
80 * values must be passed back in @a *data_len and @a *data, respectively.
81 * Allocations will be done from @a result_pool.
83 typedef svn_error_t *(*svn_cache__partial_setter_func_t)(void **data,
86 apr_pool_t *result_pool);
89 * A function type for serializing an object @a in into bytes. The
90 * function should allocate the serialized value in @a result_pool, set
91 * @a *data to the serialized value, and set @a *data_len to its length.
93 typedef svn_error_t *(*svn_cache__serialize_func_t)(void **data,
96 apr_pool_t *result_pool);
99 * A function type for transforming or ignoring errors. @a scratch_pool may
100 * be used for temporary allocations.
102 typedef svn_error_t *(*svn_cache__error_handler_t)(svn_error_t *err,
104 apr_pool_t *scratch_pool);
107 * A wrapper around apr_memcache_t, provided essentially so that the
108 * Subversion public API doesn't depend on whether or not you have
109 * access to the APR memcache libraries.
111 typedef struct svn_memcache_t svn_memcache_t;
114 * An opaque structure representing a membuffer cache object.
116 typedef struct svn_membuffer_t svn_membuffer_t;
119 * Opaque type for an in-memory cache.
121 typedef struct svn_cache__t svn_cache__t;
124 * A structure containing typical statistics about a given cache instance.
125 * Use svn_cache__get_info() to get this data. Note that not all types
126 * of caches will be able to report complete and correct information.
128 typedef struct svn_cache__info_t
130 /** A string identifying the cache instance. Usually a copy of the @a id
131 * or @a prefix parameter passed to the cache constructor.
135 /** Number of getter calls (svn_cache__get() or svn_cache__get()).
139 /** Number of getter calls that return data.
143 /** Number of setter calls (svn_cache__set()).
147 /** Number of function calls that returned an error.
149 apr_uint64_t failures;
151 /** Size of the data currently stored in the cache.
152 * May be 0 if that information is not available.
154 apr_uint64_t used_size;
156 /** Amount of memory currently reserved for cached data.
157 * Will be equal to @a used_size if no precise information is available.
159 apr_uint64_t data_size;
161 /** Lower threshold of the total size of memory allocated to the cache and
162 * its index as well as management structures. The actual memory allocated
163 * by the cache may be larger.
165 apr_uint64_t total_size;
167 /** Number of cache entries.
168 * May be 0 if that information is not available.
170 apr_uint64_t used_entries;
172 /** Maximum numbers of cache entries.
173 * May be 0 if that information is not available.
175 apr_uint64_t total_entries;
179 * Creates a new cache in @a *cache_p. This cache will use @a pool
180 * for all of its storage needs. The elements in the cache will be
181 * indexed by keys of length @a klen, which may be APR_HASH_KEY_STRING
182 * if they are strings. Cached values will be copied in and out of
183 * the cache using @a serialize_func and @a deserialize_func, respectively.
185 * The cache stores up to @a pages * @a items_per_page items at a
186 * time. The exact cache invalidation strategy is not defined here,
187 * but in general, a lower value for @a items_per_page means more
188 * memory overhead for the same number of items, but a higher value
189 * for @a items_per_page means more items are cleared at once. Both
190 * @a pages and @a items_per_page must be positive (though they both
191 * may certainly be 1).
193 * If @a thread_safe is true, and APR is compiled with threads, all
194 * accesses to the cache will be protected with a mutex. The @a id
195 * is a purely user-visible information that will allow coders to
196 * identify this cache instance in a #svn_cache__info_t struct.
197 * It does not influence the behavior of the cache itself.
199 * Note that NULL is a legitimate value for cache entries (and
200 * @a serialize_func will not be called on it).
202 * It is not safe for @a serialize_func nor @a deserialize_func to
203 * interact with the cache itself.
206 svn_cache__create_inprocess(svn_cache__t **cache_p,
207 svn_cache__serialize_func_t serialize_func,
208 svn_cache__deserialize_func_t deserialize_func,
211 apr_int64_t items_per_page,
212 svn_boolean_t thread_safe,
217 * Creates a new cache in @a *cache_p, communicating to a memcached
218 * process via @a memcache. The elements in the cache will be indexed
219 * by keys of length @a klen, which may be APR_HASH_KEY_STRING if they
220 * are strings. Values will be serialized for memcached using @a
221 * serialize_func and deserialized using @a deserialize_func. Because
222 * the same memcached server may cache many different kinds of values,
223 * @a prefix should be specified to differentiate this cache from
224 * other caches. @a *cache_p will be allocated in @a result_pool.
226 * If @a deserialize_func is NULL, then the data is returned as an
227 * svn_string_t; if @a serialize_func is NULL, then the data is
228 * assumed to be an svn_stringbuf_t.
230 * These caches are always thread safe.
232 * These caches do not support svn_cache__iter.
234 * If Subversion was not built with apr_memcache support, always
235 * raises SVN_ERR_NO_APR_MEMCACHE.
238 svn_cache__create_memcache(svn_cache__t **cache_p,
239 svn_memcache_t *memcache,
240 svn_cache__serialize_func_t serialize_func,
241 svn_cache__deserialize_func_t deserialize_func,
244 apr_pool_t *result_pool);
247 * Given @a config, returns an APR memcached interface in @a
248 * *memcache_p allocated in @a result_pool if @a config contains entries in
249 * the SVN_CACHE_CONFIG_CATEGORY_MEMCACHED_SERVERS section describing
250 * memcached servers; otherwise, sets @a *memcache_p to NULL.
252 * If Subversion was not built with apr_memcache_support, then raises
253 * SVN_ERR_NO_APR_MEMCACHE if and only if @a config is configured to
257 svn_cache__make_memcache_from_config(svn_memcache_t **memcache_p,
258 svn_config_t *config,
259 apr_pool_t *result_pool);
262 * Creates a new membuffer cache object in @a *cache. It will contain
263 * up to @a total_size bytes of data, using @a directory_size bytes
264 * for index information and the remainder for serialized objects.
266 * Since each index entry is about 50 bytes long, 1 to 10 percent of
267 * the @a total_size should be allocated to the @a directory_size,
268 * depending on the average serialized object size. Higher percentages
269 * will generally result in higher hit rates and reduced conflict
270 * resolution overhead.
272 * The cache will be split into @a segment_count segments of equal size.
273 * A higher number reduces lock contention but also limits the maximum
274 * cachable item size. If it is not a power of two, it will be rounded
275 * down to next lower power of two. Also, there is an implementation
276 * specific upper limit and the setting will be capped there automatically.
277 * If the number is 0, a default will be derived from @a total_size.
279 * If access to the resulting cache object is guaranteed to be serialized,
280 * @a thread_safe may be set to @c FALSE for maximum performance.
282 * There is no limit on the number of threads reading a given cache segment
283 * concurrently. Writes, however, need an exclusive lock on the respective
284 * segment. @a allow_blocking_writes controls contention is handled here.
285 * If set to TRUE, writes will wait until the lock becomes available, i.e.
286 * reads should be short. If set to FALSE, write attempts will be ignored
287 * (no data being written to the cache) if some reader or another writer
288 * currently holds the segment lock.
290 * Allocations will be made in @a result_pool, in particular the data buffers.
293 svn_cache__membuffer_cache_create(svn_membuffer_t **cache,
294 apr_size_t total_size,
295 apr_size_t directory_size,
296 apr_size_t segment_count,
297 svn_boolean_t thread_safe,
298 svn_boolean_t allow_blocking_writes,
299 apr_pool_t *result_pool);
302 * Creates a new cache in @a *cache_p, storing the data in a potentially
303 * shared @a membuffer object. The elements in the cache will be indexed
304 * by keys of length @a klen, which may be APR_HASH_KEY_STRING if they
305 * are strings. Values will be serialized for the memcache using @a
306 * serialize_func and deserialized using @a deserialize_func. Because
307 * the same memcache object may cache many different kinds of values
308 * form multiple caches, @a prefix should be specified to differentiate
309 * this cache from other caches. @a *cache_p will be allocated in @a result_pool.
311 * If @a deserialize_func is NULL, then the data is returned as an
312 * svn_string_t; if @a serialize_func is NULL, then the data is
313 * assumed to be an svn_stringbuf_t.
315 * If @a thread_safe is true, and APR is compiled with threads, all
316 * accesses to the cache will be protected with a mutex, if the shared
317 * @a memcache has also been created with thread_safe flag set.
319 * These caches do not support svn_cache__iter.
322 svn_cache__create_membuffer_cache(svn_cache__t **cache_p,
323 svn_membuffer_t *membuffer,
324 svn_cache__serialize_func_t serialize,
325 svn_cache__deserialize_func_t deserialize,
328 svn_boolean_t thread_safe,
329 apr_pool_t *result_pool);
332 * Sets @a handler to be @a cache's error handling routine. If any
333 * error is returned from a call to svn_cache__get or svn_cache__set, @a
334 * handler will be called with @a baton and the error, and the
335 * original function will return whatever error @a handler returns
336 * instead (possibly SVN_NO_ERROR); @a handler will receive the pool
337 * passed to the svn_cache_* function. @a scratch_pool is used for temporary
341 svn_cache__set_error_handler(svn_cache__t *cache,
342 svn_cache__error_handler_t handler,
344 apr_pool_t *scratch_pool);
347 * Returns @c TRUE if the @a cache supports objects of the given @a size.
348 * There is no guarantee, that svn_cache__set() will actually store the
349 * respective object in that case. However, a @c FALSE return value indicates
350 * that an attempt to cache the item will either fail or impair the overall
351 * cache performance. @c FALSE will also be returned if @a cache is @c NULL.
354 svn_cache__is_cachable(svn_cache__t *cache,
357 #define SVN_CACHE_CONFIG_CATEGORY_MEMCACHED_SERVERS "memcached-servers"
360 * Fetches a value indexed by @a key from @a cache into @a *value,
361 * setting @a *found to TRUE iff it is in the cache and FALSE if it is
362 * not found. @a key may be NULL in which case @a *found will be
363 * FALSE. The value is copied into @a result_pool using the deserialize
364 * function provided to the cache's constructor.
367 svn_cache__get(void **value,
368 svn_boolean_t *found,
371 apr_pool_t *result_pool);
374 * Stores the value @a value under the key @a key in @a cache. Uses @a
375 * scratch_pool for temporary allocations. The cache makes copies of
376 * @a key and @a value if necessary (that is, @a key and @a value may
377 * have shorter lifetimes than the cache). @a key may be NULL in which
378 * case the cache will remain unchanged.
380 * If there is already a value for @a key, this will replace it. Bear
381 * in mind that in some circumstances this may leak memory (that is,
382 * the cache's copy of the previous value may not be immediately
383 * cleared); it is only guaranteed to not leak for caches created with
384 * @a items_per_page equal to 1.
387 svn_cache__set(svn_cache__t *cache,
390 apr_pool_t *scratch_pool);
393 * Iterates over the elements currently in @a cache, calling @a func
394 * for each one until there are no more elements or @a func returns an
395 * error. Uses @a scratch_pool for temporary allocations.
397 * If @a completed is not NULL, then on return - if @a func returns no
398 * errors - @a *completed will be set to @c TRUE.
400 * If @a func returns an error other than @c SVN_ERR_ITER_BREAK, that
401 * error is returned. When @a func returns @c SVN_ERR_ITER_BREAK,
402 * iteration is interrupted, but no error is returned and @a
403 * *completed is set to @c FALSE. (The error handler set by
404 * svn_cache__set_error_handler is not used for svn_cache__iter.)
406 * It is not legal to perform any other cache operations on @a cache
409 * svn_cache__iter is not supported by all cache implementations; see
410 * the svn_cache__create_* function for details.
413 svn_cache__iter(svn_boolean_t *completed,
415 svn_iter_apr_hash_cb_t func,
417 apr_pool_t *scratch_pool);
420 * Similar to svn_cache__get() but will call a specific de-serialization
421 * function @a func. @a found will be set depending on whether the @a key
422 * has been found. Even if that reports @c TRUE, @a value may still return
423 * a @c NULL pointer depending on the logic inside @a func. For a @a NULL
424 * @a key, no data will be found. @a value will be allocated in
428 svn_cache__get_partial(void **value,
429 svn_boolean_t *found,
432 svn_cache__partial_getter_func_t func,
434 apr_pool_t *result_pool);
437 * Find the item identified by @a key in the @a cache. If it has been found,
438 * call @a func for it and @a baton to potentially modify the data. Changed
439 * data will be written back to the cache. If the item cannot be found,
440 * or if @a key is NULL, @a func does not get called. @a scratch_pool is
441 * used for temporary allocations.
444 svn_cache__set_partial(svn_cache__t *cache,
446 svn_cache__partial_setter_func_t func,
448 apr_pool_t *scratch_pool);
451 * Collect all available usage statistics on the cache instance @a cache
452 * and write the data into @a info. If @a reset has been set, access
453 * counters will be reset right after copying the statistics info.
454 * @a result_pool will be used for allocations.
457 svn_cache__get_info(svn_cache__t *cache,
458 svn_cache__info_t *info,
460 apr_pool_t *result_pool);
463 * Return the information given in @a info formatted as a multi-line string.
464 * Allocations take place in @a result_pool.
467 svn_cache__format_info(const svn_cache__info_t *info,
468 apr_pool_t *result_pool);
470 /* Access the process-global (singleton) membuffer cache. The first call
471 * will automatically allocate the cache using the current cache config.
472 * NULL will be returned if the desired cache size is 0.
476 struct svn_membuffer_t *
477 svn_cache__get_global_membuffer_cache(void);
484 #endif /* __cplusplus */
486 #endif /* SVN_CACHE_H */