Related records indicated that the hash table is a doubly linked list, but I don’t even understand how a doubly linked list stores LRUs.
I'm just guessing here, but you can do something like this (using pseudo-C here, because I'm lazy). Here are the basic data structures:
struct File { // hash key string Name; // doubly-linked list File* Previous; File* Next; // other file data... } struct Cache { HashTable<string, File*> Table // some existing hashtable implementation File* First; // most recent File* Last; // least recent }
And here is how you can open and close the file:
File* Open(Cache* cache, string name) { if (look up name in cache->Table succeeds) { File* found = find it from the hash table lookup move it to the front of the list } else { File* newFile = open the file and create a new node for it insert it at the beginning of the list if (the cache is full now) { remove the last file from the list close it remove it from the hashtable too } } }
A hash table allows you to quickly find nodes by name, and a linked list allows you to maintain them in order of use. Since they point to the same nodes, you can switch between them. This allows you to view the file by name, and then move it around the list in the future.
But I could be completely mistaken in all of this.
source share