Commits

Reid Draper  committed 91ee7b7

add mset and return None instead of False if the key doesn't exist

  • Participants
  • Parent commits 41cdafe

Comments (0)

Files changed (2)

 from tornado import iostream, ioloop
 import swirl
 
+def _flatten(l):
+    result = []
+    for item in l:
+        result.extend(item)
+    return result
+
 class RedisTornado(object):
     def __init__(self, host = None, port = None, encoding = 'utf-8'):
         """
         if not self._waiting:
             self._work()
         
+    def mset(self, kv_list, callback = None):
+        cmd = self._cmd_creator('MSET', _flatten(kv_list))
+        self._queue.append((cmd, callback))
+        if not self._waiting:
+            self._work()
+
     def get(self, key, callback = None):
         cmd = self._cmd_creator('GET', [key])
         self._queue.append((cmd, callback))
         if not self._waiting:
             self._work()
+
         
 class GetHandler(tornado.web.RequestHandler):
     @swirl.asynchronous
     def get(self, key):
-        value = (yield lambda cb : r.get(key, cb)) or str(False)
+        value = (yield lambda cb : r.get(key, cb)) or 'None'
         self.write(value)
 
 class SetHandler(tornado.web.RequestHandler):
         response = yield lambda cb: r.set(key, value, cb)
         self.write(str(response))
 
+class MSetHandler(tornado.web.RequestHandler):
+    @swirl.asynchronous
+    def post(self):
+        kv_list = [('foo', 'bar'), ('a', 'b'), ('c', 'd')]
+        response = yield lambda cb: r.mset(kv_list, cb)
+        self.write(str(response))
+
 
 def main():
     tornado.options.parse_command_line()
     application = tornado.web.Application([
-        (r'/get/(.+)',   GetHandler),
-        (r'/set/(.+)',   SetHandler),
+        (r'/get/(.+)',  GetHandler),
+        (r'/set/(.+)',  SetHandler),
+        (r'/mset/*',      MSetHandler),
     ], **settings)
     http_server = tornado.httpserver.HTTPServer(application)
     http_server.listen(options.port)