Skip to content

Commit

Permalink
Merge branch 'master' into develop
Browse files Browse the repository at this point in the history
* master:
  fix(cache): TLRU: expected behavior on getSet()
  • Loading branch information
postspectacular committed Jan 16, 2019
2 parents 2cc682a + a6e6f2a commit df49c71
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
8 changes: 8 additions & 0 deletions packages/cache/src/tlru.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,14 @@ export class TLRUCache<K, V> extends LRUCache<K, V> {
return value;
}

getSet(key: K, retrieve: () => Promise<V>, ttl = this.opts.ttl): Promise<V> {
const e = this.get(key);
if (e) {
return Promise.resolve(e);
}
return retrieve().then((v) => this.set(key, v, ttl));
}

prune() {
const now = Date.now();
let cell = this.items.head;
Expand Down
14 changes: 14 additions & 0 deletions packages/cache/test/tlru.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,18 @@ describe("TLRU", () => {
}, 20);
});

it("getSet ttl", (done) => {
setTimeout(() => {
c.getSet("a", () => Promise.resolve(10)).then(v => {
assert.equal(v, 10);
assert(!c.has("b"));
assert(!c.has("c"));
assert.deepEqual([...evicts], [["a", 1], ["b", 2], ["c", 3]]);
assert.deepEqual([...c.keys()], ["a"]);
assert.deepEqual([...c.values()], [10]);
done();
}).catch(done)
}, 20)
});

});

0 comments on commit df49c71

Please sign in to comment.