Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"jwks-rsa": "3.2.0",
"ldapjs": "3.0.7",
"lodash": "4.18.1",
"lru-cache": "11.2.7",
"lru-cache": "11.5.2",
"mime": "4.1.0",
"mongodb": "7.1.0",
"mustache": "4.2.0",
Expand Down
48 changes: 48 additions & 0 deletions spec/InMemoryCacheAdapter.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,52 @@ describe('InMemoryCacheAdapter', function () {
.then(value => expect(value).toEqual(null))
.then(done);
});

it('should keep an entry whose ttl outlives the cache ttl', async () => {
const cache = new InMemoryCacheAdapter({ ttl: 10 });

await cache.put(KEY, VALUE, 5000);
await wait(50);

expect(await cache.get(KEY)).toEqual(VALUE);
});

it('should expire an entry whose ttl is shorter than the cache ttl', async () => {
const cache = new InMemoryCacheAdapter({ ttl: 5000 });

await cache.put(KEY, VALUE, 10);
expect(await cache.get(KEY)).toEqual(VALUE);
await wait(50);

expect(await cache.get(KEY)).toEqual(null);
});

it('should not expire an entry with an infinite ttl', async () => {
const cache = new InMemoryCacheAdapter({ ttl: 10 });

await cache.put(KEY, VALUE, Infinity);
await wait(50);

expect(await cache.get(KEY)).toEqual(VALUE);
});

it('should fall back to the cache ttl when the entry ttl is not a positive number', async () => {
const cache = new InMemoryCacheAdapter({ ttl: 10 });

await cache.put(KEY, VALUE, 'not a ttl');
expect(await cache.get(KEY)).toEqual(VALUE);
await wait(50);

expect(await cache.get(KEY)).toEqual(null);
});

it('should fall back to the cache ttl when no entry ttl is given', async () => {
const cache = new InMemoryCacheAdapter({ ttl: 10 });

await cache.put(KEY, VALUE);
expect(await cache.get(KEY)).toEqual(VALUE);
await wait(50);

expect(await cache.get(KEY)).toEqual(null);
});
});
25 changes: 24 additions & 1 deletion src/Adapters/Cache/LRUCache.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import { LRUCache as LRU } from 'lru-cache';
import defaults from '../../defaults';

const isValidTTL = ttl => typeof ttl === 'number' && Number.isFinite(ttl) && ttl > 0;

// `lru-cache` expresses "never expires" as a TTL of zero.
const NO_EXPIRY = 0;

export class LRUCache {
constructor({ ttl = defaults.cacheTTL, maxSize = defaults.cacheMaxSize }) {
this.ttl = ttl;
this.cache = new LRU({
max: maxSize,
ttl,
Expand All @@ -14,7 +20,24 @@ export class LRUCache {
}

put(key, value, ttl = this.ttl) {
this.cache.set(key, value, ttl);
// `lru-cache` takes the per-entry TTL as a property of an options object.
// Passed positionally it is silently discarded, leaving the entry on the
// cache-wide TTL.
//
// A TTL of `Infinity` means "never expires", matching `RedisCacheAdapter`,
// and is translated to the zero that `lru-cache` uses for that. `Infinity`
// is not forwarded as-is because Node cannot express it as a timer
// duration: under `ttlAutopurge` it emits a `TimeoutOverflowWarning` and
// clamps the timer to 1ms, so the purge timer re-fires every millisecond.
// Any other TTL that is not a positive, finite number is forwarded as
// `undefined`, which is how the library expresses "use the cache-wide TTL".
let entryTTL;
if (ttl === Infinity) {
entryTTL = NO_EXPIRY;
} else if (isValidTTL(ttl)) {
entryTTL = ttl;
}
this.cache.set(key, value, { ttl: entryTTL });
}

del(key) {
Expand Down
4 changes: 3 additions & 1 deletion src/LiveQuery/ParseLiveQueryServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -681,7 +681,9 @@ class ParseLiveQueryServer {
const result: any = {};
if (error && error.code === Parse.Error.INVALID_SESSION_TOKEN) {
result.error = error;
this.authCache.set(sessionToken, Promise.resolve(result), this.config.cacheTimeout);
this.authCache.set(sessionToken, Promise.resolve(result), {
ttl: this.config.cacheTimeout,
});
} else {
this.authCache.delete(sessionToken);
}
Expand Down
Loading