Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit daf809c

Browse files
committed
Added shelve cache.
1 parent e059d7c commit daf809c

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

etherscan/etherscan.py

+12
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,13 @@
88
from etherscan.enums.fields_enum import FieldsEnum as fields
99
from etherscan.utils.parsing import ResponseParser as parser
1010

11+
from etherscan.utils.shelve import Shelve as shelve
12+
1113

1214
class Etherscan:
15+
# Enable/disable caching of all requests
16+
CACHING = True
17+
1318
def __new__(cls, api_key: str, net: str = "MAIN"):
1419
with resources.path(configs, f"{net.upper()}-stable.json") as path:
1520
config_path = str(path)
@@ -29,7 +34,14 @@ def wrapper(*args, **kwargs):
2934
f"{fields.API_KEY}"
3035
f"{api_key}"
3136
)
37+
38+
r = shelve.shelve_load(url)
39+
if r and Etherscan.CACHING:
40+
return parser.parse(r)
41+
3242
r = requests.get(url, headers={"User-Agent": ""})
43+
shelve.shelve_store(url, r)
44+
3345
return parser.parse(r)
3446

3547
return wrapper

etherscan/utils/shelve.py

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import shelve
2+
3+
4+
class Shelve:
5+
SHELVE_FILE = r'data/etherscan_shelve'
6+
7+
@staticmethod
8+
def shelve_store(key, data):
9+
# store result in cache
10+
d = shelve.open(Shelve.SHELVE_FILE)
11+
d[key] = data
12+
d.close()
13+
14+
@staticmethod
15+
def shelve_load(key):
16+
d = shelve.open(Shelve.SHELVE_FILE)
17+
try:
18+
data = d[key]
19+
return data
20+
except KeyError as err:
21+
return None
22+
finally:
23+
d.close()
24+

0 commit comments

Comments
 (0)