-
Notifications
You must be signed in to change notification settings - Fork 131
Expand file tree
/
Copy pathstorage.py
More file actions
187 lines (150 loc) · 4.61 KB
/
storage.py
File metadata and controls
187 lines (150 loc) · 4.61 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
"""Storage implementation using diskcache."""
from collections.abc import Iterator
from functools import partial
from pathlib import Path
from typing import Any
import diskcache
Cache = partial(diskcache.Cache, size_limit=float("inf"), cull_limit=0, eviction=None)
class DirectoryDoesNotExist(Exception):
"""Directory does not exist."""
class DiskCacheStorage:
"""
Storage implementation using diskcache.
This class provides a persistent storage solution using the diskcache library,
which allows for efficient caching of data on disk.
Parameters
----------
directory : Path
The directory path where the cache will be stored.
Attributes
----------
storage : Cache
The underlying diskcache Cache object.
"""
def __init__(self, directory: Path):
"""
Initialize the DiskCacheStorage with the specified directory.
Parameters
----------
directory : Path
The directory path where the cache will be stored.
"""
if not directory.exists():
raise DirectoryDoesNotExist(f"Directory {directory} does not exist.")
self.directory = directory
def __contains__(self, key: str) -> bool:
"""
Return True if the storage has the specified key, else False.
Parameters
----------
key : str
The key to check for existence in the storage.
Returns
-------
bool
True if the key is in the storage, else False.
"""
return key in self.keys()
def __len__(self) -> int:
"""
Return the number of items in the storage.
Returns
-------
int
The number of items in the storage.
"""
return len(list(self.keys()))
def __iter__(self) -> Iterator[str]:
"""
Yield the keys in the storage in insertion order.
Returns
-------
Iterator[str]
An iterator yielding all keys in the storage.
"""
return self.keys()
def __getitem__(self, key: str) -> Any:
"""
Retrieve an item from the storage.
Parameters
----------
key : str
The key of the item to retrieve.
Returns
-------
Any
The value associated with the given key.
Raises
------
KeyError
If the key is not found in the storage.
"""
with Cache(self.directory) as storage:
return storage[key]
def __setitem__(self, key: str, value: Any) -> None:
"""
Set an item in the storage.
Parameters
----------
key : str
The key to associate with the value.
value : Any
The value to store.
"""
with Cache(self.directory) as storage:
storage[key] = value
def __delitem__(self, key: str) -> None:
"""
Delete an item from the storage.
Parameters
----------
key : str
The key of the item to delete.
Raises
------
KeyError
If the key is not found in the storage.
"""
with Cache(self.directory) as storage:
del storage[key]
def keys(self) -> Iterator[str]:
"""
Get an iterator over the keys in the storage in insertion order.
Returns
-------
Iterator[str]
An iterator yielding all keys in the storage.
"""
with Cache(self.directory) as storage:
yield from storage
def values(self) -> Iterator[Any]:
"""
Get an iterator over the values in the storage in insertion order.
Returns
-------
Iterator[Any]
An iterator yielding all values in the storage.
"""
with Cache(self.directory) as storage:
for key in storage:
yield storage[key]
def items(self) -> Iterator[tuple[str, Any]]:
"""
Get an iterator over the (key, value) pairs in the storage in insertion order.
Returns
-------
Iterator[tuple[str, Any]]
An iterator yielding all (key, value) pairs in the storage.
"""
with Cache(self.directory) as storage:
for key in storage:
yield (key, storage[key])
def __repr__(self) -> str:
"""
Return a string representation of the storage.
Returns
-------
str
A string representation of the storage.
"""
return f"DiskCacheStorage(directory='{self.directory}')"