forked from FrancescoGuarneri/AzLyricsAPI
-
Notifications
You must be signed in to change notification settings - Fork 3
/
cache.py
33 lines (25 loc) · 923 Bytes
/
cache.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
import os
from shutil import rmtree
class Cache(object):
def __init__(self, CACHEDIR=None):
if CACHEDIR is None:
self.CACHEDIR = os.path.join(os.path.expanduser("~"), ".cache", "azlyrics")
if not os.path.exists(self.CACHEDIR):
os.makedirs(self.CACHEDIR)
def key_path(self, key):
return os.path.join(self.CACHEDIR, key)
def add(self, key, content):
key_path = self.key_path(key)
with open(key_path, "w") as f:
f.write(content)
def exists(self, key):
key_path = self.key_path(key)
return os.path.exists(key_path)
def get(self, key):
if (self.exists(key)):
key_path = self.key_path(key)
with open(key_path, "r") as f:
return f.read()
def destroy(self):
if os.path.exists(self.CACHEDIR):
rmtree(self.CACHEDIR)