Skip to content

Commit 28ca8a8

Browse files
authored
Use custom cache over FSCache if specified (#1285)
* Use custom cache over FSCache if specified Currently, if both `useFSCache` and `useCustomCache` env options are set to true (perhaps because `useFSCache` is true by default and the user forgot to unset it), the code defaults to instantiating a FileCache instead of using the custom cache implementation passed in by the user. This commit changes the default to be the custom cache if specified. * Use custom cache over browser cache if specified If `useCustomCache` is set, try to use the custom cache before falling back to the browser or the fs caches. * Throw an error if fs is not available for FSCache Meant to handle cases where the user sets `useFSCache` from an environment that doesn't support `fs`.
1 parent c536857 commit 28ca8a8

File tree

1 file changed

+19
-17
lines changed

1 file changed

+19
-17
lines changed

src/utils/hub.js

+19-17
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,22 @@ export async function getModelFile(path_or_repo_id, filename, fatal = true, opti
418418
// First, check if the a caching backend is available
419419
// If no caching mechanism available, will download the file every time
420420
let cache;
421+
if (!cache && env.useCustomCache) {
422+
// Allow the user to specify a custom cache system.
423+
if (!env.customCache) {
424+
throw Error('`env.useCustomCache=true`, but `env.customCache` is not defined.')
425+
}
426+
427+
// Check that the required methods are defined:
428+
if (!env.customCache.match || !env.customCache.put) {
429+
throw new Error(
430+
"`env.customCache` must be an object which implements the `match` and `put` functions of the Web Cache API. " +
431+
"For more information, see https://developer.mozilla.org/en-US/docs/Web/API/Cache"
432+
)
433+
}
434+
cache = env.customCache;
435+
}
436+
421437
if (!cache && env.useBrowserCache) {
422438
if (typeof caches === 'undefined') {
423439
throw Error('Browser cache is not available in this environment.')
@@ -435,28 +451,14 @@ export async function getModelFile(path_or_repo_id, filename, fatal = true, opti
435451
}
436452

437453
if (!cache && env.useFSCache) {
438-
// TODO throw error if not available
454+
if (!apis.IS_FS_AVAILABLE) {
455+
throw Error('File System Cache is not available in this environment.');
456+
}
439457

440458
// If `cache_dir` is not specified, use the default cache directory
441459
cache = new FileCache(options.cache_dir ?? env.cacheDir);
442460
}
443461

444-
if (!cache && env.useCustomCache) {
445-
// Allow the user to specify a custom cache system.
446-
if (!env.customCache) {
447-
throw Error('`env.useCustomCache=true`, but `env.customCache` is not defined.')
448-
}
449-
450-
// Check that the required methods are defined:
451-
if (!env.customCache.match || !env.customCache.put) {
452-
throw new Error(
453-
"`env.customCache` must be an object which implements the `match` and `put` functions of the Web Cache API. " +
454-
"For more information, see https://developer.mozilla.org/en-US/docs/Web/API/Cache"
455-
)
456-
}
457-
cache = env.customCache;
458-
}
459-
460462
const revision = options.revision ?? 'main';
461463
const requestURL = pathJoin(path_or_repo_id, filename);
462464

0 commit comments

Comments
 (0)