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)  #freq_map里的key都是数字
                node = self.freq_map[min_freq].pop()  #默认pop是head
                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 __init__(self, capacity):
     self.capacity = capacity
     self.map = {}  # 保存key和value的映射关系
     # key: 频率, value:频率对应的双向链表
     self.freq_map = {}
     self.list = DoubleLinkedList(self.capacity)
     self.size = 0
    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 = freq + 1
        node.freq = freq
        if freq not in self.freq_map:
            self.freq_map[freq] = DoubleLinkedList()
        self.freq_map[freq].append(node)
class FIFOCache(object):
    def __init__(self, capacity):
        self.capacity = capacity  # 容量大小
        self.size = 0
        self.map = {}  # 保存key和value的映射关系
        self.list = DoubleLinkedList(self.capacity)

    # 从缓存中获取key对应的value内容,如果key不存在就返回-1
    def get(self, key):
        if key not in self.map:
            return -1
        else:
            node = self.map.get(key)  # get是dict的方法,取出key对应的value
            return node.value

    # 添加一个缓存
    def put(self, key, value):
        if self.capacity == 0:
            return
        if key in self.map:  # 如果key已经存在,就把它的先删掉掉再放新的到最后
            node = self.map.get(key)
            self.list.remove(node)
            node.value = value  # 覆盖
            self.list.append(node)
        else:
            if self.size == self.capacity:  # 如果该key是新的,先判断缓存是否满了,满了要pop头部节点
                node = self.list.pop()
                del self.map[node.key]  # 本地映射map中删除这个head节点
                self.size -= 1
            node = Node(key, value)  # 新增一个节点
            self.list.append(node)
            self.map[key] = node  # 本地map中增加这个节点的映射
            self.size += 1

    def print(self):
        self.list.print()
 def __init__(self, capacity):
     self.capacity = capacity
     self.map = {}  # 保存key和value的映射关系
     self.list = DoubleLinkedList(self.capacity)
class LRUCache():
    def __init__(self, capacity):
        self.capacity = capacity
        self.map = {}  # 保存key和value的映射关系
        self.list = DoubleLinkedList(self.capacity)

    # 每次使用,把使用的节点放到链表最前面,返回值
    def get(self, key):
        if key in self.map:
            node = self.map[key]  # 取出这个key和它的值
            self.list.remove(node)
            self.list.append_front(node)  #把这个缓存放到最前面
            return node.value
        else:
            return -1

    #添加一个缓存
    def put(self, key, value):
        # 先判断key是否已经在缓存里:
        if key in self.map:  #是在缓存里:更新value,放入最前面
            node = self.map.get(key)
            self.list.remove(node)
            node.value = value
            self.list.append_front(node)
        else:
            node = Node(key, value)
            #缓存已经满了的话需要先删掉最后一个再往head增加新的:
            if self.list.size >= self.list.capacity - 1:
                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()