@@ -116,15 +116,15 @@ def request_file(self, cache_location):
116
116
fn = os .path .join (cache_location , self .hash () + ".pickle" )
117
117
return fn
118
118
119
- def from_cache (self , cache_location ):
119
+ def from_cache (self , cache_location , cache_timeout ):
120
120
request_file = self .request_file (cache_location )
121
121
try :
122
- if conf . default_cache_timeout is None :
122
+ if cache_timeout is None :
123
123
expired = False
124
124
else :
125
125
current_time = datetime .utcnow ()
126
126
cache_time = datetime .utcfromtimestamp (os .path .getmtime (request_file ))
127
- expired = ((current_time - cache_time ) > timedelta (seconds = conf . default_cache_timeout ))
127
+ expired = ((current_time - cache_time ) > timedelta (seconds = cache_timeout ))
128
128
if not expired :
129
129
with open (request_file , "rb" ) as f :
130
130
response = pickle .load (f )
@@ -201,6 +201,8 @@ def __init__(self):
201
201
202
202
self .name = self .__class__ .__name__ .split ("Class" )[0 ]
203
203
self ._cache_active = conf .use_cache
204
+ self .use_cache = conf .use_cache
205
+ self .cache_timeout = conf .default_cache_timeout
204
206
205
207
206
208
def __call__ (self , * args , ** kwargs ):
@@ -240,6 +242,23 @@ def _response_hook(self, response, *args, **kwargs):
240
242
f"{ response .text } \n "
241
243
f"-----------------------------------------" , '\t ' )
242
244
log .log (5 , f"HTTP response\n { response_log } " )
245
+
246
+ def clear_cache ():
247
+ """Removes all cache files."""
248
+
249
+ cache_files = [x for x in os .listdir (self .cache_location ) if x .endswidth ("pickle" )]
250
+ for fle in cache_files :
251
+ os .remove (fle )
252
+
253
+ def reset_cache_preferences ():
254
+ """Resets cache preferences to default values"""
255
+
256
+ self .cache_location = os .path .join (
257
+ conf .cache_location ,
258
+ self .__class__ .__name__ .split ("Class" )[0 ])
259
+
260
+ self .use_cache = conf .use_cache
261
+ self .cache_timeout = conf .default_cache_timeout
243
262
244
263
def _request (self , method , url ,
245
264
params = None , data = None , headers = None ,
@@ -310,13 +329,10 @@ def _request(self, method, url,
310
329
timeout = timeout ,
311
330
json = json
312
331
)
313
-
314
- # Set up cache
315
- if (cache is True ) or ((cache is not False ) and conf .use_cache ):
316
- cache_location = os .path .join (conf .cache_location , self .name )
332
+
333
+ if (cache is not False ) and self .use_cache :
317
334
cache = True
318
335
else :
319
- cache_location = None
320
336
cache = False
321
337
322
338
if save :
@@ -345,10 +361,10 @@ def _request(self, method, url,
345
361
allow_redirects = allow_redirects ,
346
362
json = json )
347
363
else :
348
- response = query .from_cache (cache_location )
364
+ response = query .from_cache (self . cache_location , self . cache_timeout )
349
365
if not response :
350
366
response = query .request (self ._session ,
351
- cache_location ,
367
+ self . cache_location ,
352
368
stream = stream ,
353
369
auth = auth ,
354
370
allow_redirects = allow_redirects ,
0 commit comments