5
5
import logging
6
6
import threading
7
7
import time
8
+ import msgpack
8
9
9
10
logger = logging .getLogger (__name__ )
10
11
_lock_mutex_update = threading .RLock ()
@@ -95,10 +96,11 @@ def _cleanup(self):
95
96
class Cacheable (object ):
96
97
97
98
def __init__ (self , capacity = 1024 * 4 , timeout = 60 ,
98
- is_deepcopy = True , mutex_update = False ):
99
+ is_deepcopy = True , is_pack = False , mutex_update = False ):
99
100
100
101
self .lru = LRU (capacity , timeout )
101
102
self .is_deepcopy = is_deepcopy
103
+ self .is_pack = is_pack
102
104
self .mutex_update = mutex_update
103
105
104
106
def _arg_str (self , args , argkv ):
@@ -124,11 +126,18 @@ def func_wrapper(*args, **argkv):
124
126
val = self .lru [generate_key ]
125
127
except KeyError :
126
128
val = fun (* args , ** argkv )
129
+ if self .is_pack :
130
+ val = msgpack .packb (val , use_bin_type = True )
127
131
self .lru [generate_key ] = val
128
132
else :
129
133
val = fun (* args , ** argkv )
134
+ if self .is_pack :
135
+ val = msgpack .packb (val , use_bin_type = True )
130
136
self .lru [generate_key ] = val
131
137
138
+ if self .is_pack :
139
+ val = msgpack .unpackb (val , raw = False )
140
+
132
141
if self .is_deepcopy :
133
142
return copy .deepcopy (val )
134
143
else :
@@ -140,7 +149,8 @@ def func_wrapper(*args, **argkv):
140
149
caches = {}
141
150
142
151
143
- def cache (name , capacity = 1024 * 4 , timeout = 60 , is_deepcopy = True , mutex_update = False ):
152
+ def cache (name , capacity = 1024 * 4 , timeout = 60 ,
153
+ is_deepcopy = True , is_pack = False , mutex_update = False ):
144
154
145
155
c = caches .get (name )
146
156
if c is None :
0 commit comments