-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathutil.py
More file actions
66 lines (51 loc) · 1.4 KB
/
util.py
File metadata and controls
66 lines (51 loc) · 1.4 KB
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
# coding: utf-8
"""Miscellaneous utility functions and classes.
"""
from tornado.util import Configurable
if type('') is not type(b''):
def u(s):
return s
bytes_type = bytes
unicode_type = str
basestring_type = str
else:
def u(s):
return s.decode('unicode_escape')
bytes_type = str
unicode_type = unicode
basestring_type = basestring
class Cache(Configurable):
""" 缓存部分数据的缓存. 例如用来维护登录的 Session, 避免一次登录请求
可以实现使用内存的缓存(MemCache), 基于Redis的缓存(TODO)
"""
@classmethod
def configurable_base(cls):
return Cache
@classmethod
def configurable_default(cls):
return MemCache
def initialize(self):
pass
def get(self, key):
""" 依据 key 获取内容
"""
raise NotImplementedError()
def set(self, key, value):
""" 设置 key 对应值为value
"""
raise NotImplementedError()
def remove(self, key):
""" 使 key 失效
"""
raise NotImplementedError()
class MemCache(Cache):
def __init__(self):
self._dict = dict()
def get(self, key):
return self._dict.get(key, None)
def set(self, key, value):
self._dict[key] = value
return self
def remove(self, key):
if key in self._dict:
del self._dict[key]