Skip to content

Commit

Permalink
feat: async cache.read()/write(), remove .has()
Browse files Browse the repository at this point in the history
  • Loading branch information
harttle committed Mar 4, 2020
1 parent 65b849c commit 61dac49
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 10 deletions.
5 changes: 2 additions & 3 deletions src/cache/cache.ts
@@ -1,5 +1,4 @@
export interface Cache<T> {
write (key: string, value: T): void;
read (key: string): T | undefined;
has (key: string): boolean;
write (key: string, value: T): void | Promise<void>;
read (key: string): T | undefined | Promise<T | undefined>;
}
4 changes: 0 additions & 4 deletions src/cache/lru.ts
Expand Up @@ -42,10 +42,6 @@ export class LRU<T> implements Cache<T> {
return value
}

has (key: string): boolean {
return !!this.cache[key]
}

remove (key: string) {
const node = this.cache[key]
node.prev.next = node.next
Expand Down
7 changes: 5 additions & 2 deletions src/liquid.ts
Expand Up @@ -76,10 +76,13 @@ export class Liquid {

for (const filepath of paths) {
const { cache } = this.options
if (cache && cache.has(filepath)) return cache.read(filepath)
if (cache) {
const tpls = yield cache.read(filepath)
if (tpls) return tpls
}
if (!(sync ? this.fs.existsSync(filepath) : yield this.fs.exists(filepath))) continue
const tpl = this.parse(sync ? this.fs.readFileSync(filepath) : yield this.fs.readFile(filepath), filepath)
cache && cache.write(filepath, tpl)
if (cache) cache.write(filepath, tpl)
return tpl
}
throw this.lookupError(file, options.root)
Expand Down
20 changes: 19 additions & 1 deletion test/integration/liquid/cache.ts
Expand Up @@ -71,7 +71,6 @@ describe('LiquidOptions#cache', function () {
extname: '.html',
cache: {
read: (): Template[] | undefined => last,
has: (): boolean => !!last,
write: (key: string, value: Template[]) => { last = value }
}
})
Expand All @@ -82,6 +81,25 @@ describe('LiquidOptions#cache', function () {
expect(await engine.renderFile('files/bar')).to.equal('foo')
expect(await engine.renderFile('files/coo')).to.equal('foo')
})
it('should respect cache={} option (async)', async function () {
const cached: { [key: string]: Template[] | undefined } = {}
const engine = new Liquid({
root: '/root/',
extname: '.html',
cache: {
read: (key: string) => Promise.resolve(cached[key]),
write: (key: string, value: Template[]) => { cached[key] = value; Promise.resolve() }
}
})
mock({ '/root/files/foo.html': 'foo' })
mock({ '/root/files/bar.html': 'bar' })
mock({ '/root/files/coo.html': 'coo' })
expect(await engine.renderFile('files/foo')).to.equal('foo')
expect(await engine.renderFile('files/bar')).to.equal('bar')
expect(await engine.renderFile('files/coo')).to.equal('coo')
mock({ '/root/files/coo.html': 'COO' })
expect(await engine.renderFile('files/coo')).to.equal('coo')
})
it('should not cache not exist file', async function () {
const engine = new Liquid({
root: '/root/',
Expand Down

0 comments on commit 61dac49

Please sign in to comment.