-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathcache_manager.py
More file actions
34 lines (27 loc) · 858 Bytes
/
cache_manager.py
File metadata and controls
34 lines (27 loc) · 858 Bytes
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 os import path
# Operates with book pages cache
class CacheManager:
def __init__(this, cache_dir: str):
this.cache_dir = cache_dir
this.ensure_cache_dir()
def ensure_cache_dir(this):
if not path.exists(this.cache_dir):
os.mkdir(this.cache_dir)
def get_path(this, id: str):
return path.join(this.cache_dir, id + '.html')
def is_cached(this, id: str):
return path.exists(this.get_path(id))
def save(this, id: str, content: str):
file_name = this.get_path(id)
with open(file_name, 'wb') as file:
print('Save to cache: "%s"' % file_name)
file.write(content)
def load(this, id: str):
file_name = this.get_path(id)
try:
with open(file_name, 'r', encoding="utf-8") as file:
return file.read()
except Exception as ex:
print('load_book_content_from_cache("%s"): %s' % (file_name, ex))
return None