| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- # -*- coding: utf-8 -*-
- # !/usr/bin/env python
- """
- -------------------------------------------------
- File Name: DbClient.py
- Description : DB工厂类
- Author : JHao
- date: 2016/12/2
- -------------------------------------------------
- Change Activity:
- 2016/12/2:
- -------------------------------------------------
- """
- __author__ = 'JHao'
- import os
- import sys
- from Util.GetConfig import config
- from Util.utilClass import Singleton
- sys.path.append(os.path.dirname(os.path.abspath(__file__)))
- class DbClient(object):
- """
- DbClient DB工厂类 提供get/put/pop/delete/getAll/changeTable方法
- 目前存放代理的table/collection/hash有两种:
- raw_proxy: 存放原始的代理;
- useful_proxy_queue: 存放检验后的代理;
- 抽象方法定义:
- get(proxy): 返回proxy的信息;
- put(proxy): 存入一个代理;
- pop(): 弹出一个代理
- exists(proxy): 判断代理是否存在
- getNumber(raw_proxy): 返回代理总数(一个计数器);
- update(proxy, num): 修改代理属性计数器的值;
- delete(proxy): 删除指定代理;
- getAll(): 返回所有代理;
- changeTable(name): 切换 table or collection or hash;
- 所有方法需要相应类去具体实现:
- SSDB:SsdbClient.py
- REDIS:RedisClient.py
- """
- __metaclass__ = Singleton
- def __init__(self):
- """
- init
- :return:
- """
- self.__initDbClient()
- def __initDbClient(self):
- """
- init DB Client
- :return:
- """
- __type = None
- if "SSDB" == config.db_type:
- __type = "SsdbClient"
- elif "REDIS" == config.db_type:
- __type = "RedisClient"
- elif "MONGODB" == config.db_type:
- __type = "MongodbClient"
- else:
- pass
- assert __type, 'type error, Not support DB type: {}'.format(config.db_type)
- self.client = getattr(__import__(__type), __type)(name=config.db_name,
- host=config.db_host,
- port=config.db_port,
- password=config.db_password)
- def get(self, key, **kwargs):
- return self.client.get(key, **kwargs)
- def put(self, key, **kwargs):
- return self.client.put(key, **kwargs)
- def update(self, key, value, **kwargs):
- return self.client.update(key, value, **kwargs)
- def delete(self, key, **kwargs):
- return self.client.delete(key, **kwargs)
- def exists(self, key, **kwargs):
- return self.client.exists(key, **kwargs)
- def pop(self, **kwargs):
- return self.client.pop(**kwargs)
- def getAll(self):
- return self.client.getAll()
- def changeTable(self, name):
- self.client.changeTable(name)
- def getNumber(self):
- return self.client.getNumber()
- if __name__ == "__main__":
- account = DbClient()
- print(account.get())
- account.changeTable('use')
- account.put('ac')
- print(account.get())
|