2019-05-03 11:20:47 -04:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
struct cache;
|
|
|
|
|
2019-05-05 19:28:22 -04:00
|
|
|
typedef void *(*cache_getter_t)(void *user_data, const char *key, int *err);
|
2019-05-03 11:20:47 -04:00
|
|
|
typedef void (*cache_free_t)(void *user_data, void *data);
|
2020-12-26 02:25:34 -05:00
|
|
|
|
|
|
|
/// Create a cache with `getter`, and a free function `f` which is used to free the cache
|
|
|
|
/// value when they are invalidated.
|
|
|
|
///
|
|
|
|
/// `user_data` will be passed to `getter` and `f` when they are called.
|
2019-05-03 11:20:47 -04:00
|
|
|
struct cache *new_cache(void *user_data, cache_getter_t getter, cache_free_t f);
|
|
|
|
|
2020-12-26 02:25:34 -05:00
|
|
|
/// Fetch a value from the cache. If the value doesn't present in the cache yet, the
|
|
|
|
/// getter will be called, and the returned value will be stored into the cache.
|
2019-05-05 19:28:22 -04:00
|
|
|
void *cache_get(struct cache *, const char *key, int *err);
|
2020-12-26 02:25:34 -05:00
|
|
|
|
|
|
|
/// Invalidate a value in the cache.
|
2019-05-03 11:20:47 -04:00
|
|
|
void cache_invalidate(struct cache *, const char *key);
|
2020-12-26 02:25:34 -05:00
|
|
|
|
|
|
|
/// Invalidate all values in the cache.
|
2019-05-03 11:20:47 -04:00
|
|
|
void cache_invalidate_all(struct cache *);
|
|
|
|
|
2020-12-26 02:25:34 -05:00
|
|
|
/// Invalidate all values in the cache and free it. Returns the user data passed to
|
|
|
|
/// `new_cache`
|
2019-05-03 11:20:47 -04:00
|
|
|
void *cache_free(struct cache *);
|
2020-12-26 02:25:34 -05:00
|
|
|
|
|
|
|
/// Insert a key-value pair into the cache. Only used for internal testing. Takes
|
|
|
|
/// ownership of `data`
|
|
|
|
///
|
|
|
|
/// If `key` already exists in the cache, this function will abort the program.
|
|
|
|
void cache_set(struct cache *c, const char *key, void *data);
|