mirror of
https://github.com/luzhisheng/js_reverse.git
synced 2025-04-20 10:25:01 +08:00
42 lines
1.2 KiB
Python
42 lines
1.2 KiB
Python
from datetime import datetime
|
|
import threading
|
|
import pymongo
|
|
import settings
|
|
|
|
|
|
class MyMongodb(object):
|
|
_instance_lock = threading.Lock()
|
|
|
|
def __new__(cls, *kw):
|
|
"""调用mongo数据库 使用单列模式避免创建多个对象,导致内存泄露"""
|
|
if not hasattr(cls, '_instance'):
|
|
with cls._instance_lock:
|
|
if not hasattr(cls, '_instance'):
|
|
cls._instance = object.__new__(cls)
|
|
|
|
return cls._instance
|
|
|
|
def __init__(self):
|
|
kw = settings.MONGODB_CONF
|
|
self.client = pymongo.MongoClient(kw.get('host'), int(kw.get('port')), connect=False)
|
|
self.db = self.client[kw.get('db')]
|
|
# self.db.authenticate(
|
|
# kw.get('username'), kw.get('pwd'), source=kw.get('source'))
|
|
|
|
def shutdown(self):
|
|
self.client.close()
|
|
|
|
|
|
class MongoDao(object):
|
|
|
|
def __init__(self):
|
|
self.client = MyMongodb().db
|
|
|
|
def insert_item(self, collection, item):
|
|
collection = self.client[collection]
|
|
if collection.find_one({"sign": item['sign']}):
|
|
print(f"【{datetime.now()}】过滤")
|
|
else:
|
|
print(f"【{datetime.now()}】入库{item.get('sign')}")
|
|
return collection.insert_one(item)
|