evijit HF Staff Claude Opus 4.7 (1M context) commited on
Commit
dc95237
·
1 Parent(s): 40339dc

Prefer /data persistent bucket for sidecar cache when available

Browse files

The Space now has a persistent storage bucket mounted at /data. Default
the sidecar cache there (when writable) so the cache survives container
rebuilds, not just restarts within a single container's lifetime. Falls
back to <tmpdir>/eval-card-sidecars otherwise. SIDECAR_CACHE_DIR still
overrides everything.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

Files changed (1) hide show
  1. lib/sidecars.ts +21 -5
lib/sidecars.ts CHANGED
@@ -1,6 +1,7 @@
1
  import "server-only"
2
 
3
  import { createHash } from "node:crypto"
 
4
  import { mkdir, readFile, stat, writeFile } from "node:fs/promises"
5
  import { tmpdir } from "node:os"
6
  import { join } from "node:path"
@@ -39,11 +40,26 @@ function sidecarUrl(name: string) {
39
  // built-in fetch cache rejects items over 2 MB so the 47 MB
40
  // comparison-index / 6 MB peer-ranks / 2.5 MB hierarchy were re-fetched
41
  // from HuggingFace on every cold start. With the disk cache, a warm
42
- // container reads from `/tmp/eval-card-sidecars/*` (sub-second) instead
43
- // of re-downloading. Override the directory via `SIDECAR_CACHE_DIR` and
44
- // the TTL via `SIDECAR_CACHE_TTL_SECONDS`.
45
- const DISK_CACHE_DIR =
46
- process.env.SIDECAR_CACHE_DIR?.trim() || join(tmpdir(), "eval-card-sidecars")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47
  const DISK_CACHE_TTL_MS =
48
  Number.parseInt(process.env.SIDECAR_CACHE_TTL_SECONDS ?? "3600", 10) * 1000
49
 
 
1
  import "server-only"
2
 
3
  import { createHash } from "node:crypto"
4
+ import { accessSync, constants as fsConstants } from "node:fs"
5
  import { mkdir, readFile, stat, writeFile } from "node:fs/promises"
6
  import { tmpdir } from "node:os"
7
  import { join } from "node:path"
 
40
  // built-in fetch cache rejects items over 2 MB so the 47 MB
41
  // comparison-index / 6 MB peer-ranks / 2.5 MB hierarchy were re-fetched
42
  // from HuggingFace on every cold start. With the disk cache, a warm
43
+ // container reads from disk (sub-second) instead of re-downloading.
44
+ //
45
+ // Resolution order:
46
+ // 1. `SIDECAR_CACHE_DIR` env var (explicit override)
47
+ // 2. `/data/sidecars` when `/data` is writable — the HF Space mounts a
48
+ // persistent storage bucket there, so the cache survives container
49
+ // rebuilds (not just restarts within one container).
50
+ // 3. `<tmpdir>/eval-card-sidecars` as the local-dev / no-bucket fallback.
51
+ function resolveDiskCacheDir(): string {
52
+ const explicit = process.env.SIDECAR_CACHE_DIR?.trim()
53
+ if (explicit) return explicit
54
+ try {
55
+ accessSync("/data", fsConstants.W_OK)
56
+ return "/data/sidecars"
57
+ } catch {
58
+ return join(tmpdir(), "eval-card-sidecars")
59
+ }
60
+ }
61
+
62
+ const DISK_CACHE_DIR = resolveDiskCacheDir()
63
  const DISK_CACHE_TTL_MS =
64
  Number.parseInt(process.env.SIDECAR_CACHE_TTL_SECONDS ?? "3600", 10) * 1000
65