-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcache.py
executable file
·145 lines (116 loc) · 3.89 KB
/
cache.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Date: 2013-05-10 09:55:54
# @Author: vfasky ([email protected])
# @Version: $Id$
'''
持久化缓存
'''
import time
import asyncmongo
from tornado import gen
__all__ = ['Mongod']
'''
基于 asyncmongo 的异步缓存
============================
## demo:
``` python
mongod = Mongod()
class Handler(RequestHandler):
@asynchronous
@gen.engine
def get(self):
# 写缓存, 缓存有效期,1小时
ret = yield gen.Task(mongod.set, 'test2', {'hello': 'word'}, 3600)
print ret
# 读缓存
data = yield gen.Task(mongod.get, 'test2')
print data
# 删缓存
ret = yield gen.Task(mongod.remove, 'test2')
print ret
```
'''
class Mongod(object):
def __init__(self, **kwargs):
self._conn = asyncmongo.Client(
pool_id = kwargs.get('pool_id', 'xcat.cache.Mongod'),
host = kwargs.get('host', '127.0.0.1'),
port = kwargs.get('port', 27017),
maxcached = kwargs.get('maxcached', 10),
maxconnections = kwargs.get('maxconnections', 50),
dbname = kwargs.get('dbname', 'cache'),
dbuser = kwargs.get('dbuser', None),
dbpass = kwargs.get('dbpass', None)
)
self._table = kwargs.get('table', 'caches')
def get(self, key, default=None, callback=None):
def _callback(data, error):
if error:
raise Error(error)
if data:
last_time = int(data['update_time']) + int(data['left_time'])
if int(data['left_time']) == -1 or int(time.time()) <= last_time:
return callback(data['val'])
else:
self.remove(key)
callback(default)
self._conn[self._table].find_one({'key': key}, callback=_callback)
@gen.engine
def set(self, key, val, left_time=-1, callback=None):
def _callback(data, error):
if error:
raise Error(error)
if callback:
callback(len(data) == 1)
ret, error = yield gen.Task(self._conn[self._table].find_one, {'key': key})
data = ret[0]
if not data or len(data) == 0:
self._conn[self._table].insert({
'key' : key,
'val' : val,
'left_time' : int(left_time),
'update_time' : int(time.time()),
}, callback=_callback)
else:
self._conn[self._table].update({
'_id' : data['_id']
},{
'key' : key,
'val' : val,
'left_time' : int(left_time),
'update_time' : int(time.time()),
}, upsert=True, safe=True, callback=_callback)
def remove(self, key, callback=None):
def _callback(data, error):
if error:
raise Error(error)
if callback:
callback(len(data) == 1)
self._conn[self._table].remove({
'key' : key
}, callback=_callback)
# 测试
if __name__ == '__main__':
from tornado.ioloop import IOLoop
from tornado.httpserver import HTTPServer
#from tornado.options import parse_command_line
from tornado.web import asynchronous, RequestHandler, Application
mongod = Mongod()
class Handler(RequestHandler):
@asynchronous
@gen.engine
def get(self):
ret = yield gen.Task(mongod.set, 'test2', {'hello': 'word'})
print ret
data = yield gen.Task(mongod.get, 'test2')
print data
ret = yield gen.Task(mongod.remove, 'test2')
print ret
self.finish()
application = Application([
(r'/', Handler),
], debug=True)
http_server = HTTPServer(application)
http_server.listen(8181)
IOLoop.instance().start()