C Collections

clrucache · Section 3

ccache_construct

ccache_construct - declare and initialise an LRU cache in one step

C Collections (libccollections, -lccollections)

#include <ccollections/clrucache.h>
 
ccache_construct(name, KeyT, ValT, capacity, getter, setter, evict_cb);

ccache_declare() and ccache_init() in a single statement. This is the
usual way to create an LRU cache, and it calls ccol__fatal_err() when
the creation fails.

name      Variable name for the cache handle
KeyT      Key type
ValT      Value type
capacity  The total number of live entries; see
          clrucache_create_full(3). It must be greater than 0
getter    Remote getter callback, or NULL
setter    Remote setter callback, or NULL
evict_cb  Eviction callback, or NULL

An array key type or value type, such as int[2] or char[16], is a
compile error, and the message says to wrap the array in a struct. The
macros copy a key or a value by assignment, C cannot assign an array,
and without the error the cache would store the address of the
caller's array instead of its elements. A struct that holds the
array works.

ccache_construct(cache, int, double, 128, NULL, NULL, NULL);
 
int k = 42;
double v = 3.14;
ccache_set(cache, k, v);
 
/* val_ptr is a pointer to the value type (double *), not cmap_pair * */
double out = 0.0;
if (ccache_get(cache, k, &out) == ccol_success) {
    printf("%.2f\n", out);
}
 
ccache_destroy(cache);

ccache_construct_scoped(3), ccache_declare(3), ccache_init(3), ccache_destroy(3)