Lines Matching defs:cache
18 * util_ldap_cache_mgr.c: LDAP cache manager things
79 void util_ald_free(util_ald_cache_t *cache, const void *ptr)
82 if (cache->rmm_addr) {
85 apr_rmm_free(cache->rmm_addr, apr_rmm_offset_get(cache->rmm_addr, (void *)ptr));
98 void *util_ald_alloc(util_ald_cache_t *cache, unsigned long size)
103 if (cache->rmm_addr) {
105 apr_rmm_off_t block = apr_rmm_calloc(cache->rmm_addr, size);
106 return block ? (void *)apr_rmm_addr_get(cache->rmm_addr, block) : NULL;
117 const char *util_ald_strdup(util_ald_cache_t *cache, const char *s)
120 if (cache->rmm_addr) {
122 apr_rmm_off_t block = apr_rmm_calloc(cache->rmm_addr, strlen(s)+1);
123 char *buf = block ? (char *)apr_rmm_addr_get(cache->rmm_addr, block) : NULL;
145 util_compare_subgroup_t *util_ald_sgl_dup(util_ald_cache_t *cache, util_compare_subgroup_t *sgl_in)
154 sgl_out = (util_compare_subgroup_t *) util_ald_alloc(cache, sizeof(util_compare_subgroup_t));
156 sgl_out->subgroupDNs = util_ald_alloc(cache, sizeof(char *) * sgl_in->len);
159 sgl_out->subgroupDNs[i] = util_ald_strdup(cache, sgl_in->subgroupDNs[i]);
163 util_ald_free(cache, sgl_out->subgroupDNs[i]);
165 util_ald_free(cache, sgl_out->subgroupDNs);
166 util_ald_free(cache, sgl_out);
184 void util_ald_sgl_free(util_ald_cache_t *cache, util_compare_subgroup_t **sgl)
192 util_ald_free(cache, (*sgl)->subgroupDNs[i]);
194 util_ald_free(cache, *sgl);
227 Purges a cache that has gotten full. We keep track of the time that we
228 added the entry that made the cache 3/4 full, then delete all entries
232 void util_ald_cache_purge(util_ald_cache_t *cache)
238 if (!cache)
241 cache->last_purge = apr_time_now();
242 cache->npurged = 0;
243 cache->numpurges++;
245 for (i=0; i < cache->size; ++i) {
246 pp = cache->nodes + i;
249 if (p->add_time < cache->marktime) {
251 (*cache->free)(cache, p->payload);
252 util_ald_free(cache, p);
253 cache->numentries--;
254 cache->npurged++;
265 cache->avg_purgetime =
266 ((t - cache->last_purge) + (cache->avg_purgetime * (cache->numpurges-1))) /
267 cache->numpurges;
328 void * (*copyfunc)(util_ald_cache_t *cache, void *),
329 void (*freefunc)(util_ald_cache_t *cache, void *),
330 void (*displayfunc)(request_rec *r, util_ald_cache_t *cache, void *))
332 util_ald_cache_t *cache;
343 cache = (util_ald_cache_t *)calloc(sizeof(util_ald_cache_t), 1);
347 cache = block ? (util_ald_cache_t *)apr_rmm_addr_get(st->cache_rmm, block) : NULL;
350 cache = (util_ald_cache_t *)calloc(sizeof(util_ald_cache_t), 1);
352 if (!cache)
356 cache->rmm_addr = st->cache_rmm;
357 cache->shm_addr = st->cache_shm;
359 cache->maxentries = cache_size;
360 cache->numentries = 0;
361 cache->size = cache_size / 3;
362 if (cache->size < 64)
363 cache->size = 64;
364 for (i = 0; primes[i] && primes[i] < cache->size; ++i)
366 cache->size = primes[i] ? primes[i] : primes[i-1];
368 cache->nodes = (util_cache_node_t **)util_ald_alloc(cache, cache->size * sizeof(util_cache_node_t *));
369 if (!cache->nodes) {
370 /* This frees cache in the right way even if !APR_HAS_SHARED_MEMORY or !st->cache_rmm */
371 util_ald_free(cache, cache);
375 for (i=0; i < cache->size; ++i)
376 cache->nodes[i] = NULL;
378 cache->hash = hashfunc;
379 cache->compare = comparefunc;
380 cache->copy = copyfunc;
381 cache->free = freefunc;
382 cache->display = displayfunc;
384 cache->fullmark = cache->maxentries / 4 * 3;
385 cache->marktime = 0;
386 cache->avg_purgetime = 0.0;
387 cache->numpurges = 0;
388 cache->last_purge = 0;
389 cache->npurged = 0;
391 cache->fetches = 0;
392 cache->hits = 0;
393 cache->inserts = 0;
394 cache->removes = 0;
396 return cache;
399 void util_ald_destroy_cache(util_ald_cache_t *cache)
404 if (cache == NULL)
407 for (i = 0; i < cache->size; ++i) {
408 p = cache->nodes[i];
412 (*cache->free)(cache, p->payload);
413 util_ald_free(cache, p);
417 util_ald_free(cache, cache->nodes);
418 util_ald_free(cache, cache);
421 void *util_ald_cache_fetch(util_ald_cache_t *cache, void *payload)
426 if (cache == NULL)
429 cache->fetches++;
431 hashval = (*cache->hash)(payload) % cache->size;
433 for (p = cache->nodes[hashval];
434 p && !(*cache->compare)(p->payload, payload);
438 cache->hits++;
447 * Insert an item into the cache.
450 void *util_ald_cache_insert(util_ald_cache_t *cache, void *payload)
457 if (cache == NULL || payload == NULL) {
462 if (cache->numentries >= cache->maxentries) {
463 util_ald_cache_purge(cache);
464 if (cache->numentries >= cache->maxentries) {
467 "Purge of LDAP cache failed");
472 node = (util_cache_node_t *)util_ald_alloc(cache,
476 * XXX: The cache management should be rewritten to work
482 if (cache->numentries < cache->fullmark) {
488 cache->marktime = apr_time_now();
490 util_ald_cache_purge(cache);
491 node = (util_cache_node_t *)util_ald_alloc(cache,
495 "Could not allocate memory for LDAP cache entry");
501 tmp_payload = (*cache->copy)(cache, payload);
504 * XXX: The cache management should be rewritten to work
510 if (cache->numentries < cache->fullmark) {
516 cache->marktime = apr_time_now();
518 util_ald_cache_purge(cache);
519 tmp_payload = (*cache->copy)(cache, payload);
522 "Could not allocate memory for LDAP cache value");
523 util_ald_free(cache, node);
530 cache->inserts++;
531 hashval = (*cache->hash)(payload) % cache->size;
534 node->next = cache->nodes[hashval];
535 cache->nodes[hashval] = node;
540 if (++cache->numentries == cache->fullmark) {
541 cache->marktime=apr_time_now();
547 void util_ald_cache_remove(util_ald_cache_t *cache, void *payload)
552 if (cache == NULL)
555 cache->removes++;
556 hashval = (*cache->hash)(payload) % cache->size;
557 for (p = cache->nodes[hashval], q=NULL;
558 p && !(*cache->compare)(p->payload, payload);
569 cache->nodes[hashval] = p->next;
575 (*cache->free)(cache, p->payload);
576 util_ald_free(cache, p);
577 cache->numentries--;
580 char *util_ald_cache_display_stats(request_rec *r, util_ald_cache_t *cache, char *name, char *id)
590 if (cache == NULL) {
594 for (i=0; i < cache->size; ++i) {
595 if (cache->nodes[i] != NULL) {
597 for (n = cache->nodes[i];
626 cache->numentries,
627 (double)cache->numentries / (double)cache->maxentries * 100.0,
629 cache->hits,
630 cache->fetches,
631 (cache->fetches > 0 ? (double)(cache->hits) / (double)(cache->fetches) * 100.0 : 100.0),
632 cache->inserts,
633 cache->removes);
635 if (cache->numpurges) {
638 apr_ctime(str_ctime, cache->last_purge);
644 cache->numpurges,
653 buf = apr_psprintf(p, "%s<td align='right'>%.2gms</td>\n</tr>", buf, cache->avg_purgetime);
663 char *argfmt = "cache=%s&id=%d&off=%d";
664 char *scanfmt = "cache=%4s&id=%u&off=%u%1s";