DbClient.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. # -*- coding: utf-8 -*-
  2. # !/usr/bin/env python
  3. """
  4. -------------------------------------------------
  5. File Name: DbClient.py
  6. Description : DB工厂类
  7. Author : JHao
  8. date: 2016/12/2
  9. -------------------------------------------------
  10. Change Activity:
  11. 2016/12/02: DB工厂类
  12. 2020/07/03: 取消raw_proxy储存
  13. -------------------------------------------------
  14. """
  15. __author__ = 'JHao'
  16. import os
  17. import sys
  18. from util.six import urlparse
  19. from util.singleton import Singleton
  20. sys.path.append(os.path.dirname(os.path.abspath(__file__)))
  21. class DbClient(object):
  22. """
  23. DbClient DB工厂类 提供get/put/update/pop/delete/exists/getAll/clean/getCount/changeTable方法
  24. 抽象方法定义:
  25. get(): 随机返回一个proxy;
  26. put(proxy): 存入一个proxy;
  27. pop(): 顺序返回并删除一个proxy;
  28. update(proxy): 更新指定proxy信息;
  29. delete(proxy): 删除指定proxy;
  30. exists(proxy): 判断指定proxy是否存在;
  31. getAll(): 返回所有代理;
  32. clean(): 清除所有proxy信息;
  33. getCount(): 返回proxy统计信息;
  34. changeTable(name): 切换操作对象
  35. 所有方法需要相应类去具体实现:
  36. ssdb: ssdbClient.py
  37. redis: redisClient.py
  38. mongodb: mongodbClient.py
  39. """
  40. __metaclass__ = Singleton
  41. def __init__(self, db_conn):
  42. """
  43. init
  44. :return:
  45. """
  46. self.db_conn = db_conn
  47. self.parseDbConn(db_conn)
  48. self.__initDbClient()
  49. @classmethod
  50. def parseDbConn(cls, db_conn):
  51. db_conf = urlparse(db_conn)
  52. cls.db_type = db_conf.scheme.upper().strip()
  53. cls.db_host = db_conf.hostname
  54. cls.db_port = db_conf.port
  55. cls.db_user = db_conf.username
  56. cls.db_pwd = db_conf.password
  57. cls.db_name = db_conf.path[1:]
  58. return cls
  59. def __initDbClient(self):
  60. """
  61. init DB Client
  62. :return:
  63. """
  64. __type = None
  65. if "SSDB" == self.db_type:
  66. __type = "ssdbClient"
  67. elif "REDIS" == self.db_type:
  68. __type = "redisClient"
  69. elif "MONGODB" == self.db_type:
  70. __type = "mongodbClient"
  71. else:
  72. pass
  73. assert __type, 'type error, Not support DB type: {}'.format(self.db_type)
  74. self.client = getattr(__import__(__type), "%sClient" % self.db_type.title())(host=self.db_host,
  75. port=self.db_port,
  76. username=self.db_user,
  77. password=self.db_pwd,
  78. db=self.db_name)
  79. def get(self, **kwargs):
  80. return self.client.get(**kwargs)
  81. def put(self, key, **kwargs):
  82. return self.client.put(key, **kwargs)
  83. def update(self, key, value, **kwargs):
  84. return self.client.update(key, value, **kwargs)
  85. def delete(self, key, **kwargs):
  86. return self.client.delete(key, **kwargs)
  87. def exists(self, key, **kwargs):
  88. return self.client.exists(key, **kwargs)
  89. def pop(self, **kwargs):
  90. return self.client.pop(**kwargs)
  91. def getAll(self):
  92. return self.client.getAll()
  93. def clear(self):
  94. return self.client.clear()
  95. def changeTable(self, name):
  96. self.client.changeTable(name)
  97. def getCount(self):
  98. return self.client.getCount()