Loading...
Searching...
No Matches
lru.h
Go to the documentation of this file.
//
// Created by claunia on 2/10/22.
//
#ifndef LIBAARUFORMAT_LRU_H
#define LIBAARUFORMAT_LRU_H
#include <stdint.h>
#include <uthash.h>
struct CacheEntry
{
char *key;
void *value;
UT_hash_handle hh;
};
struct CacheHeader
{
uint64_t max_items;
struct CacheEntry *cache;
void (*free_func)(void *);
};
void *find_in_cache(struct CacheHeader *cache, const char *key);
void add_to_cache(struct CacheHeader *cache, const char *key, void *value);
void *find_in_cache_uint64(struct CacheHeader *cache, uint64_t key);
void add_to_cache_uint64(struct CacheHeader *cache, uint64_t key, void *value);
void free_cache(struct CacheHeader *cache);
#endif // LIBAARUFORMAT_LRU_H
add_to_cache_uint64
void add_to_cache_uint64(struct CacheHeader *cache, uint64_t key, void *value)
Adds a value to the cache with a uint64_t key, using string conversion.
Definition lru.c:113
find_in_cache_uint64
void * find_in_cache_uint64(struct CacheHeader *cache, uint64_t key)
Finds a value in the cache by uint64_t key, using string conversion.
Definition lru.c:93
free_cache
void free_cache(struct CacheHeader *cache)
Frees all entries in the cache and clears it.
Definition lru.c:130
add_to_cache
void add_to_cache(struct CacheHeader *cache, const char *key, void *value)
Adds a value to the cache with a string key, pruning if necessary.
Definition lru.c:47
find_in_cache
void * find_in_cache(struct CacheHeader *cache, const char *key)
Finds a value in the cache by string key.
Definition lru.c:24
CacheEntry
Single hash entry in the in-memory cache.
Definition lru.h:27
CacheEntry::value
void * value
Opaque value pointer associated with key (not freed automatically on eviction/clear).
Definition lru.h:29
CacheEntry::hh
UT_hash_handle hh
uthash handle linking this entry into the hash table (must remain last or per uthash docs).
Definition lru.h:30
CacheEntry::key
char * key
Null-terminated key string (unique within the cache). May encode numeric keys.
Definition lru.h:28
CacheHeader
Cache top-level descriptor encapsulating the hash table root and capacity limit.
Definition lru.h:46
CacheHeader::cache
struct CacheEntry * cache
Hash root (uthash). NULL when empty.
Definition lru.h:48
CacheHeader::max_items
uint64_t max_items
Hard limit for number of entries (policy: enforce/ignore depends on implementation).
Definition lru.h:47
CacheHeader::free_func
void(* free_func)(void *)
Optional callback to free cached values. NULL if values don't need freeing.
Definition lru.h:49