我收到一个面试问题,说我需要存储几百万个缓存,然后我需要跟踪 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/