-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlru_algorithm.py
More file actions
56 lines (43 loc) · 1.36 KB
/
lru_algorithm.py
File metadata and controls
56 lines (43 loc) · 1.36 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# -*- coding: utf-8 -*-
# @File : lru_algorithm.py
# @Date : 2021-12-17
# @Author : windyzhao
import collections
"""
使用有序字典存储
"""
class LRUAlgorithm(object):
def __init__(self, length):
self.order_dict = collections.OrderedDict()
self.length = length # 最长的限制
self.now_length = 0 # 当前长度
def get(self, key):
value = self.order_dict.pop(key, None)
if value is None:
return -1
self.order_dict[key] = value # 放到头部
return value
def put(self, key, values):
if self.now_length >= self.length:
self.order_dict.popitem(last=False) # 去掉最后一个(最先那个)
value = self.order_dict.pop(key, False)
self.order_dict[key] = value or values
self.now_length += 1
if __name__ == '__main__':
lru = LRUAlgorithm(length=3)
print("a:{}".format(lru.get("a")))
print("=============")
lru.put("a", 1)
lru.put("b", 2)
lru.put("c", 3)
lru.put("d", 4)
lru.put("e", 5)
print("=============")
print("a:{}".format(lru.get("a")))
print("b:{}".format(lru.get("b")))
print("c:{}".format(lru.get("c")))
print("d:{}".format(lru.get("d")))
print("e:{}".format(lru.get("e")))
print("c:{}".format(lru.get("c")))
print("============")
print(lru.order_dict)