Skip to content

Commit 2b270ac

Browse files
committed
[16.0][BKP] fs_storage: Makes the cache policy of fsspec client configurable
1 parent 566497e commit 2b270ac

File tree

2 files changed

+40
-4
lines changed

2 files changed

+40
-4
lines changed

fs_storage/models/fs_storage.py

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,16 @@ def __init__(self, env, ids=(), prefetch_ids=()):
157157
"* List File : List all files from root directory",
158158
)
159159

160+
is_cacheable = fields.Boolean(
161+
help="If True, once instantiated, the filesystem will be cached and reused.\n"
162+
"By default, the filesystem is cacheable but in some cases, like "
163+
"when using OAuth2 authentication, the filesystem cannot be cached "
164+
"because it depends on the user and the token can change.\n"
165+
"In this case, you can set this field to False to avoid caching the "
166+
"filesystem.",
167+
default=True,
168+
)
169+
160170
_sql_constraints = [
161171
(
162172
"code_uniq",
@@ -208,6 +218,23 @@ def get_by_code(self, code) -> FSStorage:
208218
res = self.browse(res_id)
209219
return res
210220

221+
@api.model
222+
@tools.ormcache("code")
223+
def get_protocol_by_code(self, code):
224+
record = self.get_by_code(code)
225+
return record.protocol if record else None
226+
227+
@api.model
228+
@tools.ormcache("code")
229+
def _is_fs_cacheable(self, code):
230+
"""Return True if the filesystem is cacheable."""
231+
# This method is used to check if the filesystem is cacheable.
232+
# It is used to avoid caching filesystems that are not cacheable.
233+
# For example, the msgd protocol is not cacheable because it uses
234+
# OAuth2 authentication and the token can change.
235+
fs_storage = self.get_by_code(code)
236+
return fs_storage and fs_storage.is_cacheable
237+
211238
@api.model
212239
@tools.ormcache()
213240
def get_storage_codes(self):
@@ -216,15 +243,23 @@ def get_storage_codes(self):
216243

217244
@api.model
218245
@tools.ormcache("code")
219-
def get_fs_by_code(self, code):
246+
def _get_fs_by_code_from_cache(self, code):
247+
return self.get_fs_by_code(code, force_no_cache=True)
248+
249+
@api.model
250+
def get_fs_by_code(self, code, force_no_cache=False):
220251
"""Return the filesystem associated to the given code.
221252
222253
:param code: the code of the filesystem
223254
"""
255+
use_cache = not force_no_cache and self._is_fs_cacheable(code)
224256
fs = None
225-
fs_storage = self.get_by_code(code)
226-
if fs_storage:
227-
fs = fs_storage.fs
257+
if use_cache:
258+
fs = self._get_fs_by_code_from_cache(code)
259+
else:
260+
fs_storage = self.get_by_code(code)
261+
if fs_storage:
262+
fs = fs_storage.fs
228263
return fs
229264

230265
def copy(self, default=None):

fs_storage/views/fs_storage_view.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
<group>
3535
<field name="code" />
3636
<field name="protocol" />
37+
<field name="is_cacheable" />
3738
<field name="directory_path" />
3839
<field name="eval_options_from_env" />
3940
<field

0 commit comments

Comments
 (0)