forked from ricerati/proxy-checker-python
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFileCache.py
More file actions
73 lines (57 loc) · 2 KB
/
FileCache.py
File metadata and controls
73 lines (57 loc) · 2 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import json
import os
import time
from typing import Any, Optional
class FileCache:
"""
A simple file-based cache system with expiration.
Attributes:
file_path (str): Path to the cache file.
Methods:
write_cache(value: Any, expires_in: int) -> None:
Writes a value to the cache with an expiration time.
read_cache() -> Optional[Any]:
Reads the value from the cache if it has not expired.
"""
def __init__(self, file_path: str) -> None:
"""
Initializes the FileCache with a file path.
Args:
file_path (str): The path to the cache file.
"""
self.file_path = file_path
def write_cache(self, value: Any, expires_in: int) -> None:
"""
Writes a value to the cache with an expiration time.
Args:
value (Any): The value to be cached.
expires_in (int): The expiration time in seconds.
"""
# Ensure the parent directory exists
os.makedirs(os.path.dirname(self.file_path), exist_ok=True)
cache_data = {
"value": value,
"timestamp": time.time(),
"expires_in": expires_in,
}
with open(self.file_path, "w") as cache_file:
json.dump(cache_data, cache_file)
def read_cache(self) -> Optional[Any]:
"""
Reads the value from the cache if it has not expired.
Returns:
Optional[Any]: The cached value if valid, otherwise None.
"""
if not os.path.exists(self.file_path):
return None
try:
with open(self.file_path, "r") as cache_file:
cache_data = json.load(cache_file)
current_time = time.time()
cache_time = cache_data["timestamp"]
expires_in = cache_data["expires_in"]
if current_time - cache_time > expires_in:
return None
return cache_data["value"]
except Exception:
return None