working on cache for real now

This commit is contained in:
Jack Case
2025-12-12 01:40:12 +00:00
parent 1b0f7e7704
commit 7fbfa2a773
2 changed files with 85 additions and 54 deletions

View File

@@ -1,4 +1,4 @@
import { SlopDB } from "../scripts/indexed-db.js"
import { SlopDB, CheckCache } from "../scripts/indexed-db.js"
import { openDB, deleteDB } from "../scripts/idb/index.js"
describe("sanity check", () => {
@@ -34,17 +34,37 @@ describe("SlopDB", () => {
})
describe("version 2", () => {
it("creates a version 2 indexeddb", async () => {
const slopdb_v2 = new SlopDB(2)
await slopdb_v2.db_opened()
db = slopdb_v2.db
const object_stores = slopdb_v2.db.objectStoreNames
let slopdb
beforeEach(async () => {
slopdb = new SlopDB(2)
await slopdb.db_opened()
db = slopdb.db
})
it("creates a version 2 indexeddb", async () => {
const object_stores = slopdb.db.objectStoreNames
expect(object_stores).toContain("slop")
expect(object_stores).toContain("checkcache")
// slopdb_v2.db.close()
})
it("caches a checked url", async () => {
const cache = slopdb.get_check_cache()
expect(cache).toBeInstanceOf(CheckCache)
const slop_url = new URL("https://sloppy-slop.com/sloparticle")
await cache.store(slop_url.host)
const store_time = Date.now()
const cached_item = cache.get(slop_url.host)
expect(cached_item.url).toEqual(slop_url)
expect(cached_item.check_timestamp).toBeCloseTo(store_time)
})
})

View File

@@ -34,6 +34,62 @@ export class IDBCursorValueIterator {
}
export class CheckCache {
slopdb: SlopDB
cache_capacity: number
static cache_objectstore_name = "checkcache"
constructor(slopdb: SlopDB, max_entries: number) {
this.slopdb = slopdb
this.cache_capacity = max_entries
}
cache_item_factory(url: URL) {
return {
url: url,
check_timestamp: Date.now()
}
}
async store(url: string) {
const cache_store = this.slopdb.db.transaction(CheckCache.cache_objectstore_name, "readwrite").objectStore(CheckCache.cache_objectstore_name)
await cache_store.add(this.cache_item_factory(new URL(url)))
}
get(url: URL) {
return url
}
// async evict_least_recently_checked(count: number) {
// const transaction = this.slopdb.start_transaction(CheckCache.cache_objectstore_name, "readwrite")
// const cache_objectstore = transaction.objectStore(CheckCache.cache_objectstore_name)
// const cursor_result_promise = new Promise<Iterable<any>>((resolve, reject) => {
// const cache_cursor_request = cache_objectstore.openCursor()
// cache_cursor_request.onerror = (error) => {
// reject(error)
// }
// cache_cursor_request.onsuccess = (event) => {
// const cursor = cache_cursor_request.result
// resolve(new IDBCursorValueIterator(cursor))
// }
// })
// const cursor = await cursor_result_promise
// const key_array = Array.from(cursor)
// key_array.sort((a, b) => {
// const a_datetime = a.check_timestamp
// const b_datetime = b.check_timestamp
// return a_datetime.getTime - b_datetime.getTime
// })
// }
}
export class SlopDB {
version: number
@@ -72,53 +128,8 @@ export class SlopDB {
this.db = await this.open_promise
}
get_check_cache() {
return new CheckCache(this, 256)
}
}
// export class CheckCache {
// slopdb: SlopDB
// cache_capacity: number
// static cache_objectstore_name = "checkcache"
// constructor(slopdb: SlopDB, max_entries: number) {
// this.slopdb = slopdb
// this.cache_capacity = max_entries
// }
// cache_item_factory(url: URL) {
// return {
// url: url,
// check_timestamp: Date.now()
// }
// }
// async evict_least_recently_checked(count: number) {
// const transaction = this.slopdb.start_transaction(CheckCache.cache_objectstore_name, "readwrite")
// const cache_objectstore = transaction.objectStore(CheckCache.cache_objectstore_name)
// const cursor_result_promise = new Promise<Iterable<any>>((resolve, reject) => {
// const cache_cursor_request = cache_objectstore.openCursor()
// cache_cursor_request.onerror = (error) => {
// reject(error)
// }
// cache_cursor_request.onsuccess = (event) => {
// const cursor = cache_cursor_request.result
// resolve(new IDBCursorValueIterator(cursor))
// }
// })
// const cursor = await cursor_result_promise
// const key_array = Array.from(cursor)
// key_array.sort((a, b) => {
// const a_datetime = a.check_timestamp
// const b_datetime = b.check_timestamp
// return a_datetime.getTime - b_datetime.getTime
// })
// }
// }