Skip to content

Instantly share code, notes, and snippets.

@vinnyt
Forked from jehiah/lru_cache.c
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vinnyt/e16dc658c3d0d7ae25ee to your computer and use it in GitHub Desktop.
Save vinnyt/e16dc658c3d0d7ae25ee to your computer and use it in GitHub Desktop.
#include <string.h>
#include <uthash.h>
// this is an example of how to do a LRU cache in C using uthash
// http://uthash.sourceforge.net/
// by Jehiah Czebotar 2011 - jehiah@gmail.com
// this code is in the public domain http://unlicense.org/
#define MAX_CACHE_SIZE 100000
struct CacheEntry {
char *key;
char *value;
UT_hash_handle hh;
};
struct CacheEntry *cache = NULL;
char* find_in_cache(char *key)
{
struct CacheEntry *entry;
HASH_FIND_STR(cache, key, entry);
if (entry) {
// remove it (so the subsequent add will throw it on the front of the list)
HASH_DELETE(hh, cache, entry);
HASH_ADD_KEYPTR(hh, cache, entry->key, strlen(entry->key), entry);
return entry->value;
}
return NULL;
}
void add_to_cache(char *key, char *value)
{
struct CacheEntry *entry, *tmp_entry;
entry = malloc(sizeof(struct CacheEntry));
entry->key = strdup(key);
entry->value = strdup(value);
HASH_ADD_KEYPTR(hh, cache, entry->key, strlen(entry->key), entry);
// prune the cache to MAX_CACHE_SIZE
if (HASH_COUNT(cache) >= MAX_CACHE_SIZE) {
HASH_ITER(hh, cache, entry, tmp_entry) {
// prune the first entry (loop is based on insertion order so this deletes the oldest item)
HASH_DELETE(hh, cache, entry);
free(entry->key);
free(entry->value);
free(entry);
break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment