Skip to content

Commit 10efd5d

Browse files
committed
Merge pull request #694 from ParsePlatform/nlutsenko.cache
Unify and cleanup cache.js.
2 parents 8883541 + 768a781 commit 10efd5d

File tree

11 files changed

+55
-77
lines changed

11 files changed

+55
-77
lines changed

spec/helper.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,13 @@ delete defaultConfiguration.cloud;
5252

5353
// Allows testing specific configurations of Parse Server
5454
var setServerConfiguration = configuration => {
55-
api = new ParseServer(configuration);
55+
server.close();
56+
cache.clearCache();
5657
app = express();
58+
api = new ParseServer(configuration);
5759
app.use('/1', api);
58-
cache.clearCache();
59-
server.close();
6060
server = app.listen(port);
61-
}
61+
};
6262

6363
var restoreServerConfiguration = () => setServerConfiguration(defaultConfiguration);
6464

src/Auth.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ function nobody(config) {
4343

4444
// Returns a promise that resolves to an Auth object
4545
var getAuthForSessionToken = function(config, sessionToken) {
46-
var cachedUser = cache.getUser(sessionToken);
46+
var cachedUser = cache.users.get(sessionToken);
4747
if (cachedUser) {
4848
return Promise.resolve(new Auth(config, false, cachedUser));
4949
}
@@ -65,8 +65,8 @@ var getAuthForSessionToken = function(config, sessionToken) {
6565
delete obj.password;
6666
obj['className'] = '_User';
6767
obj['sessionToken'] = sessionToken;
68-
var userObject = Parse.Object.fromJSON(obj);
69-
cache.setUser(sessionToken, userObject);
68+
let userObject = Parse.Object.fromJSON(obj);
69+
cache.users.set(sessionToken, userObject);
7070
return new Auth(config, false, userObject);
7171
});
7272
};

