@@ -25,6 +25,8 @@ def __init__(self,
2525 fallback_domain_list .insert (0 , domain )
2626
2727 self .domain_list = fallback_domain_list
28+ self .CLIENT_CACHE = None
29+ self .enable_cache ()
2830 self .after_init ()
2931
3032 def after_init (self ):
@@ -111,31 +113,59 @@ def debug_topic_request(self):
111113 def before_retry (self , e , kwargs , retry_count , url ):
112114 jm_debug ('req.error' , str (e ))
113115
114- def enable_cache (self , debug = False ):
115- if self .is_cache_enabled ():
116- return
116+ def enable_cache (self ):
117+ # noinspection PyDefaultArgument,PyShadowingBuiltins
118+ def make_key (args , kwds , typed ,
119+ kwd_mark = (object (),),
120+ fasttypes = {int , str },
121+ tuple = tuple , type = type , len = len ):
122+ key = args
123+ if kwds :
124+ key += kwd_mark
125+ for item in kwds .items ():
126+ key += item
127+ if typed :
128+ key += tuple (type (v ) for v in args )
129+ if kwds :
130+ key += tuple (type (v ) for v in kwds .values ())
131+ elif len (key ) == 1 and type (key [0 ]) in fasttypes :
132+ return key [0 ]
133+ return hash (key )
117134
118135 def wrap_func_with_cache (func_name , cache_field_name ):
119136 if hasattr (self , cache_field_name ):
120137 return
121138
122- if sys .version_info > (3 , 9 ):
123- import functools
124- cache = functools .cache
125- else :
126- from functools import lru_cache
127- cache = lru_cache ()
128-
129139 func = getattr (self , func_name )
130- setattr (self , func_name , cache (func ))
140+
141+ def cache_wrapper (* args , ** kwargs ):
142+ cache = self .CLIENT_CACHE
143+
144+ # Equivalent to not enable cache
145+ if cache is None :
146+ return func (* args , ** kwargs )
147+
148+ key = make_key (args , kwargs , False )
149+ sentinel = object () # unique object used to signal cache misses
150+
151+ result = cache .get (key , sentinel )
152+ if result is not sentinel :
153+ return result
154+
155+ result = func (* args , ** kwargs )
156+ cache [key ] = result
157+ return result
158+
159+ setattr (self , func_name , cache_wrapper )
131160
132161 for func_name in self .func_to_cache :
133162 wrap_func_with_cache (func_name , f'__{ func_name } .cache.dict__' )
134163
135- setattr (self , '__enable_cache__' , True )
164+ def set_cache_dict (self , cache_dict : Optional [Dict ]):
165+ self .CLIENT_CACHE = cache_dict
136166
137- def is_cache_enabled (self ) -> bool :
138- return getattr ( self , '__enable_cache__' , False )
167+ def get_cache_dict (self ):
168+ return self . CLIENT_CACHE
139169
140170 def get_domain_list (self ):
141171 return self .domain_list
@@ -635,7 +665,7 @@ class FutureClientProxy(JmcomicClient):
635665 client_key = 'cl_proxy_future'
636666 proxy_methods = ['album_comment' , 'enable_cache' , 'get_domain_list' ,
637667 'get_html_domain' , 'get_html_domain_all' , 'get_jm_image' ,
638- 'is_cache_enabled ' , 'set_domain_list' , ]
668+ 'set_cache_dict' , 'get_cache_dict ' , 'set_domain_list' , ]
639669
640670 class FutureWrapper :
641671 def __init__ (self , future ):
0 commit comments