SsdbClient.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. # -*- coding: utf-8 -*-
  2. """
  3. -------------------------------------------------
  4. File Name: SsdbClient.py
  5. Description : 封装SSDB操作
  6. Author : JHao
  7. date: 2016/12/2
  8. -------------------------------------------------
  9. Change Activity:
  10. 2016/12/2:
  11. -------------------------------------------------
  12. """
  13. __author__ = 'JHao'
  14. from ssdb.connection import BlockingConnectionPool
  15. from ssdb import SSDB
  16. import json
  17. class SsdbClient(object):
  18. """
  19. SSDB client
  20. """
  21. def __init__(self, name, host, port):
  22. """
  23. init
  24. :param name:
  25. :param host:
  26. :param port:
  27. :return:
  28. """
  29. self.name = name
  30. self.__conn = SSDB(connection_pool=BlockingConnectionPool(host=host, port=port))
  31. def get(self):
  32. """
  33. get an item from the queue front
  34. :return:
  35. """
  36. value = self.__conn.qfront(name=self.name)
  37. return value
  38. def put(self, value):
  39. """
  40. put an item in the back of a queue
  41. :param value:
  42. :return:
  43. """
  44. value = json.dump(value, ensure_ascii=False).encode('utf-8') if isinstance(value, (dict, list)) else value
  45. return self.__conn.qpush_back(value)
  46. def pop(self):
  47. """
  48. pop an item from the queue front
  49. :return:
  50. """
  51. value = self.__conn.qpop_front(self.name)
  52. return value[0] if value else value