/
FsCache.ts
212 lines (199 loc) · 6.21 KB
/
FsCache.ts
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import fs from 'fs-extra'
import path from 'path'
import shell from './shell'
import uuidv4 from 'uuid/v4'
import getSize from 'get-folder-size'
/**
* Gets the total size (in bytes) of a given directory
* @param dirPath path to the directory for which to compute size
*/
const getSizeAsync: (dirPath: string) => Promise<number> = dirPath =>
new Promise((resolve, reject) => {
getSize(dirPath, (err, size) => {
err ? reject(err) : resolve(size)
})
})
export interface CacheManifest {
/**
* Entries stored in the cache
*/
entries: CacheManifestEntry[]
/**
* Total size of the cache
*/
size: number
}
export interface CacheManifestEntry {
/**
* Id of the entry
* ex: my-miniapp@1.0.0
*/
id: string
/**
* Absolute path to the entry
* ex : /Users/blemair/.ern/cache/78c40fda-d5bb-4681-9bd2-a5fba9df1f70
*/
path: string
/**
* Size in bytes of the entry
*/
size: number
/**
* Time (epoc) when the entry was last accessed
*/
lastAccessed: number
}
const cacheManifestFileName = 'cache-manifest.json'
export const maxDefaultCacheSize = 2 * 1024 * 1024 * 1024 /* 2GB */
type AddObjectToCacheDirectory<T> = (obj: T, dirPath: string) => Promise<void>
type ObjectToId<T> = (obj: T) => string
/**
* Simple generic file system based LRU cache.
* This cache stores arbirtrary objects in unique directories.
* The root directory for the cache can be configured, as well as
* the maximum size allowed for the cache.
* Once cache reaches max size, the least recenctly accessed
* objects will be removed from the cache, until size of the cache
* falls below maximum size.
*/
export class FsCache<T> {
/**
* Root path to the directory holding the cache
*/
public readonly rootCachePath: string
/**
* Maximum cache size (in bytes)
*/
public readonly maxCacheSize: number
/**
* Custom function invoked to add an object entry to the cache
*/
private readonly addObjectToCacheDirectory: AddObjectToCacheDirectory<T>
/**
* Custom function invoked to convert an object entry to a unique id
*/
private readonly objectToId: ObjectToId<T>
constructor({
/**
* Custom function invoked to add an object entry to the cache
*/
addObjectToCacheDirectory,
/**
* Custom function invoked to convert an object entry to a unique id
*/
objectToId,
/**
* Maximum cache size (in bytes)
*/
maxCacheSize = maxDefaultCacheSize,
/**
* Root path to the directory holding the cache
*/
rootCachePath,
}: {
addObjectToCacheDirectory: AddObjectToCacheDirectory<T>
objectToId: ObjectToId<T>
maxCacheSize?: number
rootCachePath: string
}) {
this.rootCachePath = rootCachePath
this.maxCacheSize = maxCacheSize
this.addObjectToCacheDirectory = addObjectToCacheDirectory
this.objectToId = objectToId
fs.ensureDirSync(rootCachePath)
if (!fs.existsSync(this.cacheManifestPath)) {
fs.writeJsonSync(
this.cacheManifestPath,
{ entries: [], size: 0 },
{ spaces: 2 }
)
}
}
/**
* Indicates whether a given object is currently stored in the cache
* @param obj The object to check for presence in the cache
*/
public async isInCache(obj: T): Promise<boolean> {
const manifestObj = await this.getManifestObj()
return !!manifestObj.entries.find(e => e.id === this.objectToId(obj))
}
/**
* Adds an object entry to the cache
* @param obj The object to add to the cache
*/
public async addToCache(obj: T): Promise<string> {
if (await this.isInCache(obj)) {
throw new Error(
`Object with id ${this.objectToId(obj)} is already in the cache`
)
}
// Create unique directory to store the object
const pathToObjectCacheDir = this.createUniqueCacheDirectory()
// Invoke client provided function to write object in cache directory
await this.addObjectToCacheDirectory(obj, pathToObjectCacheDir)
// Compute cached object directory size
const cachedObjectDirSize = await getSizeAsync(pathToObjectCacheDir)
// Create manifest entry for new cached object
const entry: CacheManifestEntry = {
id: this.objectToId(obj),
lastAccessed: Date.now(),
path: pathToObjectCacheDir,
size: cachedObjectDirSize,
}
// Update manifest total cache size
const manifestObj = await this.getManifestObj()
manifestObj.size += cachedObjectDirSize
manifestObj.entries.push(entry)
// Clean cache if newly added object caused total cache
// size to exceed the defined max cache size
// Remove all entries that were least recently accessed
// until the total cache size falls below the max cache size
const entriesSortedByLat = manifestObj.entries.sort(
(a, b) => b.lastAccessed - a.lastAccessed
)
while (manifestObj.size >= this.maxCacheSize) {
const entryToRemove = entriesSortedByLat.pop()
manifestObj.size -= entryToRemove!.size
shell.rm('-rf', entryToRemove!.id)
}
manifestObj.entries = entriesSortedByLat
// Save updated manifest
await fs.writeJson(this.cacheManifestPath, manifestObj, { spaces: 2 })
return pathToObjectCacheDir
}
/**
* Gets path to the directory containing a cached object
* @param obj The object for which to retrieve cached directory path
*/
public async getObjectCachePath(obj: T): Promise<string | void> {
const manifestObj = await this.getManifestObj()
const cacheEntry = manifestObj.entries.find(
e => e.id === this.objectToId(obj)
)
if (cacheEntry) {
cacheEntry.lastAccessed = Date.now()
await fs.writeJson(this.cacheManifestPath, manifestObj, { spaces: 2 })
return cacheEntry.path
}
}
/**
* Gets the cache manifest object
*/
private async getManifestObj(): Promise<CacheManifest> {
return fs.readJson(this.cacheManifestPath)
}
/**
* Creates a uniquely named cache directory
*/
private createUniqueCacheDirectory(): string {
const pathToCacheDirectory = path.join(this.rootCachePath, uuidv4())
shell.mkdir(pathToCacheDirectory)
return pathToCacheDirectory
}
/**
* Gets the path to the cache manifest file
*/
private get cacheManifestPath() {
return path.join(this.rootCachePath, cacheManifestFileName)
}
}