Skip to content

Commit

Permalink
refactor: Replace LRU Dependency With Custom Implementation (#1659)
Browse files Browse the repository at this point in the history
* build: remove `lru-cache`

* feat: Add Custom LRU

* fix: remove log

* refactor: Use `Map` instead of Linked List

* chore: Move `LRU` to a `util.ts`

* chore: Rename for clarity

* test: Add `LRUCache` tests

* chore: use utility LRUCache

* fix: use manual Promise `setTimeout`

'timers/promises' is not available in Node 14

* chore: lint

* docs: LRUCache
  • Loading branch information
d-goog authored Oct 6, 2023
1 parent 37291d5 commit 311d6a1
Show file tree
Hide file tree
Showing 4 changed files with 181 additions and 6 deletions.
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,13 @@
"gaxios": "^6.0.0",
"gcp-metadata": "^6.0.0",
"gtoken": "^7.0.0",
"jws": "^4.0.0",
"lru-cache": "^6.0.0"
"jws": "^4.0.0"
},
"devDependencies": {
"@compodoc/compodoc": "^1.1.7",
"@types/base64-js": "^1.2.5",
"@types/chai": "^4.1.7",
"@types/jws": "^3.1.0",
"@types/lru-cache": "^5.0.0",
"@types/mocha": "^9.0.0",
"@types/mv": "^2.1.0",
"@types/ncp": "^2.0.1",
Expand Down
6 changes: 3 additions & 3 deletions src/auth/jwtaccess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
// limitations under the License.

import * as jws from 'jws';
import * as LRU from 'lru-cache';
import * as stream from 'stream';

import {JWTInput} from './credentials';
import {Headers} from './oauth2client';
import {LRUCache} from '../util';

const DEFAULT_HEADER: jws.Header = {
alg: 'RS256',
Expand All @@ -35,8 +35,8 @@ export class JWTAccess {
projectId?: string;
eagerRefreshThresholdMillis: number;

private cache = new LRU<string, {expiration: number; headers: Headers}>({
max: 500,
private cache = new LRUCache<{expiration: number; headers: Headers}>({
capacity: 500,
maxAge: 60 * 60 * 1000,
});

Expand Down
110 changes: 110 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

export interface LRUCacheOptions {
/**
* The maximum number of items to cache.
*/
capacity: number;
/**
* An optional max age for items in milliseconds.
*/
maxAge?: number;
}

/**
* A simple LRU cache utility.
* Not meant for external usage.
*
* @experimental
* @internal
*/
export class LRUCache<T> {
readonly capacity: number;

/**
* Maps are in order. Thus, the older item is the first item.
*
* {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map}
*/
#cache = new Map<string, {lastAccessed: number; value: T}>();
maxAge?: number;

constructor(options: LRUCacheOptions) {
this.capacity = options.capacity;
this.maxAge = options.maxAge;
}

/**
* Moves the key to the end of the cache.
*
* @param key the key to move
* @param value the value of the key
*/
#moveToEnd(key: string, value: T) {
this.#cache.delete(key);
this.#cache.set(key, {
value,
lastAccessed: Date.now(),
});
}

/**
* Add an item to the cache.
*
* @param key the key to upsert
* @param value the value of the key
*/
set(key: string, value: T) {
this.#moveToEnd(key, value);
this.#evict();
}

/**
* Get an item from the cache.
*
* @param key the key to retrieve
*/
get(key: string): T | undefined {
const item = this.#cache.get(key);
if (!item) return;

this.#moveToEnd(key, item.value);
this.#evict();

return item.value;
}

/**
* Maintain the cache based on capacity and TTL.
*/
#evict() {
const cutoffDate = this.maxAge ? Date.now() - this.maxAge : 0;

/**
* Because we know Maps are in order, this item is both the
* last item in the list (capacity) and oldest (maxAge).
*/
let oldestItem = this.#cache.entries().next();

while (
!oldestItem.done &&
(this.#cache.size > this.capacity || // too many
oldestItem.value[1].lastAccessed < cutoffDate) // too old
) {
this.#cache.delete(oldestItem.value[0]);
oldestItem = this.#cache.entries().next();
}
}
}
67 changes: 67 additions & 0 deletions test/test.util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import {strict as assert} from 'assert';

import {LRUCache} from '../src/util';

describe('util', () => {
describe('LRUCache', () => {
it('should set and get a cached item', () => {
const expected = 'value';
const lru = new LRUCache({capacity: 5});
lru.set('sample', expected);

assert.equal(lru.get('sample'), expected);
});

it('should evict oldest items when over capacity', () => {
const capacity = 5;
const overCapacity = 2;

const lru = new LRUCache({capacity});

for (let i = 0; i < capacity + overCapacity; i++) {
lru.set(`${i}`, i);
}

// the first few shouldn't be there
for (let i = 0; i < overCapacity; i++) {
assert.equal(lru.get(`${i}`), undefined);
}

// the rest should be there
for (let i = overCapacity; i < capacity + overCapacity; i++) {
assert.equal(lru.get(`${i}`), i);
}
});

it('should evict items older than a supplied `maxAge`', async () => {
const maxAge = 50;

const lru = new LRUCache({capacity: 5, maxAge});

lru.set('first', 1);
lru.set('second', 2);

await new Promise(res => setTimeout(res, maxAge + 1));

lru.set('third', 3);

assert.equal(lru.get('first'), undefined);
assert.equal(lru.get('second'), undefined);
assert.equal(lru.get('third'), 3);
});
});
});

0 comments on commit 311d6a1

Please sign in to comment.