-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache.py
More file actions
executable file
·46 lines (34 loc) · 1.31 KB
/
cache.py
File metadata and controls
executable file
·46 lines (34 loc) · 1.31 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
#!/usr/local/bin/python3
def check(f):
"""A decorator checking for a file in cache before making request"""
def wrapped_f(self, *args, **kwargs):
if kwargs['_id'] in self.cached_files:
cached_file = self.get_from_cache(kwargs['_id'])
return {'status': 200, 'message': {'file': cached_file}}
else:
r = f(self, *args, **kwargs)
if r['status'] == 200:
self.add_to_cache(r['message']['file'])
return r
return wrapped_f
def update_on_add(f):
"""A decorator updating cache when a new file is addded"""
def wrapped_f(self, *args, **kwargs):
r = f(self, *args, **kwargs)
if r['status'] == 200:
self.add_to_cache({**kwargs, **r['message']['file']})
return r
return wrapped_f
def update_on_edit(f):
"""A decorator updating cache when a file is edited"""
def wrapped_f(self, *args, **kwargs):
r = f(self, *args, **kwargs)
if r['status'] == 200:
self.add_to_cache({**kwargs, **r['message']['file']})
else:
# there should be some code here to deal with
# the file not being allowed to update,
# as it may be a version issue!
print('file not updated succesfuly!')
return r
return wrapped_f