Skip to content

Commit 61dac49

Browse files
committed
feat: async cache.read()/write(), remove .has()
1 parent 65b849c commit 61dac49

File tree

4 files changed

+26
-10
lines changed

4 files changed

+26
-10
lines changed

src/cache/cache.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
export interface Cache<T> {
2-
write (key: string, value: T): void;
3-
read (key: string): T | undefined;
4-
has (key: string): boolean;
2+
write (key: string, value: T): void | Promise<void>;
3+
read (key: string): T | undefined | Promise<T | undefined>;
54
}

src/cache/lru.ts

-4
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,6 @@ export class LRU<T> implements Cache<T> {
4242
return value
4343
}
4444

45-
has (key: string): boolean {
46-
return !!this.cache[key]
47-
}
48-
4945
remove (key: string) {
5046
const node = this.cache[key]
5147
node.prev.next = node.next

src/liquid.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,13 @@ export class Liquid {
7676

7777
for (const filepath of paths) {
7878
const { cache } = this.options
79-
if (cache && cache.has(filepath)) return cache.read(filepath)
79+
if (cache) {
80+
const tpls = yield cache.read(filepath)
81+
if (tpls) return tpls
82+
}
8083
if (!(sync ? this.fs.existsSync(filepath) : yield this.fs.exists(filepath))) continue
8184
const tpl = this.parse(sync ? this.fs.readFileSync(filepath) : yield this.fs.readFile(filepath), filepath)
82-
cache && cache.write(filepath, tpl)
85+
if (cache) cache.write(filepath, tpl)
8386
return tpl
8487
}
8588
throw this.lookupError(file, options.root)

test/integration/liquid/cache.ts

+19-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ describe('LiquidOptions#cache', function () {
7171
extname: '.html',
7272
cache: {
7373
read: (): Template[] | undefined => last,
74-
has: (): boolean => !!last,
7574
write: (key: string, value: Template[]) => { last = value }
7675
}
7776
})
@@ -82,6 +81,25 @@ describe('LiquidOptions#cache', function () {
8281
expect(await engine.renderFile('files/bar')).to.equal('foo')
8382
expect(await engine.renderFile('files/coo')).to.equal('foo')
8483
})
84+
it('should respect cache={} option (async)', async function () {
85+
const cached: { [key: string]: Template[] | undefined } = {}
86+
const engine = new Liquid({
87+
root: '/root/',
88+
extname: '.html',
89+
cache: {
90+
read: (key: string) => Promise.resolve(cached[key]),
91+
write: (key: string, value: Template[]) => { cached[key] = value; Promise.resolve() }
92+
}
93+
})
94+
mock({ '/root/files/foo.html': 'foo' })
95+
mock({ '/root/files/bar.html': 'bar' })
96+
mock({ '/root/files/coo.html': 'coo' })
97+
expect(await engine.renderFile('files/foo')).to.equal('foo')
98+
expect(await engine.renderFile('files/bar')).to.equal('bar')
99+
expect(await engine.renderFile('files/coo')).to.equal('coo')
100+
mock({ '/root/files/coo.html': 'COO' })
101+
expect(await engine.renderFile('files/coo')).to.equal('coo')
102+
})
85103
it('should not cache not exist file', async function () {
86104
const engine = new Liquid({
87105
root: '/root/',

0 commit comments

Comments
 (0)