-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlocalCache.py
More file actions
95 lines (73 loc) · 1.99 KB
/
localCache.py
File metadata and controls
95 lines (73 loc) · 1.99 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/usr/bin/python
# coding:utf8
"""
@author: Cong Yu
@time: 2020-05-24 23:12
"""
import time
class LocalCache:
def __init__(self):
self.dict = {}
@staticmethod
def now_time():
return int(time.time())
def get(self, key):
# 这里面采用查询时,删除过期key 的策略: 查询,根据设置的时间,定时
value = self.dict.get(key, None)
if value:
# 判断 当前 key 是否过期
expire_time = value.get("expire", None)
if expire_time is not None:
if self.now_time() - value["time"] < expire_time:
return value["value"]
else:
self.dict.pop(key)
return None
else:
return value["value"]
else:
return None
def expire(self, key, t):
value = self.dict.get(key, None)
if value:
# 判断 当前 key 是否过期
self.dict[key]["expire"] = t
return True
else:
return False
def set(self, key, value):
try:
# 记录下 key 创建的时间
self.dict[key] = {"value": value, "time": self.now_time()}
return True
except Exception as e:
print(repr(e))
return False
if __name__ == "__main__":
# 测试下 自己实现的 模拟 本地redis
"""
核心逻辑基本完成
"""
redis = LocalCache()
a = redis.get("1")
print("初始情况:", a)
a = redis.set("1", 1233124124)
print("设置key", a)
a = redis.get("1")
print("获取key", a)
time.sleep(3)
a = redis.get("1")
print("3s获取key", a)
a = redis.expire("1", 5)
print("设置时间3s", a)
a = redis.get("1")
print("3s", a)
time.sleep(1)
a = redis.get("1")
print("1s", a)
time.sleep(1)
a = redis.get("1")
print("1s", a)
time.sleep(1)
a = redis.get("1")
print("1s", a)