Skip to content

Commit 95430bb

Browse files
authored
Use LRU cache as default mechanism for caching in memory (#3979)
* Use LRU cache as default mechanism for caching in memory * Return null as what’s expected
1 parent 29c2fad commit 95430bb

File tree

4 files changed

+40
-3
lines changed

4 files changed

+40
-3
lines changed

src/Adapters/Cache/InMemoryCacheAdapter.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import {InMemoryCache} from './InMemoryCache';
1+
import {LRUCache} from './LRUCache';
22

33
export class InMemoryCacheAdapter {
44

55
constructor(ctx) {
6-
this.cache = new InMemoryCache(ctx)
6+
this.cache = new LRUCache(ctx)
77
}
88

99
get(key) {

src/Adapters/Cache/LRUCache.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import LRU from 'lru-cache';
2+
import defaults from '../../defaults';
3+
4+
export class LRUCache {
5+
constructor({
6+
ttl = defaults.cacheTTL,
7+
maxSize = defaults.cacheMaxSize,
8+
}) {
9+
this.cache = new LRU({
10+
max: maxSize,
11+
maxAge: ttl
12+
});
13+
}
14+
15+
get(key) {
16+
return this.cache.get(key) || null;
17+
}
18+
19+
put(key, value, ttl = this.ttl) {
20+
this.cache.set(key, value, ttl);
21+
}
22+
23+
del(key) {
24+
this.cache.del(key);
25+
}
26+
27+
clear() {
28+
this.cache.reset();
29+
}
30+
31+
}
32+
33+
export default LRUCache;

src/ParseServer.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,8 @@ class ParseServer {
140140
expireInactiveSessions = defaults.expireInactiveSessions,
141141
revokeSessionOnPasswordReset = defaults.revokeSessionOnPasswordReset,
142142
schemaCacheTTL = defaults.schemaCacheTTL, // cache for 5s
143+
cacheTTL = defaults.cacheTTL, // cache for 5s
144+
cacheMaxSize = defaults.cacheMaxSize, // 10000
143145
enableSingleSchemaCache = false,
144146
objectIdSize = defaults.objectIdSize,
145147
__indexBuildCompletionCallbackForTests = () => {},
@@ -204,7 +206,7 @@ class ParseServer {
204206
const emailControllerAdapter = loadAdapter(emailAdapter);
205207
const userController = new UserController(emailControllerAdapter, appId, { verifyUserEmails });
206208

207-
const cacheControllerAdapter = loadAdapter(cacheAdapter, InMemoryCacheAdapter, {appId: appId});
209+
const cacheControllerAdapter = loadAdapter(cacheAdapter, InMemoryCacheAdapter, {appId: appId, ttl: cacheTTL, maxSize: cacheMaxSize });
208210
const cacheController = new CacheController(cacheControllerAdapter, appId);
209211

210212
const analyticsControllerAdapter = loadAdapter(analyticsAdapter, AnalyticsAdapter);

src/defaults.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ export default {
3232
expireInactiveSessions: true,
3333
revokeSessionOnPasswordReset: true,
3434
schemaCacheTTL: 5000, // in ms
35+
cacheTTL: 5000,
36+
cacheMaxSize: 10000,
3537
userSensitiveFields: ['email'],
3638
objectIdSize: 10
3739
}

0 commit comments

Comments
 (0)