相关文章表明,一个双向链表的哈希表,但我甚至不清楚双向链表如何保持LRU。
我只是在猜测这里,但你可以做这样的事情(在这里使用伪C因为我很懒)。以下是一些基本的数据结构:
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
}
这里是如何你打开和关闭文件:
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
}
}
}
哈希表,您可以快速查找名称节点和链接列表,可以维持使用顺序。由于它们指向相同的节点,因此可以在它们之间切换。这可以让您按名称查看文件,但之后会在列表中移动它。
但我可能完全错误的所有这一切。
在某种程度上,这也似乎与您的操作系统的磁盘缓存功能相同。 – Thanatos 2010-06-12 04:31:17
你需要这个怎么样?如果您只是希望减少平均访问延迟,并且您正在全服务操作系统中运行,那么请让操作系统处理它。自己管理它是一种特殊情况... – dmckee 2010-06-12 04:44:08
执行大部分相同功能(提供大量小文件)的HTTP服务器通常包含自己的缓存,而不是使用操作系统的磁盘缓存。我打算把它作为一个指标,仅仅依靠操作系统来缓存并不是最佳解决方案。 – lazyconfabulator 2010-06-12 05:11:27