-
Notifications
You must be signed in to change notification settings - Fork 429
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/main' into morph-refreshes
* origin/main: Revert disk cache store (#1062)
- Loading branch information
Showing
16 changed files
with
85 additions
and
312 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,56 @@ | ||
import { DiskStore } from "./cache_stores/disk_store" | ||
import { MemoryStore } from "./cache_stores/memory_store" | ||
import { toCacheKey } from "../url" | ||
|
||
export class SnapshotCache { | ||
static currentStore = new MemoryStore(10) | ||
|
||
static setStore(storeName) { | ||
switch (storeName) { | ||
case "memory": | ||
SnapshotCache.currentStore = new MemoryStore(10) | ||
break | ||
case "disk": | ||
SnapshotCache.currentStore = new DiskStore() | ||
break | ||
default: | ||
throw new Error(`Invalid store name: ${storeName}`) | ||
} | ||
keys = [] | ||
snapshots = {} | ||
|
||
constructor(size) { | ||
this.size = size | ||
} | ||
|
||
has(location) { | ||
return SnapshotCache.currentStore.has(location) | ||
return toCacheKey(location) in this.snapshots | ||
} | ||
|
||
get(location) { | ||
return SnapshotCache.currentStore.get(location) | ||
if (this.has(location)) { | ||
const snapshot = this.read(location) | ||
this.touch(location) | ||
return snapshot | ||
} | ||
} | ||
|
||
put(location, snapshot) { | ||
return SnapshotCache.currentStore.put(location, snapshot) | ||
this.write(location, snapshot) | ||
this.touch(location) | ||
return snapshot | ||
} | ||
|
||
clear() { | ||
return SnapshotCache.currentStore.clear() | ||
this.snapshots = {} | ||
} | ||
|
||
// Private | ||
|
||
read(location) { | ||
return this.snapshots[toCacheKey(location)] | ||
} | ||
|
||
write(location, snapshot) { | ||
this.snapshots[toCacheKey(location)] = snapshot | ||
} | ||
|
||
touch(location) { | ||
const key = toCacheKey(location) | ||
const index = this.keys.indexOf(key) | ||
if (index > -1) this.keys.splice(index, 1) | ||
this.keys.unshift(key) | ||
this.trim() | ||
} | ||
|
||
trim() { | ||
for (const key of this.keys.splice(this.size)) { | ||
delete this.snapshots[key] | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.