src/pyams_notify_ws/notify.py
changeset 1 f2910a60a29a
parent 0 284c0976e3ff
child 8 a285b7e8217c
--- 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):