CernVM-FS
2.12.0
|
#include <stdint.h>
#include <algorithm>
#include <cassert>
#include <cstring>
#include <functional>
#include <map>
#include <string>
#include "smallhash.h"
#include "statistics.h"
#include "util/atomic.h"
#include "util/platform.h"
#include "util/single_copy.h"
#include "util/smalloc.h"
Go to the source code of this file.
Namespaces | |
lru | |
Macros | |
#define | LRU_CACHE_THREAD_SAFE |
#define LRU_CACHE_THREAD_SAFE |
This file is part of the CernVM File System.
This class provides an Least Recently Used (LRU) cache for arbitrary data It stores Key-Value pairs of arbitrary data types in a hash table and automatically deletes the entries which are least touched in the last time to prevent the structure from growing beyond a given maximal cache size. The cache uses a hand crafted memory allocator to use memory efficiently
Hash functions have to be provided. They should return an equal distribution of keys in uint32_t. In addition, a special key has to be provided that is used to mark "empty" elements in the hash table.
The cache size has to be a multiply of 64.
usage: // 100 entries, -1 special key LruCache<int, string> cache(100, -1, hasher_int);
// Inserting some stuff cache.insert(42, "fourtytwo"); cache.insert(2, "small prime number"); cache.insert(1337, "leet");
// Trying to retrieve a value int result; if (cache.lookup(21, result)) { cout << "cache hit: " << result << endl; } else { cout << "cache miss" << endl; }
cache.drop(); // Empty the cache