Browse code
switching from mongodb to redis
Ed L authored on 13/06/2014 21:17:10
Showing 3 changed files
Showing 3 changed files
... | ... |
@@ -13,70 +13,17 @@ import random |
13 | 13 |
import json |
14 | 14 |
|
15 | 15 |
import text_server |
16 |
+import datastore |
|
16 | 17 |
|
17 |
-class DonutResource(twisted.web.resource.Resource): |
|
18 |
- |
|
19 |
- def __init__(self): |
|
20 |
- twisted.web.resource.Resource.__init__(self) |
|
21 |
- self.data_store = [] |
|
22 |
- |
|
23 |
- isLeaf = True |
|
24 |
- |
|
25 |
- def render_POST(self, request): |
|
26 |
- print self.data_store |
|
27 |
- data_store = self.data_store |
|
28 |
- data_store.append(json.loads(request.content.read())) |
|
29 |
- _id = len(data_store)-1 |
|
30 |
- data_store[-1]['id'] = _id |
|
31 |
- return json.dumps(dict(id=len(data_store)-1)) |
|
32 |
- |
|
33 |
- def render_PUT(self, request): |
|
34 |
- data_store = self.data_store |
|
35 |
- new = json.load(request.content) |
|
36 |
- print new |
|
37 |
- if new['id'] < len(data_store): |
|
38 |
- data_store[new['id']] = new |
|
39 |
- return '' |
|
40 |
- |
|
41 |
- def render_GET(self, request): |
|
42 |
- if not request.postpath: |
|
43 |
- return json.dumps(self.data_store) |
|
44 |
- |
|
45 |
- idx = request.postpath[0] |
|
46 |
- idx = int(idx) |
|
47 |
- return json.dumps(self.data_store[idx]) |
|
48 |
- |
|
49 |
-class DonutShopListResource(twisted.web.resource.Resource): |
|
50 |
- donut_shops = dict( |
|
51 |
- a = DonutResource(), |
|
52 |
- b = DonutResource(), |
|
53 |
- c = DonutResource(), |
|
54 |
- ) |
|
55 |
- |
|
56 |
- isLeaf = False |
|
57 |
- |
|
58 |
- def getChild(self, path, request): |
|
59 |
- print path |
|
60 |
- |
|
61 |
- if path in self.donut_shops: |
|
62 |
- result = self.donut_shops.get(path) |
|
63 |
- return result |
|
64 |
- |
|
65 |
- return twisted.web.resource.Resource.getChild(self, path, request) |
|
66 |
- |
|
67 |
- |
|
68 |
- |
|
69 |
-class TestResource(twisted.web.resource.Resource): |
|
18 |
+class TextResource(twisted.web.resource.Resource): |
|
70 | 19 |
lqueue = collections.deque() |
71 | 20 |
nqueue = collections.deque() |
72 | 21 |
|
73 | 22 |
isLeaf = False |
74 | 23 |
|
75 | 24 |
def __init__(self): |
76 |
- from pymongo import MongoClient |
|
77 |
- mc = MongoClient() |
|
78 |
- db = mc.text_server |
|
79 |
- collection = db.texts |
|
25 |
+ import redis |
|
26 |
+ collection = datastore.RedisDataStore(redis.Redis()) |
|
80 | 27 |
|
81 | 28 |
twisted.web.resource.Resource.__init__(self) |
82 | 29 |
self.putChild('html', static.File('html')) |
83 | 30 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,18 @@ |
1 |
+import json |
|
2 |
+class RedisDataStore(object): |
|
3 |
+ key = 0 |
|
4 |
+ def __init__(self, connection): |
|
5 |
+ self.db = connection |
|
6 |
+ |
|
7 |
+ rkey = '%05d__BOOK%%s' |
|
8 |
+ def insert(self, dct): |
|
9 |
+ self.key += 1 |
|
10 |
+ rkey = self.rkey % self.key |
|
11 |
+ self.db.set(rkey % '', 1) |
|
12 |
+ self.db.set(rkey % '__TITLE', dct['title']) |
|
13 |
+ self.db.set(rkey % '__CONTENT', json.dumps(dct['content'])) |
|
14 |
+ |
|
15 |
+ def find(self, key): |
|
16 |
+ rkey = self.rkey % self.key |
|
17 |
+ if self.db.get(rkey % '') is not None: |
|
18 |
+ return self.db.get(rkey % '__TITLE'), self.db.get(rkey % '__CONTENT') |
... | ... |
@@ -22,6 +22,7 @@ def load_data(fil): |
22 | 22 |
a,b = chunk.split('--') |
23 | 23 |
a = filter(None, [x.strip() for x in a.split('\n\n')]) |
24 | 24 |
b = filter(None, [x.strip() for x in b.split('\n\n')]) |
25 |
+ print b[0], type(b[0]) |
|
25 | 26 |
|
26 | 27 |
name = '' |
27 | 28 |
if a[0].startswith('#'): |
... | ... |
@@ -45,8 +46,10 @@ class ChunkResource(twisted.web.resource.Resource): |
45 | 46 |
|
46 | 47 |
def render_GET(self, request): |
47 | 48 |
idx = request.postpath[0] |
48 |
- data = self.data_store.find(_id=idx)[0] |
|
49 |
- title, cont = data['title'], data['content'] |
|
50 |
- result = u'var text_title = "%s", objs = %s' % (title, json.dumps(cont)) |
|
49 |
+ data = self.data_store.find(idx) |
|
50 |
+ title, cont = data |
|
51 |
+ title = title.decode('utf-8') |
|
52 |
+ cont = cont.decode('utf-8') |
|
53 |
+ result = u'var text_title = "%s", objs = %s' % (title, cont) |
|
51 | 54 |
return bytes(result.encode('utf-8')) |
52 | 55 |
|