Skip to content

Commit a470107

Browse files
committed
Now propagating caches upwards in the OnionCache
1 parent bf34658 commit a470107

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-2
lines changed

lib/onion.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,24 @@ class OnionCache extends BaseCache {
2121
if (!result) {
2222
return this.__get(key, index + 1);
2323
}
24-
return result;
24+
25+
return this.__update(key, result, index - 1).then(() => {
26+
return result;
27+
});
2528
});
2629
}
2730

31+
__update(key, value, index) {
32+
if (index < 0) {
33+
return Promise.resolve();
34+
}
35+
36+
const cache = this.caches[index];
37+
return cache.set(key, value).then(() => {
38+
return this.__update(key, value, index - 1);
39+
})
40+
}
41+
2842
set(key, value) {
2943
return this.__set(key, value, 0);
3044
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "layer-cache",
3-
"version": "1.0.3",
3+
"version": "1.1.0",
44
"main": "./index",
55
"private": true,
66
"author": "[email protected]",

test/onion.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,30 @@ describe('OnionCache', function () {
3232
})
3333
});
3434
});
35+
36+
it('should populate first cache with data from second cache', function () {
37+
const fastCache = new MemoryCache();
38+
const slowCache = new MemoryCache();
39+
const cache = new OnionCache(fastCache, slowCache);
40+
const testKey = 'key';
41+
const testValue = 'value';
42+
43+
return slowCache.set(testKey, testValue).then(function () {
44+
return fastCache.get(testKey).then((result) => {
45+
assert(result === undefined);
46+
}).then(() => {
47+
return cache.get(testKey).then((result) => {
48+
assert(result === testValue);
49+
});
50+
}).then(() => {
51+
return fastCache.get(testKey).then((result) => {
52+
assert(result === testValue);
53+
})
54+
})
55+
56+
});
57+
});
58+
3559
});
3660

3761
describe('set', function () {

0 commit comments

Comments
 (0)