Skip to content

Commit

Permalink
feat: make cachalot free from dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
alexkvak committed Jul 31, 2020
1 parent d264c1a commit fc54e6c
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 25 deletions.
15 changes: 5 additions & 10 deletions package-lock.json

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

7 changes: 2 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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": {
Expand Down
9 changes: 4 additions & 5 deletions src/Cache.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import defaultsDeep from "lodash/defaultsDeep";
import { ConnectionStatus } from "./ConnectionStatus";
import { Executor, runExecutor } from "./Executor";
import { Logger } from "./Logger";
Expand Down Expand Up @@ -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);
Expand All @@ -128,8 +127,8 @@ class Cache {
value: R,
options: WriteOptions<R> & ManagerSelectorOptions = {}
): Promise<Record<R>> {
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);
Expand Down
14 changes: 14 additions & 0 deletions src/storage/BaseStorage.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
19 changes: 14 additions & 5 deletions src/storage/BaseStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,20 @@ 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";

function uniq<T>(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;
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit fc54e6c

Please sign in to comment.