caching - 如何存储数百万个缓存,然后追踪 20 个最旧的缓存

标签 caching data-structures language-agnostic

我收到一个面试问题,说我需要存储几百万个缓存,然后我需要跟踪 20 个最旧的缓存,一旦缓存收集的阈值增加,就用下一组最旧的缓存替换 20 个最旧的缓存.

I answered to keep a hashmap for it, again the question increases what if we wanna access any of the element on hashmap fastly, how to do, so I told its map so accessing won't be time taking but interviewer was not satisfied. So what should be the idle way for such scenarios.

最佳答案

一个queue非常适合查找和删除最旧的成员。

A queue implemented as a doubly linked list has O(1) insertion and deletion at both ends.

一个priority queue有助于为队列中的不同项目赋予不同的权重(例如,某些队列元素的重新创建成本可能比其他元素更高)。

您可以使用 HashMap 来保存实际元素并根据哈希键快速找到它们,并使用哈希键的队列来跟踪缓存元素的生命周期。

关于caching - 如何存储数百万个缓存,然后追踪 20 个最旧的缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10425293/

相关文章:

ASP.NET 后台处理

c++ - 我可以在父构造函数中多次重用函数的返回值吗?

二维区间树的Java实现

algorithm - 排序算法以保持排序位置编号更新

algorithm - 稳定、高效的排序?

algorithm - 我如何使用 Trie 进行拼写检查

python - 如何使用 Python 在 Redis 中写入和读取自定义对象?

reactjs - 缓存和redux有什么区别

C++ : Data Structure withe fast searching and less memory requirements

c++ - 内存优化结构cpp