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
This commit is contained in:
@@ -1,9 +1,9 @@
|
||||
import {InMemoryCache} from './InMemoryCache';
|
||||
import {LRUCache} from './LRUCache';
|
||||
|
||||
export class InMemoryCacheAdapter {
|
||||
|
||||
constructor(ctx) {
|
||||
this.cache = new InMemoryCache(ctx)
|
||||
this.cache = new LRUCache(ctx)
|
||||
}
|
||||
|
||||
get(key) {
|
||||
|
||||
33
src/Adapters/Cache/LRUCache.js
Normal file
33
src/Adapters/Cache/LRUCache.js
Normal file
@@ -0,0 +1,33 @@
|
||||
import LRU from 'lru-cache';
|
||||
import defaults from '../../defaults';
|
||||
|
||||
export class LRUCache {
|
||||
constructor({
|
||||
ttl = defaults.cacheTTL,
|
||||
maxSize = defaults.cacheMaxSize,
|
||||
}) {
|
||||
this.cache = new LRU({
|
||||
max: maxSize,
|
||||
maxAge: ttl
|
||||
});
|
||||
}
|
||||
|
||||
get(key) {
|
||||
return this.cache.get(key) || null;
|
||||
}
|
||||
|
||||
put(key, value, ttl = this.ttl) {
|
||||
this.cache.set(key, value, ttl);
|
||||
}
|
||||
|
||||
del(key) {
|
||||
this.cache.del(key);
|
||||
}
|
||||
|
||||
clear() {
|
||||
this.cache.reset();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export default LRUCache;
|
||||
@@ -140,6 +140,8 @@ class ParseServer {
|
||||
expireInactiveSessions = defaults.expireInactiveSessions,
|
||||
revokeSessionOnPasswordReset = defaults.revokeSessionOnPasswordReset,
|
||||
schemaCacheTTL = defaults.schemaCacheTTL, // cache for 5s
|
||||
cacheTTL = defaults.cacheTTL, // cache for 5s
|
||||
cacheMaxSize = defaults.cacheMaxSize, // 10000
|
||||
enableSingleSchemaCache = false,
|
||||
objectIdSize = defaults.objectIdSize,
|
||||
__indexBuildCompletionCallbackForTests = () => {},
|
||||
@@ -204,7 +206,7 @@ class ParseServer {
|
||||
const emailControllerAdapter = loadAdapter(emailAdapter);
|
||||
const userController = new UserController(emailControllerAdapter, appId, { verifyUserEmails });
|
||||
|
||||
const cacheControllerAdapter = loadAdapter(cacheAdapter, InMemoryCacheAdapter, {appId: appId});
|
||||
const cacheControllerAdapter = loadAdapter(cacheAdapter, InMemoryCacheAdapter, {appId: appId, ttl: cacheTTL, maxSize: cacheMaxSize });
|
||||
const cacheController = new CacheController(cacheControllerAdapter, appId);
|
||||
|
||||
const analyticsControllerAdapter = loadAdapter(analyticsAdapter, AnalyticsAdapter);
|
||||
|
||||
@@ -32,6 +32,8 @@ export default {
|
||||
expireInactiveSessions: true,
|
||||
revokeSessionOnPasswordReset: true,
|
||||
schemaCacheTTL: 5000, // in ms
|
||||
cacheTTL: 5000,
|
||||
cacheMaxSize: 10000,
|
||||
userSensitiveFields: ['email'],
|
||||
objectIdSize: 10
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user