Skip to content

Commit 1659a7b

Browse files
committed
more logging
1 parent 1e382ac commit 1659a7b

2 files changed

Lines changed: 19 additions & 6 deletions

File tree

packages/core/src/fscache.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export class FsCache<K, V> implements WorkspaceFileCache<any, any> {
4949
else await writeText(fn, JSON.stringify(value, null, 2))
5050
this.dbg(`set ${sha}: updated`)
5151
} catch (e) {
52-
this.dbg(`set ${sha}: update failed (${errorMessage(e)})`)
52+
this.dbg(`set ${sha}: failed (${errorMessage(e)})`)
5353
}
5454
}
5555
async values(): Promise<any[]> {

packages/core/src/memcache.ts

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// Import necessary modules and types
2-
import { host } from "./host"
32
import { CACHE_FORMAT_VERSION, CACHE_SHA_LENGTH, CHANGE } from "./constants"
43
import { hash } from "./crypto"
54
import type { CacheEntry } from "./cache"
5+
import debug, { Debugger } from "debug"
66

77
/**
88
* A cache class that manages entries stored in JSONL format.
@@ -17,10 +17,12 @@ export class MemoryCache<K, V>
1717
protected _entries: Record<string, CacheEntry<V>>
1818
private _pending: Record<string, Promise<V>>
1919
private readonly hashOptions: HashOptions
20+
protected dbg: Debugger
2021

2122
// Constructor is private to enforce the use of byName factory method
2223
constructor(public readonly name: string) {
2324
super() // Initialize EventTarget
25+
this.dbg = debug(`genaiscript:cache:${name}`) // Initialize debugger
2426
this.hashOptions = {
2527
salt: CACHE_FORMAT_VERSION,
2628
length: CACHE_SHA_LENGTH,
@@ -51,7 +53,9 @@ export class MemoryCache<K, V>
5153
if (key === undefined) return undefined // Handle undefined key
5254
await this.initialize()
5355
const sha = await this.getSha(key)
54-
return this._entries[sha]?.val
56+
const res = this._entries[sha]?.val
57+
this.dbg(`get ${sha}: ${res !== undefined ? "hit" : "miss"}`)
58+
return res
5559
}
5660

5761
async getOrUpdate(
@@ -61,16 +65,23 @@ export class MemoryCache<K, V>
6165
): Promise<{ key: string; value: V; cached?: boolean }> {
6266
await this.initialize()
6367
const sha = await hash(key)
64-
if (this._entries[sha])
68+
if (this._entries[sha]) {
69+
this.dbg(`getup ${sha}: hit`)
6570
return { key: sha, value: this._entries[sha].val, cached: true }
66-
if (this._pending[sha])
71+
}
72+
if (this._pending[sha]) {
73+
this.dbg(`getup ${sha}: hit (pending)`)
6774
return { key: sha, value: await this._pending[sha], cached: true }
75+
}
6876

6977
try {
7078
const p = updater()
7179
this._pending[sha] = p
7280
const value = await p
73-
if (!validator || validator(value)) await this.set(key, value)
81+
if (!validator || validator(value)) {
82+
await this.set(key, value)
83+
this.dbg(`set ${sha}: updated`)
84+
}
7485
return { key: sha, value, cached: false }
7586
} finally {
7687
delete this._pending[sha]
@@ -91,9 +102,11 @@ export class MemoryCache<K, V>
91102
const ent = { sha, val } satisfies CacheEntry<V>
92103
const ex = this._entries[sha]
93104
if (ex && JSON.stringify(ex) == JSON.stringify(ent)) return // No change
105+
94106
this._entries[sha] = ent
95107
await this.appendEntry(ent)
96108
this.dispatchEvent(new Event(CHANGE)) // Notify listeners
109+
this.dbg(`set ${sha}: updated`)
97110
}
98111

99112
/**

0 commit comments

Comments
 (0)