Skip to content

Commit 5c72dab

Browse files
committed
Added NonSerializingCache (if you want the non-serializing version of the cache fill)
1 parent a470107 commit 5c72dab

File tree

4 files changed

+157
-5
lines changed

4 files changed

+157
-5
lines changed

index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
const { BaseCache } = require('./lib/base');
2-
const { SerializingCache } = require('./lib/serializing');
2+
const { SerializingCache, NonSerializingCache } = require('./lib/serializing');
33
const { MemoryCache } = require('./lib/memory');
44
const { OnionCache } = require('./lib/onion');
55

66
exports.BaseCache = BaseCache;
77
exports.SerializingCache = SerializingCache;
8+
exports.NonSerializingCache = NonSerializingCache;
89
exports.MemoryCache = MemoryCache;
910
exports.OnionCache = OnionCache;

lib/serializing.js

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,10 @@ class SerializingCache extends BaseCache {
122122
return this.fill(key).then((result) => {
123123
debug("Fill '%s' successful", key);
124124
return this.cache.set(key, result).then(() => {
125-
debug("Storing '%s' successful");
125+
debug("Storing '%s' successful", key);
126126
return result;
127127
}).catch((err) => {
128-
debug("Storing '%s' failed");
128+
debug("Storing '%s' failed", key);
129129
throw err;
130130
});
131131
}).catch((err) => {
@@ -136,4 +136,45 @@ class SerializingCache extends BaseCache {
136136
}
137137
}
138138

139+
class NonSerializingCache extends BaseCache {
140+
constructor(cache, fill) {
141+
super();
142+
143+
this.cache = cache;
144+
this.fill = fill;
145+
}
146+
147+
get(key) {
148+
debug("Getting '%s'", key);
149+
return this.cache.get(key).then((result) => {
150+
if (result === undefined) {
151+
return this.fill(key).then((result) => {
152+
debug("Fill '%s' successful", key);
153+
return this.cache.set(key, result).then(() => {
154+
debug("Storing '%s' successful", key);
155+
return result;
156+
}).catch((err) => {
157+
debug("Storing '%s' failed", key);
158+
throw err;
159+
});
160+
}).catch((err) => {
161+
debug("Fill '%s' failed", key);
162+
throw err;
163+
});
164+
}
165+
166+
debug("Get '%s' successful", key);
167+
return result;
168+
});
169+
}
170+
171+
del(key) {
172+
debug("Deleting '%s'", key);
173+
return this.cache.del(key);
174+
}
175+
}
176+
177+
139178
exports.SerializingCache = SerializingCache;
179+
exports.NonSerializingCache = NonSerializingCache;
180+

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.1.0",
3+
"version": "1.2.0",
44
"main": "./index",
55
"private": true,
66
"author": "[email protected]",

test/serializing.js

Lines changed: 111 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const { MemoryCache } = require('..');
2-
const { SerializingCache } = require('..');
2+
const { SerializingCache, NonSerializingCache } = require('..');
33
const { DelayedCache } = require('./util/delayed');
44

55
const assert = require('assert');
@@ -113,3 +113,113 @@ describe("SerializingCache", function () {
113113
});
114114
});
115115
});
116+
117+
describe("NonSerializingCache", function () {
118+
describe('get', function () {
119+
it ("should fill using callback", function () {
120+
const testValue = 'value';
121+
const testKey = 'key';
122+
123+
const memoryCache = new MemoryCache();
124+
const cache = new NonSerializingCache(memoryCache, () => {
125+
return new Promise((resolve) => resolve(testValue));
126+
});
127+
128+
return cache.get(testKey).then((result) => {
129+
assert(result === testValue);
130+
}).then(() => {
131+
return memoryCache.get(testKey).then((result) => {
132+
assert(result === testValue);
133+
})
134+
});
135+
});
136+
137+
it("should only issue one read per request run", function () {
138+
class TestCache extends MemoryCache {
139+
constructor() {
140+
super();
141+
this.counter = 0;
142+
}
143+
get(key) {
144+
this.counter++;
145+
return super.get(key);
146+
}
147+
}
148+
149+
const testValue = 'value';
150+
const testKey = 'key';
151+
152+
const rootCache = new DelayedCache(new TestCache(), { get: 10 });
153+
const cache = new NonSerializingCache(rootCache, () => {
154+
return new Promise((resolve) => resolve(testValue));
155+
});
156+
157+
return Promise.all([
158+
cache.get(testKey), cache.get(testKey), cache.get(testKey)
159+
]).then(() => {
160+
assert.equal(rootCache.cache.counter, 3, "Cache should be read three times");
161+
});
162+
});
163+
});
164+
165+
describe('set', function () {
166+
it('should not be supported', function () {
167+
const memoryCache = new MemoryCache();
168+
const cache = new NonSerializingCache(memoryCache, () => {
169+
return new Promise((resolve) => resolve("value"));
170+
});
171+
const testKey = 'key';
172+
173+
return cache.set(testKey, "dummy").then(() => {
174+
throw Error("Set successful");
175+
}, (err) => {});
176+
});
177+
});
178+
179+
describe('del', function () {
180+
it ("should fill using callback", function () {
181+
const testValue = 'value';
182+
const testKey = 'key';
183+
184+
const memoryCache = new MemoryCache();
185+
const cache = new NonSerializingCache(memoryCache, () => {
186+
return new Promise((resolve) => resolve(testValue));
187+
});
188+
189+
return cache.get(testKey).then((result) => {
190+
assert(result === testValue);
191+
}).then(() => {
192+
return memoryCache.get(testKey).then((result) => {
193+
assert(result === testValue);
194+
})
195+
});
196+
});
197+
198+
it("should only issue one delete per request run", function () {
199+
class TestCache extends MemoryCache {
200+
constructor() {
201+
super();
202+
this.counter = 0;
203+
}
204+
del(key) {
205+
this.counter++;
206+
return super.del(key);
207+
}
208+
}
209+
210+
const testValue = 'value';
211+
const testKey = 'key';
212+
213+
const rootCache = new DelayedCache(new TestCache(), { get: 10 });
214+
const cache = new NonSerializingCache(rootCache, () => {
215+
return new Promise((resolve) => resolve(testValue));
216+
});
217+
218+
return Promise.all([
219+
cache.del(testKey), cache.del(testKey), cache.del(testKey)
220+
]).then(() => {
221+
assert.equal(rootCache.cache.counter, 3, "Cache entry should be deleted three times");
222+
});
223+
});
224+
});
225+
});

0 commit comments

Comments
 (0)