src/Config.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,12 @@ import cache from './cache';
77
export class Config {
88
constructor(applicationId: string, mount: string) {
99
let DatabaseAdapter = require('./DatabaseAdapter');
10-
11-
let cacheInfo = cache.apps[applicationId];
12-
this.valid = !!cacheInfo;
13-
if (!this.valid) {
10+
let cacheInfo = cache.apps.get(applicationId);
11+
if (!cacheInfo) {
1412
return;
1513
}
1614

1715
this.applicationId = applicationId;
18-
this.collectionPrefix = cacheInfo.collectionPrefix || '';
1916
this.masterKey = cacheInfo.masterKey;
2017
this.clientKey = cacheInfo.clientKey;
2118
this.javascriptKey = cacheInfo.javascriptKey;
@@ -25,7 +22,7 @@ export class Config {
2522
this.facebookAppIds = cacheInfo.facebookAppIds;
2623
this.enableAnonymousUsers = cacheInfo.enableAnonymousUsers;
2724
this.allowClientClassCreation = cacheInfo.allowClientClassCreation;
28-
this.database = DatabaseAdapter.getDatabaseConnection(applicationId, this.collectionPrefix);
25+
this.database = DatabaseAdapter.getDatabaseConnection(applicationId, cacheInfo.collectionPrefix);
2926
this.hooksController = cacheInfo.hooksController;
3027
this.filesController = cacheInfo.filesController;
3128
this.pushController = cacheInfo.pushController;

src/Routers/UsersRouter.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ export class UsersRouter extends ClassesRouter {
7575
}
7676

7777
let user;
78-
return req.database.find('_User', { username: req.body.username })
78+
return req.config.database.find('_User', { username: req.body.username })
7979
.then((results) => {
8080
if (!results.length) {
8181
throw new Parse.Error(Parse.Error.OBJECT_NOT_FOUND, 'Invalid username/password.');

src/cache.js

Lines changed: 24 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,35 @@
1-
export var apps = {};
2-
export var stats = {};
3-
export var isLoaded = false;
4-
export var users = {};
1+
/** @flow weak */
52

6-
export function getApp(app, callback) {
7-
if (apps[app]) return callback(true, apps[app]);
8-
return callback(false);
3+
export function CacheStore<KeyType, ValueType>() {
4+
let dataStore: {[id:KeyType]:ValueType} = {};
5+
return {
6+
get: (key: KeyType): ValueType => {
7+
return dataStore[key];
8+
},
9+
set(key: KeyType, value: ValueType): void {
10+
dataStore[key] = value;
11+
},
12+
remove(key: KeyType): void {
13+
delete dataStore[key];
14+
},
15+
clear(): void {
16+
dataStore = {};
17+
}
18+
};
919
}
1020

11-
export function updateStat(key, value) {
12-
stats[key] = value;
13-
}
14-
15-
export function getUser(sessionToken) {
16-
if (users[sessionToken]) return users[sessionToken];
17-
return undefined;
18-
}
19-
20-
export function setUser(sessionToken, userObject) {
21-
users[sessionToken] = userObject;
22-
}
23-
24-
export function clearUser(sessionToken) {
25-
delete users[sessionToken];
26-
}
21+
const apps = CacheStore();
22+
const users = CacheStore();
2723

2824
//So far used only in tests
29-
export function clearCache() {
30-
apps = {};
31-
stats = {};
32-
users = {};
25+
export function clearCache(): void {
26+
apps.clear();
27+
users.clear();
3328
}
3429

3530
export default {
3631
apps,
37-
stats,
38-
isLoaded,
39-
getApp,
40-
updateStat,
41-
clearUser,
42-
getUser,
43-
setUser,
32+
users,
4433
clearCache,
34+
CacheStore
4535
};

src/index.js

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ function ParseServer({
126126
const loggerController = new LoggerController(loggerControllerAdapter);
127127
const hooksController = new HooksController(appId, collectionPrefix);
128128

129-
cache.apps[appId] = {
129+
cache.apps.set(appId, {
130130
masterKey: masterKey,
131131
collectionPrefix: collectionPrefix,
132132
clientKey: clientKey,
@@ -142,11 +142,11 @@ function ParseServer({
142142
enableAnonymousUsers: enableAnonymousUsers,
143143
allowClientClassCreation: allowClientClassCreation,
144144
oauth: oauth
145-
};
145+
});
146146

147147
// To maintain compatibility. TODO: Remove in v2.1
148148
if (process.env.FACEBOOK_APP_ID) {
149-
cache.apps[appId]['facebookAppIds'].push(process.env.FACEBOOK_APP_ID);
149+
cache.apps.get(appId)['facebookAppIds'].push(process.env.FACEBOOK_APP_ID);
150150
}
151151

152152
// This app serves the Parse API directly.
@@ -220,13 +220,6 @@ function addParseCloud() {
220220
global.Parse = Parse;
221221
}
222222

223-
function getClassName(parseClass) {
224-
if (parseClass && parseClass.className) {
225-
return parseClass.className;
226-
}
227-
return parseClass;
228-
}
229-
230223
module.exports = {
231224
ParseServer: ParseServer,
232225
S3Adapter: S3Adapter

src/middlewares.js

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ function handleParseHeaders(req, res, next) {
3535

3636
var fileViaJSON = false;
3737

38-
if (!info.appId || !cache.apps[info.appId]) {
38+
if (!info.appId || !cache.apps.get(info.appId)) {
3939
// See if we can find the app id on the body.
4040
if (req.body instanceof Buffer) {
4141
// The only chance to find the app id is if this is a file
@@ -44,12 +44,10 @@ function handleParseHeaders(req, res, next) {
4444
fileViaJSON = true;
4545
}
4646

47-
if (req.body && req.body._ApplicationId
48-
&& cache.apps[req.body._ApplicationId]
49-
&& (
50-
!info.masterKey
51-
||
52-
cache.apps[req.body._ApplicationId]['masterKey'] === info.masterKey)
47+
if (req.body &&
48+
req.body._ApplicationId &&
49+
cache.apps.get(req.body._ApplicationId) &&
50+
(!info.masterKey || cache.apps.get(req.body._ApplicationId).masterKey === info.masterKey)
5351
) {
5452
info.appId = req.body._ApplicationId;
5553
info.javascriptKey = req.body._JavaScriptKey || '';
@@ -84,9 +82,8 @@ function handleParseHeaders(req, res, next) {
8482
req.body = new Buffer(base64, 'base64');
8583
}
8684

87-
info.app = cache.apps[info.appId];
85+
info.app = cache.apps.get(info.appId);
8886
req.config = new Config(info.appId, mount);
89-
req.database = req.config.database;
9087
req.info = info;
9188

9289
var isMaster = (info.masterKey === req.config.masterKey);

src/requiredParameter.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
/* @flow */
2-
export default (errorMessage: string) => {throw errorMessage}
1+
/** @flow */
2+
export default (errorMessage: string): any => { throw errorMessage }

src/rest.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ function del(config, auth, className, objectId) {
4646
.then((response) => {
4747
if (response && response.results && response.results.length) {
4848
response.results[0].className = className;
49-
cache.clearUser(response.results[0].sessionToken);
49+
cache.users.remove(response.results[0].sessionToken);
5050
inflatedObject = Parse.Object.fromJSON(response.results[0]);
5151
return triggers.maybeRunTrigger(triggers.Types.beforeDelete, auth, inflatedObject, null, config.applicationId);
5252
}

src/testing-routes.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@ var router = express.Router();
1010
// creates a unique app in the cache, with a collection prefix
1111
function createApp(req, res) {
1212
var appId = cryptoUtils.randomHexString(32);
13-
cache.apps[appId] = {
13+
// TODO: (nlutsenko) This doesn't work and should die, since there are no controllers on this configuration.
14+
cache.apps.set(appId, {
1415
'collectionPrefix': appId + '_',
1516
'masterKey': 'master'
16-
};
17+
});
1718
var keys = {
1819
'application_id': appId,
1920
'client_key': 'unused',
@@ -31,7 +32,7 @@ function clearApp(req, res) {
3132
if (!req.auth.isMaster) {
3233
return res.status(401).send({"error": "unauthorized"});
3334
}
34-
req.database.deleteEverything().then(() => {
35+
return req.config.database.deleteEverything().then(() => {
3536
res.status(200).send({});
3637
});
3738
}
@@ -41,8 +42,8 @@ function dropApp(req, res) {
4142
if (!req.auth.isMaster) {
4243
return res.status(401).send({"error": "unauthorized"});
4344
}
44-
req.database.deleteEverything().then(() => {
45-
delete cache.apps[req.config.applicationId];
45+
return req.config.database.deleteEverything().then(() => {
46+
cache.apps.remove(req.config.applicationId);
4647
res.status(200).send({});
4748
});
4849
}

0 commit comments

Comments
 (0)