Explorar el Código

[update] 添加DB

jhao104 hace 9 años
padre
commit
6c65f69f09
Se han modificado 3 ficheros con 114 adiciones y 3 borrados
  1. 52 0
      DB/DbClient.py
  2. 59 0
      DB/SsdbClient.py
  3. 3 3
      DB/__init__.py

+ 52 - 0
DB/DbClient.py

@@ -0,0 +1,52 @@
+# -*- coding: utf-8 -*-
+"""
+-------------------------------------------------
+   File Name:     DbClient.py  
+   Description :  DB工厂类
+   Author :       JHao
+   date:          2016/12/2
+-------------------------------------------------
+   Change Activity:
+                   2016/12/2: 
+-------------------------------------------------
+"""
+__author__ = 'JHao'
+
+
+class DbClient(object):
+    """
+    DbClient
+    """
+    def __init__(self, db_type, name, host='localhost', port=8080, **kwargs):
+        """
+        init
+        :param db_type: DB type
+        :param name:
+        :param host:
+        :param port:
+        :param kwargs:
+        :return:
+        """
+
+        __type = None
+        if "ssdb" in db_type.lower():
+            __type = "SsdbClient"
+        else:
+            pass
+        assert __type, 'type error, Not support DB type: {}'.format(db_type)
+
+        self.client = getattr(__import__(__type), __type)(name=name, host=host, port=port, **kwargs)
+
+    def get(self, **kwargs):
+        return self.client.get(**kwargs)
+
+    def put(self, **kwargs):
+        return self.client.put(**kwargs)
+
+    def pop(self, **kwargs):
+        return self.client.pop(**kwargs)
+
+
+if __name__ == "__main__":
+    account = DbClient('SSDB').pop()
+    print(account)

+ 59 - 0
DB/SsdbClient.py

@@ -0,0 +1,59 @@
+# -*- coding: utf-8 -*-
+"""
+-------------------------------------------------
+   File Name:     SsdbClient.py  
+   Description :  封装SSDB操作
+   Author :       JHao
+   date:          2016/12/2
+-------------------------------------------------
+   Change Activity:
+                   2016/12/2: 
+-------------------------------------------------
+"""
+__author__ = 'JHao'
+
+from ssdb.connection import BlockingConnectionPool
+from ssdb import SSDB
+import json
+
+
+class SsdbClient(object):
+    """
+    SSDB client
+    """
+
+    def __init__(self, name, host, port):
+        """
+        init
+        :param name:
+        :param host:
+        :param port:
+        :return:
+        """
+        self.name = name
+        self.__conn = SSDB(connection_pool=BlockingConnectionPool(host=host, port=port))
+
+    def get(self):
+        """
+        get an item from the queue front
+        :return:
+        """
+        value = self.__conn.qfront(name=self.name)
+        return value
+
+    def put(self, value):
+        """
+        put an  item in the back of a queue
+        :param value:
+        :return:
+        """
+        value = json.dump(value, ensure_ascii=False).encode('utf-8') if isinstance(value, (dict, list)) else value
+        return self.__conn.qpush_back(value)
+
+    def pop(self):
+        """
+        pop an item from the queue front
+        :return:
+        """
+        value = self.__conn.qpop_front(self.name)
+        return value[0] if value else value

+ 3 - 3
__init__.py → DB/__init__.py

@@ -1,12 +1,12 @@
 # -*- coding: utf-8 -*-
 """
 -------------------------------------------------
-   File Name:     __init__.py  
+   File Name:     __init__.py.py  
    Description :  
    Author :       JHao
-   date:          2016/11/25
+   date:          2016/12/2
 -------------------------------------------------
    Change Activity:
-                   2016/11/25: 
+                   2016/12/2: 
 -------------------------------------------------
 """