-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb_cache.py
More file actions
executable file
·51 lines (44 loc) · 1.31 KB
/
db_cache.py
File metadata and controls
executable file
·51 lines (44 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
47
48
49
50
51
from datetime import datetime, timedelta
from pymongo import MongoClient
import pickle
import zlib
from bson.binary import Binary
class MongoCache:
def __init__(self, client=None, expires=timedelta(days=30)):
#connect to mongoDB
if client is None:
self.client = MongoClient('localhost', 27017)
else:
self.client = client
#create collection to store cached webpages,
self.db = self.client.cache
#create index to expire cached webpages
self.db.webpage.create_index('timestamp', expireAfterSeconds=expires.total_seconds())
def __getitem__(self, url):
#use object[x] to call this function
record = self.db.webpage.find_one({'_id': url})
if record:
plain_data = pickle.loads(zlib.decompress(record['result']))
#print plain_data
return plain_data
else:
raise KeyError(url + ' does not exist')
def __setitem__(self, url, result):
"""Save value for this URL
"""
record = {
'result': Binary(zlib.compress(pickle.dumps(result))),
'timestamp': datetime.utcnow()
}
self.db.webpage.update({'_id': url}, {'$set': record}, upsert=True)
###
### test
###
def main():
cache = MongoCache(expires=timedelta())
cache.db.webpage.remove()
#result = {'html': '...'}
#cache[url] = result
#cache[url]
if __name__ == '__main__':
main()