-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpy的LRU缓存机制.py
More file actions
42 lines (28 loc) · 797 Bytes
/
Copy pathpy的LRU缓存机制.py
File metadata and controls
42 lines (28 loc) · 797 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# !/usr/bin/python
# -*- coding: utf-8 -*-
"""
@File : py的LRU缓存机制.py
@Time : 2021/01/01 09:59:58
@Author : mayuan
@Version : 1.0
@Contact : 2901429479@qq.com
@License : (C)Copyright 2020-2021
@Desc : None
"""
class LRUCache(collections.OrderedDict):
def __init__(self, capacity: int):
super().__init__()
self.capacity = capacity
def get(self, key: int) -> int:
if key not in self:
return -1
self.move_to_end(key)
return self[key]
def put(self, key: int, value: int) -> None:
if key in self:
self.move_to_end(key)
self[key] = value
if len(self) > self.capacity:
self.popitem(last=False)
if __name__ == "__main__":
s = Solution()