-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathcache_helpers.go
More file actions
55 lines (44 loc) · 1.14 KB
/
cache_helpers.go
File metadata and controls
55 lines (44 loc) · 1.14 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
// Copyright (c) Meta Platforms, Inc. and affiliates.
// All rights reserved.
package shelper
import (
"encoding/json"
"os"
"path/filepath"
"time"
)
// GetSlurmMetadataCache returns the cached result if it exists and is still valid, otherwise returns an empty string
func GetSlurmMetadataCache(cacheFilepath string, cacheDuration int) (string, error) {
if _, err := os.Stat(cacheFilepath); os.IsNotExist(err) {
return "", nil
}
fileMetadata, err := os.Stat(cacheFilepath)
if err != nil {
return "", err
}
if time.Since(fileMetadata.ModTime()).Seconds() > float64(cacheDuration) {
return "", nil
}
cachedData, err := os.ReadFile(cacheFilepath)
if err != nil {
return "", err
}
return string(cachedData), nil
}
// SaveSlurmToJSON saves the slurm metadata to a JSON file
func SaveSlurmToJSON(cacheFilepath string, slurmMetadata map[string]SlurmMetadata) error {
jsonData, err := json.Marshal(slurmMetadata)
if err != nil {
return err
}
dir := filepath.Dir(cacheFilepath)
err = os.MkdirAll(dir, 0600)
if err != nil {
return err
}
err = os.WriteFile(cacheFilepath, jsonData, 0600)
if err != nil {
return err
}
return nil
}