Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert disk cache store #1062

Merged
merged 5 commits into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 1 addition & 14 deletions src/core/cache.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { setMetaContent } from "../util"
import { SnapshotCache } from "./drive/snapshot_cache"

export class Cache {
clear() {
this.store.clear()
this.session.clearCache()
}

resetCacheControl() {
Expand All @@ -18,18 +17,6 @@ export class Cache {
this.#setCacheControl("no-preview")
}

set store(store) {
if (typeof store === "string") {
SnapshotCache.setStore(store)
} else {
SnapshotCache.currentStore = store
}
}

get store() {
return SnapshotCache.currentStore
}

#setCacheControl(value) {
setMetaContent("turbo-cache-control", value)
}
Expand Down
64 changes: 0 additions & 64 deletions src/core/drive/cache_stores/disk_store.js

This file was deleted.

56 changes: 0 additions & 56 deletions src/core/drive/cache_stores/memory_store.js

This file was deleted.

2 changes: 1 addition & 1 deletion src/core/drive/navigator.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export class Navigator {
referrer: this.location,
...options
})
return this.currentVisit.start()
this.currentVisit.start()
}

submitForm(form, submitter) {
Expand Down
4 changes: 0 additions & 4 deletions src/core/drive/page_snapshot.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,6 @@ export class PageSnapshot extends Snapshot {
return this.documentElement.getAttribute("lang")
}

get html() {
return `${this.headElement.outerHTML}\n\n${this.element.outerHTML}`
}

get headElement() {
return this.headSnapshot.element
}
Expand Down
6 changes: 1 addition & 5 deletions src/core/drive/page_view.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { PageSnapshot } from "./page_snapshot"
import { SnapshotCache } from "./snapshot_cache"

export class PageView extends View {
snapshotCache = new SnapshotCache()
snapshotCache = new SnapshotCache(10)
lastRenderedLocation = new URL(location.href)
forceReloaded = false

Expand All @@ -32,10 +32,6 @@ export class PageView extends View {
return this.render(renderer)
}

setCacheStore(cacheName) {
SnapshotCache.setStore(cacheName)
}

clearSnapshotCache() {
this.snapshotCache.clear()
}
Expand Down
4 changes: 3 additions & 1 deletion src/core/drive/preloader.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ export class Preloader {
async preloadURL(link) {
const location = new URL(link.href)

if (await this.snapshotCache.has(location)) return
if (this.snapshotCache.has(location)) {
return
}

try {
const response = await fetch(location.toString(), { headers: { "Sec-Purpose": "prefetch", Accept: "text/html" } })
Expand Down
59 changes: 40 additions & 19 deletions src/core/drive/snapshot_cache.js
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]
}
}
}
26 changes: 13 additions & 13 deletions src/core/drive/visit.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,10 @@ export class Visit {
return this.isSamePage
}

async start() {
start() {
if (this.state == VisitState.initialized) {
this.recordTimingMetric(TimingMetric.visitStart)
this.state = VisitState.started
this.cachedSnapshot = await this.getCachedSnapshot()
this.adapter.visitStarted(this)
this.delegate.visitStarted(this)
}
Expand Down Expand Up @@ -155,10 +154,10 @@ export class Visit {
}
}

async issueRequest() {
issueRequest() {
if (this.hasPreloadedResponse()) {
this.simulateRequest()
} else if (!this.request && await this.shouldIssueRequest()) {
} else if (this.shouldIssueRequest() && !this.request) {
this.request = new FetchRequest(this, FetchMethod.get, this.location)
this.request.perform()
}
Expand Down Expand Up @@ -216,8 +215,8 @@ export class Visit {
}
}

async getCachedSnapshot() {
const snapshot = (await this.view.getCachedSnapshotForLocation(this.location)) || this.getPreloadedSnapshot()
getCachedSnapshot() {
const snapshot = this.view.getCachedSnapshotForLocation(this.location) || this.getPreloadedSnapshot()

if (snapshot && (!getAnchor(this.location) || snapshot.hasAnchor(getAnchor(this.location)))) {
if (this.action == "restore" || snapshot.isPreviewable) {
Expand All @@ -233,20 +232,21 @@ export class Visit {
}

hasCachedSnapshot() {
return this.cachedSnapshot != null
return this.getCachedSnapshot() != null
}

async loadCachedSnapshot() {
if (this.cachedSnapshot) {
const isPreview = await this.shouldIssueRequest()
loadCachedSnapshot() {
const snapshot = this.getCachedSnapshot()
if (snapshot) {
const isPreview = this.shouldIssueRequest()
this.render(async () => {
this.cacheSnapshot()
if (this.isSamePage) {
this.adapter.visitRendered(this)
} else {
if (this.view.renderPromise) await this.view.renderPromise

await this.renderPageSnapshot(this.cachedSnapshot, isPreview)
await this.renderPageSnapshot(snapshot, isPreview)

this.adapter.visitRendered(this)
if (!isPreview) {
Expand Down Expand Up @@ -391,10 +391,10 @@ export class Visit {
return typeof this.response == "object"
}

async shouldIssueRequest() {
shouldIssueRequest() {
if (this.isSamePage) {
return false
} else if (this.action === "restore") {
} else if (this.action == "restore") {
return !this.hasCachedSnapshot()
} else {
return this.willRender
Expand Down
6 changes: 5 additions & 1 deletion src/core/native/browser_adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ export class BrowserAdapter {

visitRequestStarted(visit) {
this.progressBar.setValue(0)
this.showVisitProgressBarAfterDelay()
if (visit.hasCachedSnapshot() || visit.action != "restore") {
this.showVisitProgressBarAfterDelay()
} else {
this.showProgressBar()
}
}

visitRequestCompleted(visit) {
Expand Down
Loading