class FIFOCache(object): def __init__(self, capacity): self.capacity = capacity self.size = 0 self.map = {} self.list = DoubleLinkedList(self.capacity) def get(self, key): if key not in self.map: return -1 else: node = self.map.get(key) return node.value def put(self, key, value): if self.capacity == 0: return if key in self.map: node = self.map.get(key) self.list.remove(node) node.value = value self.list.append(node) else: if self.size == self.capacity: node = self.list.pop() del self.map[node.key] self.size -= 1 node = Node(key, value) self.list.append(node) self.map[key] = node self.size += 1 def print(self): self.list.print()
def put(self, key, value): if self.capacity == 0: return # 缓存命中 if key in self.map: node = self.map.get(key) node.value = value self.__update_freq(node) # 缓存没有命中 else: if self.capacity == self.size: min_freq = min(self.freq_map) node = self.freq_map[min_freq].pop() del self.map[node.key] self.size -= 1 node = LFUNode(key, value) node.freq = 1 self.map[key] = node if node.freq not in self.freq_map: self.freq_map[node.freq] = DoubleLinkedList() self.freq_map[node.freq].append(node) self.size += 1
def put(self, key, value): if self.capacity == 0: return # 缓存命中情况(即缓存里有该结点) if key in self.map: node = self.map.get(key) node.value = value self.__update_freq(node) else: # 如果缓存已满,则需要淘汰使用频率链表中的头结点 if self.capacity == self.size: min_freq = min(self.freq_map) node = self.freq_map[min_freq].pop() del self.map[node.key] self.size -= 1 # 下面就是通用插入逻辑 node = LFUNode(key, value) node.freq = 1 self.map[key] = node if node.freq not in self.freq_map: self.freq_map[node.freq] = DoubleLinkedList() node = self.freq_map[node.freq].append(node) self.size += 1
def __update_freq__(self, node): freq = node.freq node = self.freq_map[freq].remove(node) if self.freq_map[freq].size == 0: del self.freq_map[freq] freq += 1 node.freq = freq if freq not in self.freq_map: self.freq_map[freq] = DoubleLinkedList() self.freq_map[freq].append(node)
def __update_freq(self, node): freq = node.freq # 使用过后要到最前面,故先删除,下面在 append 到头部 node = self.freq_map[freq].remove(node) if self.freq_map[freq].size == 0: del self.freq_map[freq] # 且使用频率要增加 1 freq += 1 node.freq = freq if freq not in self.freq_map: # 没有对应的频率链表的话要创建一个 self.freq_map[freq] = DoubleLinkedList() self.freq_map[freq].append(node)
def __init__(self, capacity): self.capacity = capacity self.map = {} self.list = DoubleLinkedList(self.capacity)
class LRUCache(object): def __init__(self, capacity): self.capacity = capacity self.map = {} self.list = DoubleLinkedList(self.capacity) # 使用(或获取)一个结点,此时它就是最近使用的,故要提前 def get(self, key): if key in self.map: node = self.map[key] self.list.remove(node) self.list.append_front(node) return node.value else: return -1 def put(self, key, value): # 已存在结点 if key in self.map: node = self.map.get(key) self.list.remove(node) node.value = value self.list.append_front(node) else: # 新结点 node = Node(key, value) # 缓存已满的话先删掉末尾的元素 if self.list.size >= self.capacity: old_node = self.list.remove() self.map.pop(old_node.key) self.list.append_front(node) self.map[key] = node def print(self): self.list.print()
class LRUCache(ICache): def __init__(self, capacity): self.capacity = capacity self.map = {} self.list = DoubleLinkedList(self.capacity) def set(self, key, value): if self.capacity == 0: return if key in self.map: node = self.map.get(key) self.list.remove(node) node.value = value self.list.append_head(node) else: node = Node(key, value) if self.list.size >= self.list.capacity: old_node = self.list.remove() self.map.pop(old_node.key) self.map[key] = node self.list.append_head(node) def get(self, key): if key not in self.map: return -1 else: node = self.map.get(key) self.list.remove(node) self.list.append_head(node) return node.value
class LRUCache(object): def __init__(self, capacity): self.capacity = capacity self.map = {} self.list = DoubleLinkedList(self.capacity) def get(self, key): if key in self.map: node = self.map[key] self.list.remove(node) self.list.append_front(node) return node.value else: return -1 def put(self, key, value): if key in self.map: node = self.get(key) self.list.remove(node) node.value = value self.list.append_front(node) else: node = Node(key, value) # 缓存满了 if self.list.size >= self.list.capacity: old_node = self.list.remove() self.map.pop(old_node.key) self.list.append_front(node) self.map[key] = node def print(self): self.list.print()