DbClient.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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.GetConfig import GetConfig
  18. from Util.utilClass import Singleton
  19. sys.path.append(os.path.dirname(os.path.abspath(__file__)))
  20. class DbClient(object):
  21. """
  22. DbClient DB工厂类 提供get/put/pop/delete/getAll/changeTable方法
  23. 目前存放代理的table/collection/hash有两种:
  24. raw_proxy: 存放原始的代理;
  25. useful_proxy_queue: 存放检验后的代理;
  26. 抽象方法定义:
  27. get(proxy): 返回proxy的信息;
  28. put(proxy): 存入一个代理;
  29. pop(): 弹出一个代理
  30. exists(proxy): 判断代理是否存在
  31. getNumber(raw_proxy): 返回代理总数(一个计数器);
  32. update(proxy, num): 修改代理属性计数器的值;
  33. delete(proxy): 删除指定代理;
  34. getAll(): 返回所有代理;
  35. changeTable(name): 切换 table or collection or hash;
  36. 所有方法需要相应类去具体实现:
  37. SSDB:SsdbClient.py
  38. REDIS:RedisClient.py
  39. """
  40. __metaclass__ = Singleton
  41. def __init__(self):
  42. """
  43. init
  44. :return:
  45. """
  46. self.config = GetConfig()
  47. self.__initDbClient()
  48. def __initDbClient(self):
  49. """
  50. init DB Client
  51. :return:
  52. """
  53. __type = None
  54. if "SSDB" == self.config.db_type:
  55. __type = "SsdbClient"
  56. elif "REDIS" == self.config.db_type:
  57. __type = "RedisClient"
  58. elif "MONGODB" == self.config.db_type:
  59. __type = "MongodbClient"
  60. else:
  61. pass
  62. assert __type, 'type error, Not support DB type: {}'.format(self.config.db_type)
  63. self.client = getattr(__import__(__type), __type)(name=self.config.db_name,
  64. host=self.config.db_host,
  65. port=self.config.db_port,
  66. password=self.config.db_password)
  67. def get(self, key, **kwargs):
  68. return self.client.get(key, **kwargs)
  69. def put(self, key, **kwargs):
  70. return self.client.put(key, **kwargs)
  71. def update(self, key, value, **kwargs):
  72. return self.client.update(key, value, **kwargs)
  73. def delete(self, key, **kwargs):
  74. return self.client.delete(key, **kwargs)
  75. def exists(self, key, **kwargs):
  76. return self.client.exists(key, **kwargs)
  77. def pop(self, **kwargs):
  78. return self.client.pop(**kwargs)
  79. def getAll(self):
  80. return self.client.getAll()
  81. def changeTable(self, name):
  82. self.client.changeTable(name)
  83. def getNumber(self):
  84. return self.client.getNumber()
  85. if __name__ == "__main__":
  86. account = DbClient()
  87. print(account.get())
  88. account.changeTable('use')
  89. account.put('ac')
  90. print(account.get())