1111import functools
1212import datetime as datetime_module
1313
14+ from python_toolbox import misc_tools
1415from python_toolbox import binary_search
1516from python_toolbox import decorator_tools
1617from python_toolbox .sleek_reffing import SleekCallArgs
@@ -93,7 +94,6 @@ def decorator(function):
9394
9495 if time_to_keep :
9596
96- cache_dict = OrderedDict ()
9797 sorting_key_function = lambda sleek_call_args : \
9898 cached ._cache [sleek_call_args ][1 ]
9999
@@ -111,11 +111,11 @@ def remove_expired_entries():
111111 for key in list (cached ._cache .keys ())[:cutting_point ]:
112112 del cached ._cache [key ]
113113
114-
114+ @ misc_tools . set_attributes ( _cache = OrderedDict ())
115115 def cached (function , * args , ** kwargs ):
116116 remove_expired_entries ()
117117 sleek_call_args = \
118- SleekCallArgs (cache_dict , function , * args , ** kwargs )
118+ SleekCallArgs (cached . _cache , function , * args , ** kwargs )
119119 try :
120120 return cached ._cache [sleek_call_args ][0 ]
121121 except KeyError :
@@ -129,11 +129,10 @@ def cached(function, *args, **kwargs):
129129
130130 else : # not time_to_keep
131131
132- cache_dict = {}
133-
132+ @misc_tools .set_attributes (_cache = {})
134133 def cached (function , * args , ** kwargs ):
135134 sleek_call_args = \
136- SleekCallArgs (cache_dict , function , * args , ** kwargs )
135+ SleekCallArgs (cached . _cache , function , * args , ** kwargs )
137136 try :
138137 return cached ._cache [sleek_call_args ]
139138 except KeyError :
@@ -143,11 +142,10 @@ def cached(function, *args, **kwargs):
143142
144143 else : # max_size < infinity
145144
146- cache_dict = OrderedDict ()
147-
145+ @misc_tools .set_attributes (_cache = OrderedDict ())
148146 def cached (function , * args , ** kwargs ):
149147 sleek_call_args = \
150- SleekCallArgs (cache_dict , function , * args , ** kwargs )
148+ SleekCallArgs (cached . _cache , function , * args , ** kwargs )
151149 try :
152150 result = cached ._cache [sleek_call_args ]
153151 cached ._cache .move_to_end (sleek_call_args )
@@ -159,7 +157,6 @@ def cached(function, *args, **kwargs):
159157 cached ._cache .popitem (last = False )
160158 return value
161159
162- cached ._cache = cache_dict
163160
164161 result = decorator_tools .decorator (cached , function )
165162
0 commit comments