Updated cache management using pyams_cache package
authorThierry Florac <thierry.florac@onf.fr>
Thu, 01 Jun 2017 15:14:48 +0200
changeset 1 f2910a60a29a
parent 0 284c0976e3ff
child 2 4bdc5043fd3d
Updated cache management using pyams_cache package
src/pyams_notify_ws/include.py
src/pyams_notify_ws/interfaces/__init__.py
src/pyams_notify_ws/notify.py
--- a/src/pyams_notify_ws/include.py	Thu Jun 02 16:02:23 2016 +0200
+++ b/src/pyams_notify_ws/include.py	Thu Jun 01 15:14:48 2017 +0200
@@ -18,6 +18,7 @@
 # import interfaces
 
 # import packages
+from pyams_notify_ws.interfaces import CACHE_CONFIGURATION_KEY
 
 
 def include_package(config):
@@ -31,7 +32,7 @@
     config.add_route('subscribe', '/subscribe')
     config.add_route('notify', '/notify')
 
-    memcached_server = config.registry.settings.get('pyams_notify_ws.memcached_server')
-    if memcached_server:
-        from .notify import init_memcached_client
-        init_memcached_client(memcached_server)
+    cache_server = config.registry.settings.get(CACHE_CONFIGURATION_KEY)
+    if cache_server:
+        from .notify import init_cache_client
+        init_cache_client(cache_server)
--- a/src/pyams_notify_ws/interfaces/__init__.py	Thu Jun 02 16:02:23 2016 +0200
+++ b/src/pyams_notify_ws/interfaces/__init__.py	Thu Jun 01 15:14:48 2017 +0200
@@ -16,8 +16,8 @@
 # import standard library
 
 # import interfaces
-from zope.interface import Interface
 
 # import packages
 
-from pyams_notify_ws import _
+
+CACHE_CONFIGURATION_KEY = 'pyams_notify_ws.cache_server'
--- a/src/pyams_notify_ws/notify.py	Thu Jun 02 16:02:23 2016 +0200
+++ b/src/pyams_notify_ws/notify.py	Thu Jun 01 15:14:48 2017 +0200
@@ -20,27 +20,27 @@
 import pickle
 
 # import interfaces
+from pyams_cache.interfaces import IAioCacheHandler
 
 # import packages
-from aiomcache import Client as MemcachedClient
 from aiopyramid.websocket.config.gunicorn import WebsocketMapper
 from aiopyramid.websocket.view import WebsocketConnectionView
+from pyams_cache.cache import get_cache_handler
 from pyams_notify_ws.subscribe import users
 from pyramid.view import view_config
 
 
 queue = collections.deque(maxlen=50)
-queue_key = b'_PyAMS_notify_messages_queue_'
+queue_key = b'PyAMS:notify:messages_queue'
 queue_lock = asyncio.Lock()
 
-memcached_client = None
+cache_handler = None
 
 
-def init_memcached_client(server):
-    """Initialize memcached handler"""
-    global memcached_client
-    ip, port = server.split(':')
-    memcached_client = MemcachedClient(ip, int(port))
+def init_cache_client(server):
+    """Initialize cache handler"""
+    global cache_handler
+    cache_handler = get_cache_handler(server, IAioCacheHandler)
 
 
 @view_config(route_name='notify', mapper=WebsocketMapper)
@@ -67,15 +67,15 @@
                     if subscription.filter_target(target):
                         yield from connection.send(json_message)
                 # store message in memcached_queue
-                if memcached_client is not None:
+                if cache_handler is not None:
                     with (yield from queue_lock):
-                        mem_queue = yield from memcached_client.get(queue_key)
+                        mem_queue = yield from cache_handler.get(queue_key)
                         if mem_queue is None:
                             mem_queue = queue
                         else:
                             mem_queue = pickle.loads(mem_queue)
                         mem_queue.append(message)
-                        yield from memcached_client.set(queue_key, pickle.dumps(mem_queue))
+                        yield from cache_handler.set(queue_key, pickle.dumps(mem_queue))
 
     @asyncio.coroutine
     def on_close(self):