from pymongo import MongoClient from datetime import datetime,timedelta import zlib import pickle class MongoCache: def __init__(self,client=None,expires=timedelta(days=30)): self.client=MongoClient('localhost',27017) self.db=client['cache'] #新建数据库 '''Mongodb不需要创建表,会自动创建''' self.db.webpage.create_index('timestamp', expireAfterSeconds=expires.total_seconds()) def __getitem__(self, item): record=self.db.webpage.find_one({'_id':item}) if record: return pickle.loads(zlib.decompress(record['result'])) else: raise KeyError(item+' dose not exist!') def __setitem__(self, key, value): record={'result':zlib.compress(pickle.dumps(value)),'timestamp':datetime.utcnow()} self.db.webpage.update({'_id':key},{'$set':record},upset=True)
Mongodb缓存爬虫数据
最新推荐文章于 2019-09-01 16:13:00 发布