-
Notifications
You must be signed in to change notification settings - Fork 6
/
cache.py
47 lines (32 loc) · 864 Bytes
/
cache.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
47
# -*- coding: utf-8 -*-
import sys
from workflow import Workflow
class Hasher:
def __init__(self, wf):
self.wf = wf
self.max_age = 60 * 60 * 24 * 365
def cache(self, query):
if not query:
sys.stdout.write("")
sys.stdout.flush()
return
result = query.split(":", 1)
key = result[0]
value = result[1]
count = self.wf.cached_data(key, max_age=self.max_age)
if not count:
count = 0
count += 1
self.wf.cache_data(key, count)
sys.stdout.write(value)
sys.stdout.flush()
def main(wf):
query = None
if len(wf.args):
query = wf.args[0]
hasher = Hasher(wf)
hasher.cache(query)
if __name__ == u"__main__":
wf = Workflow()
wf.cache_serializer = 'json'
sys.exit(wf.run(main))