Skip to content

Commit

Permalink
refactor(cache/package): Enable strict checks (#12862)
Browse files Browse the repository at this point in the history
Co-authored-by: Rhys Arkins <rhys@arkins.net>
  • Loading branch information
zharinov and rarkins committed Nov 27, 2021
1 parent b90aeda commit ef51e85
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 15 deletions.
9 changes: 6 additions & 3 deletions lib/util/cache/package/decorator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ describe('util/cache/package/decorator', () => {
it('Do cache null', async () => {
class MyClass {
@cache({ namespace: 'namespace', key: (cacheKey, test) => cacheKey })
public async getString(cacheKey: string, test: string): Promise<string> {
public async getString(
cacheKey: string,
test: string | null
): Promise<string | null> {
await spy();
return test;
}
Expand All @@ -52,7 +55,7 @@ describe('util/cache/package/decorator', () => {
it('Do not cache undefined', async () => {
class MyClass {
@cache({ namespace: 'namespace', key: 'undefined' })
public async getString(): Promise<string> {
public async getString(): Promise<string | undefined> {
await spy();
return undefined;
}
Expand All @@ -66,7 +69,7 @@ describe('util/cache/package/decorator', () => {
it('should cache function', async () => {
class MyClass {
@cache({
namespace: (arg: GetReleasesConfig) => arg.registryUrl,
namespace: (arg: GetReleasesConfig) => arg.registryUrl ?? 'default',
key: () => 'key',
})
public async getNumber(_: GetReleasesConfig): Promise<number> {
Expand Down
9 changes: 7 additions & 2 deletions lib/util/cache/package/decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,20 +101,25 @@ export function cache<T>({
return callback();
}

let finalNamespace: string;
let finalNamespace: string | undefined;
if (is.string(namespace)) {
finalNamespace = namespace;
} else if (is.function_(namespace)) {
finalNamespace = namespace.apply(instance, args);
}

let finalKey: string;
let finalKey: string | undefined;
if (is.string(key)) {
finalKey = key;
} else if (is.function_(key)) {
finalKey = key.apply(instance, args);
}

// istanbul ignore if
if (!finalNamespace || !finalKey) {
return callback();
}

const cachedResult = await packageCache.get<unknown>(
finalNamespace,
finalKey
Expand Down
2 changes: 1 addition & 1 deletion lib/util/cache/package/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ async function rm(namespace: string, key: string): Promise<void> {
export async function get<T = never>(
namespace: string,
key: string
): Promise<T> {
): Promise<T | undefined> {
if (!cacheFileName) {
return undefined;
}
Expand Down
16 changes: 10 additions & 6 deletions lib/util/cache/package/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,33 @@ function getGlobalKey(namespace: string, key: string): string {
return `global%%${namespace}%%${key}`;
}

export function get<T = any>(namespace: string, key: string): Promise<T> {
export async function get<T = any>(
namespace: string,
key: string
): Promise<T | undefined> {
if (!cacheProxy) {
return undefined;
}
const globalKey = getGlobalKey(namespace, key);
if (memCache.get(globalKey) === undefined) {
memCache.set(globalKey, cacheProxy.get(namespace, key));
}
return memCache.get(globalKey);
const result = await memCache.get(globalKey);
return result;
}

export function set(
export async function set(
namespace: string,
key: string,
value: unknown,
minutes: number
): Promise<void> {
if (!cacheProxy) {
return undefined;
return;
}
const globalKey = getGlobalKey(namespace, key);
memCache.set(globalKey, value);
return cacheProxy.set(namespace, key, value, minutes);
await cacheProxy.set(namespace, key, value, minutes);
}

export function init(config: AllConfig): void {
Expand All @@ -42,7 +46,7 @@ export function init(config: AllConfig): void {
get: redisCache.get,
set: redisCache.set,
};
} else {
} else if (config.cacheDir) {
fileCache.init(config.cacheDir);
cacheProxy = {
get: fileCache.get,
Expand Down
4 changes: 2 additions & 2 deletions lib/util/cache/package/redis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ async function rm(namespace: string, key: string): Promise<void> {
export async function get<T = never>(
namespace: string,
key: string
): Promise<T> {
): Promise<T | undefined> {
if (!client) {
return undefined;
}
logger.trace(`cache.get(${namespace}, ${key})`);
try {
const res = await client?.get(getKey(namespace, key));
const cachedValue = JSON.parse(res);
const cachedValue = res && JSON.parse(res);
if (cachedValue) {
if (DateTime.local() < DateTime.fromISO(cachedValue.expiry)) {
logger.trace({ namespace, key }, 'Returning cached value');
Expand Down
2 changes: 1 addition & 1 deletion lib/util/cache/package/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export interface PackageCache {
get<T = any>(namespace: string, key: string): Promise<T>;
get<T = any>(namespace: string, key: string): Promise<T | undefined>;

set<T = any>(
namespace: string,
Expand Down
4 changes: 4 additions & 0 deletions tsconfig.strict.json
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@
"./lib/types/versioning.ts",
"./lib/types/vulnerability-alert.ts",
"./lib/util/cache/memory/index.ts",
"./lib/util/cache/package/decorator.ts",
"./lib/util/cache/package/file.ts",
"./lib/util/cache/package/index.ts",
"./lib/util/cache/package/redis.ts",
"./lib/util/cache/package/types.ts",
"./lib/util/clone.ts",
"./lib/util/date.ts",
Expand Down

0 comments on commit ef51e85

Please sign in to comment.