-
Notifications
You must be signed in to change notification settings - Fork 344
/
requests_cache_compatability.py
52 lines (42 loc) · 1.89 KB
/
requests_cache_compatability.py
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
# -*- coding: utf-8 -*-
'''
Patches older versions of requests_cache with missing expire
functionality and an updated create_key which excludes most
HEADERS
This module must be imported before any modules use requests_cache
explicitly or implicitly
'''
import requests_cache
from datetime import datetime, timedelta
rc_version_info = tuple(int(x) for x in requests_cache.__version__.split('.'))
# patch if required if older versions than '0.5.0'
# request_cache.core is deprecated in '0.6.0' and removed on '0.8.0'.
if rc_version_info < (0, 5, 0):
try:
requests_cache.backends.base.BaseCache.remove_old_entries
except Exception as e:
def remove_old_entries(self, created_before):
""" Deletes entries from cache with creation time older than ``created_before``
"""
keys_to_delete = set()
for key in self.responses:
try:
response, created_at = self.responses[key]
except KeyError:
continue
if created_at < created_before:
keys_to_delete.add(key)
for key in keys_to_delete:
self.delete(key)
def remove_expired_responses(self):
""" Removes expired responses from storage
"""
if not self._cache_expire_after:
return
# just in case expire_after is not converted in the
# original constructor, convert it to a timedelta
if not isinstance(self._cache_expire_after, timedelta):
self._cache_expire_after = timedelta(seconds=self._cache_expire_after)
self.cache.remove_old_entries(datetime.utcnow() - self._cache_expire_after)
requests_cache.backends.base.BaseCache.remove_old_entries = remove_old_entries
requests_cache.core.CachedSession.remove_expired_responses = remove_expired_responses