From fc54e6caa5e504d677a56b25651da08f0ebd874b Mon Sep 17 00:00:00 2001 From: Aleksey Kvak Date: Fri, 31 Jul 2020 21:06:52 +0300 Subject: [PATCH] feat: make cachalot free from dependencies --- package-lock.json | 15 +++++---------- package.json | 7 ++----- src/Cache.ts | 9 ++++----- src/storage/BaseStorage.spec.ts | 14 ++++++++++++++ src/storage/BaseStorage.ts | 19 ++++++++++++++----- 5 files changed, 39 insertions(+), 25 deletions(-) diff --git a/package-lock.json b/package-lock.json index c0b8d75..e927748 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1801,12 +1801,6 @@ "integrity": "sha512-7+2BITlgjgDhH0vvwZU/HZJVyk+2XUlvxXe8dFMedNX/aMkaOq++rMAFXc0tM7ij15QaWlbdQASBR9dihi+bDQ==", "dev": true }, - "@types/lodash": { - "version": "4.14.157", - "resolved": "https://registry.npmjs.org/@types/lodash/-/lodash-4.14.157.tgz", - "integrity": "sha512-Ft5BNFmv2pHDgxV5JDsndOWTRJ+56zte0ZpYLowp03tW+K+t8u8YMOzAnpuqPgzX6WO1XpDIUm7u04M8vdDiVQ==", - "dev": true - }, "@types/memcached": { "version": "2.2.6", "resolved": "https://registry.npmjs.org/@types/memcached/-/memcached-2.2.6.tgz", @@ -7054,7 +7048,8 @@ "lodash": { "version": "4.17.19", "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.19.tgz", - "integrity": "sha512-JNvd8XER9GQX0v2qJgsaN/mzFCNA5BRe/j8JN9d+tWyGLSodKQHKFicdwNYzWwI3wjRnaKPsGj1XkBjx/F96DQ==" + "integrity": "sha512-JNvd8XER9GQX0v2qJgsaN/mzFCNA5BRe/j8JN9d+tWyGLSodKQHKFicdwNYzWwI3wjRnaKPsGj1XkBjx/F96DQ==", + "dev": true }, "lodash.capitalize": { "version": "4.2.1", @@ -13351,9 +13346,9 @@ } }, "typescript": { - "version": "3.9.5", - "resolved": "https://registry.npmjs.org/typescript/-/typescript-3.9.5.tgz", - "integrity": "sha512-hSAifV3k+i6lEoCJ2k6R2Z/rp/H3+8sdmcn5NrS3/3kE7+RyZXm9aqvxWqjEXHAd8b0pShatpcdMTvEdvAJltQ==", + "version": "3.9.7", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-3.9.7.tgz", + "integrity": "sha512-BLbiRkiBzAwsjut4x/dsibSTB6yWpwT5qWmC2OfuCg3GgVQCSgMs4vEctYPhsaGtd0AeuuHMkjZ2h2WG8MSzRw==", "dev": true }, "uglify-js": { diff --git a/package.json b/package.json index 3f0420f..22b9260 100644 --- a/package.json +++ b/package.json @@ -37,15 +37,12 @@ "prepublishOnly": "npm run build", "semantic-release": "semantic-release" }, - "dependencies": { - "lodash": "^4.17.15" - }, + "dependencies": {}, "devDependencies": { "@semantic-release/changelog": "^3.0.4", "@semantic-release/git": "^7.0.16", "@types/ioredis": "^4.16.7", "@types/jest": "^25.2.3", - "@types/lodash": "^4.14.157", "@types/memcached": "^2.2.6", "@types/node": "^8.10.61", "@types/uuid": "^8.0.0", @@ -61,7 +58,7 @@ "prettier": "^1.19.1", "semantic-release": "^15.13.24", "ts-jest": "^25.5.1", - "typescript": "^3.9.5", + "typescript": "^3.9.7", "uuid": "^8.2.0" }, "config": { diff --git a/src/Cache.ts b/src/Cache.ts index ede4baf..d9c2a80 100644 --- a/src/Cache.ts +++ b/src/Cache.ts @@ -1,4 +1,3 @@ -import defaultsDeep from "lodash/defaultsDeep"; import { ConnectionStatus } from "./ConnectionStatus"; import { Executor, runExecutor } from "./Executor"; import { Logger } from "./Logger"; @@ -113,8 +112,8 @@ class Cache { return runExecutor(executor); } - const { manager: managerName = RefreshAheadManager.getName() } = options; - const computedOptions = defaultsDeep({}, options, { expiresIn: this.expiresIn }); + const { manager: managerName = RefreshAheadManager.getName(), expiresIn = this.expiresIn } = options; + const computedOptions = { ...options, expiresIn }; const manager = this.getManager(managerName); return manager.get(key, executor, computedOptions); @@ -128,8 +127,8 @@ class Cache { value: R, options: WriteOptions & ManagerSelectorOptions = {} ): Promise> { - const { manager: managerName = RefreshAheadManager.getName() } = options; - const computedOptions = defaultsDeep({}, options, { expiresIn: this.expiresIn }); + const { manager: managerName = RefreshAheadManager.getName(), expiresIn = this.expiresIn } = options; + const computedOptions = { ...options, expiresIn }; const manager = this.getManager(managerName); return manager.set(key, value, computedOptions); diff --git a/src/storage/BaseStorage.spec.ts b/src/storage/BaseStorage.spec.ts index aba8394..0697f3d 100644 --- a/src/storage/BaseStorage.spec.ts +++ b/src/storage/BaseStorage.spec.ts @@ -428,4 +428,18 @@ describe("BaseStorage", () => { const record = new Record("test", "value", []); await expect(storage.isOutdated(record)).resolves.toEqual(false); }); + + it("isOutdated returns false if no actual tags", async () => { + storage.getTags = jest.fn().mockResolvedValue([]); + + const record = new Record("test", "value", [{ name: "tag1", version: 1 }]); + await expect(storage.isOutdated(record)).resolves.toEqual(false); + }); + + it("isOutdated returns true if there is new actual tag", async () => { + storage.getTags = jest.fn().mockResolvedValue([]); + + const record = new Record("test", "value", [{ name: "tag1", version: 1 }]); + await expect(storage.isOutdated(record)).resolves.toEqual(false); + }); }); diff --git a/src/storage/BaseStorage.ts b/src/storage/BaseStorage.ts index b049c95..3f8f081 100644 --- a/src/storage/BaseStorage.ts +++ b/src/storage/BaseStorage.ts @@ -5,7 +5,6 @@ import { isOperationTimeoutError } from "../errors/errors"; import { Storage, Tag, WriteOptions } from "./Storage"; import { StorageAdapter } from "../StorageAdapter"; import { Record } from "./Record"; -import differenceWith from "lodash/differenceWith"; export const TAGS_VERSIONS_ALIAS = "cache-tags-versions"; @@ -13,6 +12,13 @@ function uniq(arr: T[]): T[] { return [...new Set(arr)]; } +function tagVersionByName(tags: Tag[]): { [key: string]: number } { + return tags.reduce<{ [key: string]: number }>((acc, tag) => { + acc[tag.name] = tag.version; + return acc; + }, {}); +} + export type BaseStorageOptions = { adapter: StorageAdapter; tagsAdapter?: StorageAdapter; @@ -211,12 +217,15 @@ export class BaseStorage implements Storage { return true; } - const isTagOutdatedComparator = (recordTag: Tag, actualTag: Tag): boolean => - recordTag.name === actualTag.name && recordTag.version >= actualTag.version; + // if no tag touched, record is not outdated + if (actualTags.length === 0) { + return false; + } - const diff = differenceWith(record.tags, actualTags, isTagOutdatedComparator); + const recordTags = tagVersionByName(record.tags); - return diff.length !== 0; + // at least one actualTag should have greater version + return actualTags.some(actualTag => actualTag.version > recordTags[actualTag.name]); } return false;