-
Notifications
You must be signed in to change notification settings - Fork 0
/
little_gadgets.py
47 lines (32 loc) · 1.01 KB
/
little_gadgets.py
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
import time
import redis
# handle login issues
conn = redis.Redis()
def check_token(token):
return conn.hget('login:', token)
def update_token(token, user, item=None):
timestamp = time.time()
conn.hset('login:', token, user)
conn.zadd('recent:', token, timestamp)
# 记录浏览过的商品
if item:
conn.zadd('viewed:'+token, user)
conn.zremrangebyrank('viewed:', token, 0, -26)
QUIT = False
LIMIT = 10000000
# another thread clean the redis DB
def clean_sessions():
while not QUIT:
size = conn.zcard()
if size <= LIMIT:
time.sleep(1)
continue
end_index = min(size-LIMIT, 100) #
tokens = conn.zrange('recent:', 0, end_index-1)
session_keys = []
for token in tokens:
session_keys.append('viewd:'+token)
# 作用:把数组中的每一个数都拿出来调用一边这个方法
conn.delete(*session_keys)
conn.hdel('login:', *tokens)
conn.zrem('recent:', *tokens)