DbClient.py 3.7 KB

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