@@ -11,16 +11,22 @@ def get_timestamp_in_ms() -> float:
11
11
12
12
class S3Manager :
13
13
def __init__ (
14
- self , client : tp .Any , bucket_name : str , check_ttl_every : tp .Union [int , float ], is_binary : bool = False
14
+ self ,
15
+ client : tp .Any ,
16
+ bucket_name : str ,
17
+ check_ttl_every : tp .Union [int , float ],
18
+ is_binary : bool = False ,
19
+ path_prefix : str = "hishel-" ,
15
20
):
16
21
self ._client = client
17
22
self ._bucket_name = bucket_name
18
23
self ._is_binary = is_binary
19
24
self ._last_cleaned = time .monotonic ()
20
25
self ._check_ttl_every = check_ttl_every
26
+ self ._path_prefix = path_prefix
21
27
22
28
def write_to (self , path : str , data : tp .Union [bytes , str ], only_metadata : bool = False ) -> None :
23
- path = "hishel-" + path
29
+ path = self . _path_prefix + path
24
30
if isinstance (data , str ):
25
31
data = data .encode ("utf-8" )
26
32
@@ -43,7 +49,7 @@ def write_to(self, path: str, data: tp.Union[bytes, str], only_metadata: bool =
43
49
)
44
50
45
51
def read_from (self , path : str ) -> tp .Union [bytes , str ]:
46
- path = "hishel-" + path
52
+ path = self . _path_prefix + path
47
53
response = self ._client .get_object (
48
54
Bucket = self ._bucket_name ,
49
55
Key = path ,
@@ -57,7 +63,7 @@ def read_from(self, path: str) -> tp.Union[bytes, str]:
57
63
return tp .cast (str , content .decode ("utf-8" ))
58
64
59
65
def remove_expired (self , ttl : int , key : str ) -> None :
60
- path = "hishel-" + key
66
+ path = self . _path_prefix + key
61
67
62
68
if time .monotonic () - self ._last_cleaned < self ._check_ttl_every :
63
69
try :
@@ -72,7 +78,7 @@ def remove_expired(self, ttl: int, key: str) -> None:
72
78
73
79
self ._last_cleaned = time .monotonic ()
74
80
for obj in self ._client .list_objects (Bucket = self ._bucket_name ).get ("Contents" , []):
75
- if not obj ["Key" ].startswith ("hishel-" ): # pragma: no cover
81
+ if not obj ["Key" ].startswith (self . _path_prefix ): # pragma: no cover
76
82
continue
77
83
78
84
try :
@@ -88,15 +94,20 @@ def remove_expired(self, ttl: int, key: str) -> None:
88
94
self ._client .delete_object (Bucket = self ._bucket_name , Key = obj ["Key" ])
89
95
90
96
def remove_entry (self , key : str ) -> None :
91
- path = "hishel-" + key
97
+ path = self . _path_prefix + key
92
98
self ._client .delete_object (Bucket = self ._bucket_name , Key = path )
93
99
94
100
95
101
class AsyncS3Manager : # pragma: no cover
96
102
def __init__ (
97
- self , client : tp .Any , bucket_name : str , check_ttl_every : tp .Union [int , float ], is_binary : bool = False
103
+ self ,
104
+ client : tp .Any ,
105
+ bucket_name : str ,
106
+ check_ttl_every : tp .Union [int , float ],
107
+ is_binary : bool = False ,
108
+ path_prefix : str = "hishel-" ,
98
109
):
99
- self ._sync_manager = S3Manager (client , bucket_name , check_ttl_every , is_binary )
110
+ self ._sync_manager = S3Manager (client , bucket_name , check_ttl_every , is_binary , path_prefix )
100
111
101
112
async def write_to (self , path : str , data : tp .Union [bytes , str ], only_metadata : bool = False ) -> None :
102
113
return await to_thread .run_sync (self ._sync_manager .write_to , path , data , only_metadata )
0 commit comments