Skip to content

Commit 141ece6

Browse files
committed
Use InMemoryCacheAdapter
1 parent c2dd5c7 commit 141ece6

10 files changed

+64
-65
lines changed

spec/ParseInstallation.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const installationSchema = { fields: Object.assign({}, defaultColumns._Default,
1818
describe('Installations', () => {
1919

2020
beforeEach(() => {
21-
config.database.schemaCache.reset();
21+
config.database.schemaCache.clear();
2222
});
2323

2424
it_exclude_dbs(['postgres'])('creates an android installation with ids', (done) => {

spec/PointerPermissions.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ var Config = require('../src/Config');
66
describe('Pointer Permissions', () => {
77

88
beforeEach(() => {
9-
new Config(Parse.applicationId).database.schemaCache.reset();
9+
new Config(Parse.applicationId).database.schemaCache.clear();
1010
});
1111

1212
it_exclude_dbs(['postgres'])('should work with find', (done) => {

spec/Schema.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ var hasAllPODobject = () => {
2121

2222
describe('SchemaController', () => {
2323
beforeEach(() => {
24-
config.database.schemaCache.reset();
24+
config.database.schemaCache.clear();
2525
});
2626

2727
it('can validate one object', (done) => {

spec/schemas.spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,8 @@ var masterKeyHeaders = {
118118
describe('schemas', () => {
119119

120120
beforeEach(() => {
121-
config.database.schemaCache.reset();
122-
})
121+
config.database.schemaCache.clear();
122+
});
123123

124124
it('requires the master key to get all schemas', (done) => {
125125
request.get({

src/Config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export class Config {
3636

3737
// Create a new DatabaseController per request
3838
if (cacheInfo.databaseController) {
39-
this.database = new DatabaseController(cacheInfo.databaseController.adapter, new SchemaCache(applicationId, cacheInfo.schemaCacheTTL));
39+
this.database = new DatabaseController(cacheInfo.databaseController.adapter, new SchemaCache(cacheInfo.schemaCacheTTL));
4040
}
4141

4242
this.serverURL = cacheInfo.serverURL;

src/Controllers/CacheController.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ function joinKeys(...keys) {
1313
* eg "Role" or "Session"
1414
*/
1515
export class SubCache {
16-
constructor(prefix, cacheController) {
16+
constructor(prefix, cacheController, ttl) {
1717
this.prefix = prefix;
1818
this.cache = cacheController;
19+
this.ttl = ttl;
1920
}
2021

2122
get(key) {

src/Controllers/DatabaseController.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ import _ from 'lodash';
77
var mongodb = require('mongodb');
88
var Parse = require('parse/node').Parse;
99

10-
var SchemaController = require('../Controllers/SchemaController');
10+
var SchemaController = require('./SchemaController');
11+
import SchemaCache from './SchemaCache';
12+
1113
const deepcopy = require('deepcopy');
1214

1315
function addWriteACL(query, acl) {

src/Controllers/SchemaCache.js

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,46 @@
1-
import Parse from 'parse/node';
2-
import LRU from 'lru-cache';
3-
4-
let MAIN_SCHEMA = "__MAIN_SCHEMA";
1+
import { InMemoryCacheAdapter } from '../Adapters/Cache/InMemoryCacheAdapter';
52

3+
const CACHED_KEYS = "__CACHED_KEYS";
4+
const MAIN_SCHEMA = "__MAIN_SCHEMA";
5+
const SCHEMA_CACHE_PREFIX = "__SCHEMA";
66
export default class SchemaCache {
77
cache: Object;
88

9-
constructor(appId: string, timeout: number = 0, maxSize: number = 10000) {
10-
this.appId = appId;
11-
this.timeout = timeout;
12-
this.maxSize = maxSize;
13-
this.cache = new LRU({
14-
max: maxSize,
15-
maxAge: timeout
16-
});
17-
}
9+
constructor(ttl) {
10+
this.ttl = ttl;
11+
this.cache = new InMemoryCacheAdapter({ ttl });
12+
}
1813

19-
get() {
20-
if (this.timeout <= 0) {
14+
getAllClasses() {
15+
if (this.ttl <= 0) {
2116
return;
2217
}
23-
return this.cache.get(this.appId+MAIN_SCHEMA);
18+
return this.cache.get(SCHEMA_CACHE_PREFIX+MAIN_SCHEMA);
2419
}
2520

26-
set(schema) {
27-
if (this.timeout <= 0) {
21+
setAllClasses(schema) {
22+
if (this.ttl <= 0) {
2823
return;
2924
}
30-
this.cache.set(this.appId+MAIN_SCHEMA, schema);
25+
this.cache.put(SCHEMA_CACHE_PREFIX+MAIN_SCHEMA, schema, this.ttl);
3126
}
3227

3328
setOneSchema(className, schema) {
34-
if (this.timeout <= 0) {
29+
if (this.ttl <= 0) {
3530
return;
3631
}
37-
this.cache.set(this.appId+className, schema);
32+
this.cache.put(SCHEMA_CACHE_PREFIX+className, schema, this.ttl);
3833
}
3934

4035
getOneSchema(className) {
41-
if (this.timeout <= 0) {
36+
if (this.ttl <= 0) {
4237
return;
4338
}
44-
return this.cache.get(this.appId+className);
39+
return this.cache.get(SCHEMA_CACHE_PREFIX+className);
4540
}
4641

47-
reset() {
48-
this.cache.reset();
42+
clear() {
43+
// That clears all caches...
44+
this.cache.clear();
4945
}
5046
}

src/Controllers/SchemaController.js

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ class SchemaController {
286286
this.data = {};
287287
this.perms = {};
288288
if (clearCache) {
289-
this._cache.reset();
289+
this._cache.clear();
290290
}
291291
return this.getAllClasses(clearCache)
292292
.then(allSchemas => {
@@ -308,37 +308,39 @@ class SchemaController {
308308

309309
getAllClasses(clearCache = false) {
310310
if (clearCache) {
311-
this._cache.reset();
311+
this._cache.clear();
312312
}
313-
let allClasses = this._cache.get();
314-
if (allClasses && allClasses.length && !clearCache) {
315-
return Promise.resolve(allClasses);
316-
}
317-
return this._dbAdapter.getAllClasses()
318-
.then(allSchemas => allSchemas.map(injectDefaultSchema))
319-
.then(allSchemas => {
320-
this._cache.set(allSchemas);
321-
return allSchemas;
322-
})
313+
return this._cache.getAllClasses().then((allClasses) => {
314+
if (allClasses && allClasses.length && !clearCache) {
315+
return Promise.resolve(allClasses);
316+
}
317+
return this._dbAdapter.getAllClasses()
318+
.then(allSchemas => allSchemas.map(injectDefaultSchema))
319+
.then(allSchemas => {
320+
this._cache.setAllClasses(allSchemas);
321+
return allSchemas;
322+
})
323+
});
323324
}
324325

325326
getOneSchema(className, allowVolatileClasses = false, clearCache) {
326327
if (clearCache) {
327-
this._cache.reset();
328-
}
329-
let cached = this._cache.getOneSchema(className);
330-
if (cached && !clearCache) {
331-
return Promise.resolve(cached);
328+
this._cache.clear();
332329
}
333-
if (allowVolatileClasses && volatileClasses.indexOf(className) > -1) {
334-
return Promise.resolve(this.data[className]);
335-
}
336-
return this._dbAdapter.getClass(className)
337-
.then(injectDefaultSchema)
338-
.then((result) => {
339-
this._cache.setOneSchema(className, result);
340-
return result;
341-
})
330+
return this._cache.getOneSchema(className).then((cached) => {
331+
if (cached && !clearCache) {
332+
return Promise.resolve(cached);
333+
}
334+
if (allowVolatileClasses && volatileClasses.indexOf(className) > -1) {
335+
return Promise.resolve(this.data[className]);
336+
}
337+
return this._dbAdapter.getClass(className)
338+
.then(injectDefaultSchema)
339+
.then((result) => {
340+
this._cache.setOneSchema(className, result);
341+
return result;
342+
});
343+
});
342344
}
343345

344346
// Create a new class that includes the three default fields.
@@ -357,7 +359,7 @@ class SchemaController {
357359
return this._dbAdapter.createClass(className, convertSchemaToAdapterSchema({ fields, classLevelPermissions, className }))
358360
.then(convertAdapterSchemaToParseSchema)
359361
.then((res) => {
360-
this._cache.reset();
362+
this._cache.clear();
361363
return res;
362364
})
363365
.catch(error => {
@@ -569,7 +571,7 @@ class SchemaController {
569571
throw new Parse.Error(Parse.Error.INVALID_JSON, `Could not add field ${fieldName}`);
570572
}
571573
// Remove the cached schema
572-
this._cache.reset();
574+
this._cache.clear();
573575
return this;
574576
});
575577
});
@@ -613,7 +615,7 @@ class SchemaController {
613615
}
614616
return database.adapter.deleteFields(className, schema, [fieldName]);
615617
}).then(() => {
616-
this._cache.reset();
618+
this._cache.clear();
617619
});
618620
}
619621

src/ParseServer.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,7 @@ class ParseServer {
194194
const userController = new UserController(emailControllerAdapter, appId, { verifyUserEmails });
195195
const liveQueryController = new LiveQueryController(liveQuery);
196196
const cacheController = new CacheController(cacheControllerAdapter, appId);
197-
const schemaCache = new SchemaCache(appId, schemaCacheTTL);
198-
const databaseController = new DatabaseController(databaseAdapter, schemaCache);
197+
const databaseController = new DatabaseController(databaseAdapter, new SchemaCache(schemaCacheTTL));
199198
const hooksController = new HooksController(appId, databaseController, webhookKey);
200199

201200
// TODO: create indexes on first creation of a _User object. Otherwise it's impossible to
@@ -247,7 +246,6 @@ class ParseServer {
247246
expireInactiveSessions: expireInactiveSessions,
248247
revokeSessionOnPasswordReset,
249248
databaseController,
250-
schemaCache,
251249
schemaCacheTTL
252250
});
253251

0 commit comments

Comments
 (0)