vmemcache - buffer-based LRU cache
#include <libvmemcache.h>
VMEMcache *vmemcache_new();
void vmemcache_delete(VMEMcache *cache);
int vmemcache_set_eviction_policy(VMEMcache *cache,
enum vmemcache_repl_p repl_p);
int vmemcache_set_size(VMEMcache *cache, size_t size);
int vmemcache_set_extent_size(VMEMcache *cache, size_t extent_size);
int vmemcache_add(VMEMcache *cache, const char *path);
void vmemcache_callback_on_evict(VMEMcache *cache,
vmemcache_on_evict *evict, void *arg);
void vmemcache_callback_on_miss(VMEMcache *cache,
vmemcache_on_miss *miss, void *arg);
ssize_t vmemcache_get(VMEMcache *cache,
const void *key, size_t key_size,
void *vbuf, size_t vbufsize, size_t offset, size_t *vsize);
int vmemcache_put(VMEMcache *cache,
const void *key, size_t key_size,
const void *value, size_t value_size);
int vmemcache_evict(VMEMcache *cache, const void *key, size_t ksize);
int vmemcache_get_stat(VMEMcache *cache,
enum vmemcache_statistic stat,
void *value, size_t value_size);
const char *vmemcache_errormsg(void);
libvmemcache is a volatile key-value store optimized for operating on NVDIMM based space, although it can work with any filesystem, stored in memory (tmpfs) or, less performant, on some kind of a disk.
VMEMcache *vmemcache_new();
Creates an empty unconfigured vmemcache instance.
int vmemcache_set_size(VMEMcache *cache, size_t size);
Sets the size of the cache; it will be rounded up towards a whole page size alignment (4KB on x86).
int vmemcache_set_extent_size(VMEMcache *cache, size_t extent_size);
Sets block size of the cache – 256 bytes minimum, strongly recommended to be a multiple of 64 bytes. If the cache is backed by a non byte-addressable medium, the extent size should be 4096 (or a multiple) or performance will greatly suffer.
int vmemcache_set_eviction_policy(VMEMcache *cache, enum vmemcache_repl_p repl_p);
Sets what should happen on a put into a full cache.
int vmemcache_add(VMEMcache *cache, const char *path);
Associate the cache with a backing medium in the given path, which may be:
/dev/dax
devicevoid vmemcache_delete(VMEMcache *cache);
Frees any structures associated with the cache.
ssize_t vmemcache_get(VMEMcache *cache, const void *key, size_t key_size, void *vbuf, size_t vbufsize, size_t offset, size_t *vsize);
Searches for an entry with the given key; it doesn’t have to be zero-terminated or be text - any sequence of bytes of length key_size is okay. If found, the entry’s value is copied to vbuf that has space for vbufsize bytes, optionally skipping offset bytes at the start. No matter if the copy was truncated or not, its true size is stored into vsize; vsize remains unmodified if the key was not found.
Return value is number of bytes successfully copied, or -1 on error. In particular, if there’s no entry for the given key in the cache, the errno will be ENOENT.
int vmemcache_put(VMEMcache *cache, const void *key, size_t key_size, const void *value, size_t value_size);
Inserts the given key:value pair into the cache. Returns 0 on success, -1 on error. Inserting a key that already exists will fail with EEXIST.
int vmemcache_evict(VMEMcache *cache, const void *key, size_t ksize);
Removes the given key from the cache. If key is null and there is a replacement policy set, the oldest entry will be removed. Returns 0 if an entry has been evicted, -1 otherwise.
You can register a hook to be called during eviction or after a cache miss, using vmemcache_callback_on_evict() or vmemcache_callback_on_miss(), respectively. The extra arg will be passed to your function.
void vmemcache_on_evict(VMEMcache *cache, const void *key, size_t key_size, void *arg);
Called when an entry is being removed from the cache. The eviction can’t be prevented, but until the callback returns, the entry remains available for queries. The thread that triggered the eviction is blocked in the meantime.
void vmemcache_on_miss(VMEMcache *cache, const void *key, size_t key_size, void *arg);
Called when a get query fails, to provide an opportunity to insert the missing key. If the callback calls put for that specific key, the get will return its value, even if it did not fit into the cache.
int vmemcache_get_stat(VMEMcache *cache, enum vmemcache_statistic stat, void *value, size_t value_size);
Obtains a piece of statistics about the cache. The stat may be:
Statistics are enabled by default. They can be disabled at the compile time of the vmemcache library if the STATS_ENABLED CMake option is set to OFF.
const char *vmemcache_errormsg(void);
Retrieves a human-friendly description of the last error.
On an error, a machine-usable description is passed in errno
. It may be: