Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(lru-cache): add typed-array based LRUCache implementation #2406

Closed
wants to merge 4 commits into from
Closed
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
5 changes: 5 additions & 0 deletions packages/lru-cache/AmazonChangeLog.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
The Open Source MIT licensed project is at: https://github.com/Yomguithereal/mnemonist

High level modifications:
- The source code is written in TypeScript.
- An option is added to delete a cache key.
36 changes: 36 additions & 0 deletions packages/lru-cache/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
The MIT License (MIT)

Copyright (c) 2016 Guillaume Plique (Yomguithereal)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
15 changes: 15 additions & 0 deletions packages/lru-cache/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# @aws-sdk/lru-cache

[![NPM version](https://img.shields.io/npm/v/@aws-sdk/lru-cache/latest.svg)](https://www.npmjs.com/package/@aws-sdk/lru-cache)
[![NPM downloads](https://img.shields.io/npm/dm/@aws-sdk/lru-cache.svg)](https://www.npmjs.com/package/@aws-sdk/lru-cache)

> An internal package

## Usage

You probably shouldn't, at least directly.

This is a fork of LRUCache in [mnemonist](https://github.com/Yomguithereal/mnemonist) with high level modifications:

- The source code is written in TypeScript.
- An option is added to delete a cache key.
5 changes: 5 additions & 0 deletions packages/lru-cache/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const base = require("../../jest.config.base.js");

module.exports = {
...base,
};
44 changes: 44 additions & 0 deletions packages/lru-cache/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"name": "@aws-sdk/lru-cache",
"version": "3.0.0",
"scripts": {
"prepublishOnly": "yarn build && downlevel-dts dist/types dist/types/ts3.4",
"build:cjs": "tsc -p tsconfig.cjs.json",
"build:es": "tsc -p tsconfig.es.json",
"build": "yarn build:es && yarn build:cjs",
"test": "jest --passWithNoTests"
},
"author": {
"name": "AWS SDK for JavaScript Team",
"url": "https://aws.amazon.com/javascript/"
},
"license": "MIT",
"main": "./dist/cjs/index.js",
"module": "./dist/es/index.js",
"types": "./dist/types/index.d.ts",
"dependencies": {
"tslib": "^2.0.0"
},
"devDependencies": {
"@types/jest": "^26.0.4",
"@types/node": "^10.0.0",
"jest": "^26.1.0",
"typescript": "~4.2.4"
},
"engines": {
"node": ">= 10.0.0"
},
"typesVersions": {
"<4.0": {
"types/*": [
"types/ts3.4/*"
]
}
},
"homepage": "https://github.com/aws/aws-sdk-js-v3/tree/main/packages/lru-cache",
"repository": {
"type": "git",
"url": "https://github.com/aws/aws-sdk-js-v3.git",
"directory": "packages/lru-cache"
}
}
149 changes: 149 additions & 0 deletions packages/lru-cache/src/LRUCache.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
import { LRUCache } from "./LRUCache";
import { getPointerArrayConstructor } from "./utils/getPointerArrayConstructor";

jest.mock("./utils/getPointerArrayConstructor");

describe(LRUCache.name, () => {
beforeEach(() => {
(getPointerArrayConstructor as jest.Mock).mockReturnValue(Array);
});

afterEach(() => {
jest.clearAllMocks();
});

describe("should throw if given an invalid capacity", () => {
[undefined, {}, -1, true, 1.01, Infinity].forEach((capacity) => {
it(`invalid capacity: ${capacity}`, () => {
expect(() => {
// @ts-ignore
new LRUCache(capacity);
}).toThrow("@aws-sdk/lru-cache: capacity should be a finite positive integer.");
});
});
});

describe("should create LRUCache for valid capacity", () => {
it.each([1, 5, 1000, Math.pow(2, 8) - 1, Math.pow(2, 16) - 1, Math.pow(2, 32) - 1])(
"valid capacity: %d",
(capacity) => {
const cache = new LRUCache<string, number>(capacity);
expect(cache.capacity).toStrictEqual(capacity);
}
);
});

it("drops least recently used value when LRUCache is full", () => {
const cache = new LRUCache<string, number>(2);
expect(cache.capacity).toStrictEqual(2);

cache.set("one", 1);
expect(cache.size).toStrictEqual(1);
expect(cache.has("one")).toStrictEqual(true);
expect(cache.has("two")).toStrictEqual(false);
expect(cache.has("three")).toStrictEqual(false);

cache.set("two", 2);
expect(cache.size).toStrictEqual(2);
expect(cache.has("one")).toStrictEqual(true);
expect(cache.has("two")).toStrictEqual(true);
expect(cache.has("three")).toStrictEqual(false);

cache.set("three", 3);
expect(cache.size).toStrictEqual(2);
expect(cache.has("one")).toStrictEqual(false);
expect(cache.has("two")).toStrictEqual(true);
expect(cache.has("three")).toStrictEqual(true);
});

it("clears LRUCache", () => {
const cache = new LRUCache<string, number>(2);
expect(cache.capacity).toStrictEqual(2);

cache.set("one", 1);
cache.set("two", 2);
expect(cache.size).toStrictEqual(2);
expect(cache.has("one")).toStrictEqual(true);
expect(cache.has("two")).toStrictEqual(true);

cache.clear();
expect(cache.size).toStrictEqual(0);
expect(cache.has("one")).toStrictEqual(false);
expect(cache.has("two")).toStrictEqual(false);
});

it("peek does not modify ordering of the key", () => {
const cache = new LRUCache<string, number>(2);
expect(cache.capacity).toStrictEqual(2);

cache.set("one", 1);
cache.set("two", 2);

// Does not modify ordering of the key, and will be evicted when cache is full.
cache.peek("one");

cache.set("three", 3);
expect(cache.has("one")).toStrictEqual(false);
expect(cache.has("two")).toStrictEqual(true);
expect(cache.has("three")).toStrictEqual(true);
});

it("get splays key to the top", () => {
const cache = new LRUCache<string, number>(2);
expect(cache.capacity).toStrictEqual(2);

cache.set("one", 1);
cache.set("two", 2);

// Does modify ordering of the key, and "two" will be evicted when cache is full.
cache.get("one");

cache.set("three", 3);
expect(cache.has("one")).toStrictEqual(true);
expect(cache.has("two")).toStrictEqual(false);
expect(cache.has("three")).toStrictEqual(true);
});

it("delete removes key from cache", () => {
const cache = new LRUCache<string, number>(3);
expect(cache.capacity).toStrictEqual(3);

cache.set("one", 1);
cache.set("two", 2);
cache.set("three", 3);
expect(cache.size).toStrictEqual(3);
expect(cache.has("one")).toStrictEqual(true);
expect(cache.has("two")).toStrictEqual(true);
expect(cache.has("three")).toStrictEqual(true);

// Delete head
cache.delete("three");
expect(cache.size).toStrictEqual(2);
expect(cache.has("three")).toStrictEqual(false);

cache.set("three", 3);
expect(cache.has("three")).toStrictEqual(true);

// Delete node which is neither head or tail
cache.delete("two");
expect(cache.size).toStrictEqual(2);
expect(cache.has("two")).toStrictEqual(false);

// Delete tail
cache.delete("one");
expect(cache.size).toStrictEqual(1);
expect(cache.has("one")).toStrictEqual(false);

// Delete the only key
cache.delete("three");
expect(cache.size).toStrictEqual(0);

cache.set("one", 1);
cache.set("two", 2);
cache.set("three", 3);
expect(cache.size).toStrictEqual(3);
expect(cache.has("one")).toStrictEqual(true);
expect(cache.has("two")).toStrictEqual(true);
expect(cache.has("three")).toStrictEqual(true);
});
});
Loading