Skip to content

Commit 8c09c3d

Browse files
committed
Adding Caching Adapter, allows caching of _Role and _User queries (fixes #168) (#1664)
* Adding Caching Adapter, allows caching of _Role and _User queries.
1 parent 5d887e1 commit 8c09c3d

18 files changed

+526
-134
lines changed

spec/CacheController.spec.js

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
var CacheController = require('../src/Controllers/CacheController.js').default;
2+
3+
describe('CacheController', function() {
4+
var FakeCacheAdapter;
5+
var FakeAppID = 'foo';
6+
var KEY = 'hello';
7+
8+
beforeEach(() => {
9+
FakeCacheAdapter = {
10+
get: () => Promise.resolve(null),
11+
put: jasmine.createSpy('put'),
12+
del: jasmine.createSpy('del'),
13+
clear: jasmine.createSpy('clear')
14+
}
15+
16+
spyOn(FakeCacheAdapter, 'get').and.callThrough();
17+
});
18+
19+
20+
it('should expose role and user caches', (done) => {
21+
var cache = new CacheController(FakeCacheAdapter, FakeAppID);
22+
23+
expect(cache.role).not.toEqual(null);
24+
expect(cache.role.get).not.toEqual(null);
25+
expect(cache.user).not.toEqual(null);
26+
expect(cache.user.get).not.toEqual(null);
27+
28+
done();
29+
});
30+
31+
32+
['role', 'user'].forEach((cacheName) => {
33+
it('should prefix ' + cacheName + ' cache', () => {
34+
var cache = new CacheController(FakeCacheAdapter, FakeAppID)[cacheName];
35+
36+
cache.put(KEY, 'world');
37+
var firstPut = FakeCacheAdapter.put.calls.first();
38+
expect(firstPut.args[0]).toEqual([FakeAppID, cacheName, KEY].join(':'));
39+
40+
cache.get(KEY);
41+
var firstGet = FakeCacheAdapter.get.calls.first();
42+
expect(firstGet.args[0]).toEqual([FakeAppID, cacheName, KEY].join(':'));
43+
44+
cache.del(KEY);
45+
var firstDel = FakeCacheAdapter.del.calls.first();
46+
expect(firstDel.args[0]).toEqual([FakeAppID, cacheName, KEY].join(':'));
47+
});
48+
});
49+
50+
it('should clear the entire cache', () => {
51+
var cache = new CacheController(FakeCacheAdapter, FakeAppID);
52+
53+
cache.clear();
54+
expect(FakeCacheAdapter.clear.calls.count()).toEqual(1);
55+
56+
cache.user.clear();
57+
expect(FakeCacheAdapter.clear.calls.count()).toEqual(2);
58+
59+
cache.role.clear();
60+
expect(FakeCacheAdapter.clear.calls.count()).toEqual(3);
61+
});
62+
63+
it('should handle cache rejections', (done) => {
64+
65+
FakeCacheAdapter.get = () => Promise.reject();
66+
67+
var cache = new CacheController(FakeCacheAdapter, FakeAppID);
68+
69+
cache.get('foo').then(done, () => {
70+
fail('Promise should not be rejected.');
71+
});
72+
});
73+
74+
});

spec/InMemoryCache.spec.js

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
const InMemoryCache = require('../src/Adapters/Cache/InMemoryCache').default;
2+
3+
4+
describe('InMemoryCache', function() {
5+
var BASE_TTL = {
6+
ttl: 10
7+
};
8+
var NO_EXPIRE_TTL = {
9+
ttl: NaN
10+
};
11+
var KEY = 'hello';
12+
var KEY_2 = KEY + '_2';
13+
14+
var VALUE = 'world';
15+
16+
17+
function wait(sleep) {
18+
return new Promise(function(resolve, reject) {
19+
setTimeout(resolve, sleep);
20+
})
21+
}
22+
23+
it('should destroy a expire items in the cache', (done) => {
24+
var cache = new InMemoryCache(BASE_TTL);
25+
26+
cache.put(KEY, VALUE);
27+
28+
var value = cache.get(KEY);
29+
expect(value).toEqual(VALUE);
30+
31+
wait(BASE_TTL.ttl * 5).then(() => {
32+
value = cache.get(KEY)
33+
expect(value).toEqual(null);
34+
done();
35+
});
36+
});
37+
38+
it('should delete items', (done) => {
39+
var cache = new InMemoryCache(NO_EXPIRE_TTL);
40+
cache.put(KEY, VALUE);
41+
cache.put(KEY_2, VALUE);
42+
expect(cache.get(KEY)).toEqual(VALUE);
43+
expect(cache.get(KEY_2)).toEqual(VALUE);
44+
45+
cache.del(KEY);
46+
expect(cache.get(KEY)).toEqual(null);
47+
expect(cache.get(KEY_2)).toEqual(VALUE);
48+
49+
cache.del(KEY_2);
50+
expect(cache.get(KEY)).toEqual(null);
51+
expect(cache.get(KEY_2)).toEqual(null);
52+
done();
53+
});
54+
55+
it('should clear all items', (done) => {
56+
var cache = new InMemoryCache(NO_EXPIRE_TTL);
57+
cache.put(KEY, VALUE);
58+
cache.put(KEY_2, VALUE);
59+
60+
expect(cache.get(KEY)).toEqual(VALUE);
61+
expect(cache.get(KEY_2)).toEqual(VALUE);
62+
cache.clear();
63+
64+
expect(cache.get(KEY)).toEqual(null);
65+
expect(cache.get(KEY_2)).toEqual(null);
66+
done();
67+
});
68+
69+
it('should deafult TTL to 5 seconds', () => {
70+
var cache = new InMemoryCache({});
71+
expect(cache.ttl).toEqual(5 * 1000);
72+
});
73+
74+
});

spec/InMemoryCacheAdapter.spec.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
var InMemoryCacheAdapter = require('../src/Adapters/Cache/InMemoryCacheAdapter').default;
2+
3+
describe('InMemoryCacheAdapter', function() {
4+
var KEY = 'hello';
5+
var VALUE = 'world';
6+
7+
function wait(sleep) {
8+
return new Promise(function(resolve, reject) {
9+
setTimeout(resolve, sleep);
10+
})
11+
}
12+
13+
it('should expose promisifyed methods', (done) => {
14+
var cache = new InMemoryCacheAdapter({
15+
ttl: NaN
16+
});
17+
18+
var noop = () => {};
19+
20+
// Verify all methods return promises.
21+
Promise.all([
22+
cache.put(KEY, VALUE),
23+
cache.del(KEY),
24+
cache.get(KEY),
25+
cache.clear()
26+
]).then(() => {
27+
done();
28+
});
29+
});
30+
31+
it('should get/set/clear', (done) => {
32+
var cache = new InMemoryCacheAdapter({
33+
ttl: NaN
34+
});
35+
36+
cache.put(KEY, VALUE)
37+
.then(() => cache.get(KEY))
38+
.then((value) => expect(value).toEqual(VALUE))
39+
.then(() => cache.clear())
40+
.then(() => cache.get(KEY))
41+
.then((value) => expect(value).toEqual(null))
42+
.then(done);
43+
});
44+
45+
it('should expire after ttl', (done) => {
46+
var cache = new InMemoryCacheAdapter({
47+
ttl: 10
48+
});
49+
50+
cache.put(KEY, VALUE)
51+
.then(() => cache.get(KEY))
52+
.then((value) => expect(value).toEqual(VALUE))
53+
.then(wait.bind(null, 50))
54+
.then(() => cache.get(KEY))
55+
.then((value) => expect(value).toEqual(null))
56+
.then(done);
57+
})
58+
59+
});

spec/helper.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ const setServerConfiguration = configuration => {
6363
DatabaseAdapter.clearDatabaseSettings();
6464
currentConfiguration = configuration;
6565
server.close();
66-
cache.clearCache();
66+
cache.clear();
6767
app = express();
6868
api = new ParseServer(configuration);
6969
app.use('/1', api);

src/Adapters/Cache/CacheAdapter.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
export class CacheAdapter {
2+
/**
3+
* Get a value in the cache
4+
* @param key Cache key to get
5+
* @return Promise that will eventually resolve to the value in the cache.
6+
*/
7+
get(key) {}
8+
9+
/**
10+
* Set a value in the cache
11+
* @param key Cache key to set
12+
* @param value Value to set the key
13+
* @param ttl Optional TTL
14+
*/
15+
put(key, value, ttl) {}
16+
17+
/**
18+
* Remove a value from the cache.
19+
* @param key Cache key to remove
20+
*/
21+
del(key) {}
22+
23+
/**
24+
* Empty a cache
25+
*/
26+
clear() {}
27+
}

src/Adapters/Cache/InMemoryCache.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
const DEFAULT_CACHE_TTL = 5 * 1000;
2+
3+
4+
export class InMemoryCache {
5+
constructor({
6+
ttl = DEFAULT_CACHE_TTL
7+
}) {
8+
this.ttl = ttl;
9+
this.cache = Object.create(null);
10+
}
11+
12+
get(key) {
13+
let record = this.cache[key];
14+
if (record == null) {
15+
return null;
16+
}
17+
18+
// Has Record and isnt expired
19+
if (isNaN(record.expire) || record.expire >= Date.now()) {
20+
return record.value;
21+
}
22+
23+
// Record has expired
24+
delete this.cache[key];
25+
return null;
26+
}
27+
28+
put(key, value, ttl = this.ttl) {
29+
if (ttl < 0 || isNaN(ttl)) {
30+
ttl = NaN;
31+
}
32+
33+
var record = {
34+
value: value,
35+
expire: ttl + Date.now()
36+
}
37+
38+
if (!isNaN(record.expire)) {
39+
record.timeout = setTimeout(() => {
40+
this.del(key);
41+
}, ttl);
42+
}
43+
44+
this.cache[key] = record;
45+
}
46+
47+
del(key) {
48+
var record = this.cache[key];
49+
if (record == null) {
50+
return;
51+
}
52+
53+
if (record.timeout) {
54+
clearTimeout(record.timeout);
55+
}
56+
57+
delete this.cache[key];
58+
}
59+
60+
clear() {
61+
this.cache = Object.create(null);
62+
}
63+
64+
}
65+
66+
export default InMemoryCache;
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import {InMemoryCache} from './InMemoryCache';
2+
3+
export class InMemoryCacheAdapter {
4+
5+
constructor(ctx) {
6+
this.cache = new InMemoryCache(ctx)
7+
}
8+
9+
get(key) {
10+
return new Promise((resolve, reject) => {
11+
let record = this.cache.get(key);
12+
if (record == null) {
13+
return resolve(null);
14+
}
15+
16+
return resolve(JSON.parse(record));
17+
})
18+
}
19+
20+
put(key, value, ttl) {
21+
this.cache.put(key, JSON.stringify(value), ttl);
22+
return Promise.resolve();
23+
}
24+
25+
del(key) {
26+
this.cache.del(key);
27+
return Promise.resolve();
28+
}
29+
30+
clear() {
31+
this.cache.clear();
32+
return Promise.resolve();
33+
}
34+
}
35+
36+
export default InMemoryCacheAdapter;

0 commit comments

Comments
 (0)