Changeset 4e30762c5ca3…
Parent fe00d58e10cc…
by Benjamin Pollack
Changes to 2 files · Browse files at 4e30762c5ca3 Showing diff from parent fe00d58e10cc Diff from another changeset...
|
@@ -1,7 +1,9 @@ #!/usr/bin/env python
import getopt
+import os
import re
+import pickle
import select
import socket
import sys
@@ -38,7 +40,7 @@ self.table = None
class MiniRedis(threading.Thread):
- def __init__(self, host='127.0.0.1', port=56784, logging=False):
+ def __init__(self, host='127.0.0.1', port=56784, logging=False, db_file=None):
super(MiniRedis, self).__init__()
self.host = host
self.port = port
@@ -46,10 +48,14 @@ self.halt = True
self.tables = {}
self.clients = {}
+ self.db_file = db_file
+
+ self.load()
def log(self, client, s):
if self.logging:
- print '%s:%s: %s' % (client.socket.getpeername() + (s,))
+ who = '%s:%s' % client.socket.getpeername() if client else 'SERVER'
+ print '%s: %s' % (who, s)
def select(self, client, db):
if db not in self.tables:
@@ -57,6 +63,18 @@ client.db = db
client.table = self.tables[db]
+ def save(self):
+ if self.db_file:
+ with open(self.db_file, 'wb') as f:
+ pickle.dump(self.tables, f, pickle.HIGHEST_PROTOCOL)
+ self.log(None, 'saved database to "%s"' % self.db_file)
+
+ def load(self):
+ if self.db_file and os.path.lexists(self.db_file):
+ with open(self.db_file, 'rb') as f:
+ self.tables = pickle.load(f)
+ self.log(None, 'loaded database from file "%s"' % self.db_file)
+
def handle_set(self, client, key, data):
client.table[key] = data
self.log(client, 'SET %s -> %s' % (key, data))
@@ -141,6 +159,11 @@ self.halt = True
return True
+ def handle_save(self, client):
+ self.save()
+ self.log(client, 'SAVE')
+ return True
+
def unwrap_set(self, client, line):
key, length = line.split()
data = client.rfile.read(int(length))
@@ -279,7 +302,7 @@ elif o == '-l':
logging = True
print 'Launching MiniRedis on %s:%s' % (host, port)
- m = MiniRedis(host=host, port=port, logging=logging)
+ m = MiniRedis(host=host, port=port, logging=logging, db_file='miniredis.db')
m.start()
m.join()
print 'Stopped'
|
Loading...