Skip to content

Commit

Permalink
refactor(cache): Make the namespace optional for the decorator
Browse files Browse the repository at this point in the history
  • Loading branch information
zharinov committed Aug 28, 2024
1 parent bb6fba2 commit 2466add
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 84 deletions.
22 changes: 22 additions & 0 deletions lib/util/cache/package/decorator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,28 @@ describe('util/cache/package/decorator', () => {
);
});

it('should use default namespace equal to class name', async () => {
class SomeClass {
@cache({ key: 'some-key' })
public fn(): Promise<string> {
return getValue();
}
}
const obj = new SomeClass();

expect(await obj.fn()).toBe('111');
expect(await obj.fn()).toBe('111');
expect(await obj.fn()).toBe('111');

expect(getValue).toHaveBeenCalledTimes(1);
expect(setCache).toHaveBeenCalledExactlyOnceWith(
'SomeClass',
'cache-decorator:some-key',
{ cachedAt: expect.any(String), value: '111' },
30,
);
});

it('disables cache if cacheability check is false', async () => {
class Class {
@cache({
Expand Down
14 changes: 13 additions & 1 deletion lib/util/cache/package/decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ interface CacheParameters {
/**
* The cache namespace
* Either a string or a hash function that generates a string
*
* @deprecated
*/
namespace: PackageCacheNamespace | NamespaceFunction;
namespace?: PackageCacheNamespace | NamespaceFunction;

/**
* The cache key
Expand All @@ -42,6 +44,14 @@ interface CacheParameters {
ttlMinutes?: number;
}

function getClassName(instance: unknown): string | undefined {
if (!is.object(instance)) {
return undefined;

Check warning on line 49 in lib/util/cache/package/decorator.ts

View check run for this annotation

Codecov / codecov/patch

lib/util/cache/package/decorator.ts#L49

Added line #L49 was not covered by tests
}

return instance.constructor.name;
}

/**
* caches the result of a decorated method.
*/
Expand All @@ -66,6 +76,8 @@ export function cache<T>({
finalNamespace = namespace;
} else if (is.function_(namespace)) {
finalNamespace = namespace.apply(instance, args);
} else {
finalNamespace = getClassName(instance);
}

let finalKey: string | undefined;
Expand Down
84 changes: 1 addition & 83 deletions lib/util/cache/package/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,88 +16,6 @@ export interface DecoratorCachedRecord {
cachedAt: string;
}

export type PackageCacheNamespace =
| '_test-namespace'
| 'changelog-bitbucket-notes@v2'
| 'changelog-bitbucket-release'
| 'changelog-gitea-notes@v2'
| 'changelog-gitea-release'
| 'changelog-github-notes@v2'
| 'changelog-github-release'
| 'changelog-gitlab-notes@v2'
| 'changelog-gitlab-release'
| 'datasource-artifactory'
| 'datasource-aws-machine-image'
| 'datasource-aws-rds'
| 'datasource-azure-bicep-resource'
| 'datasource-azure-pipelines-tasks'
| 'datasource-bazel'
| 'datasource-bitbucket-tags'
| 'datasource-bitrise'
| 'datasource-cdnjs'
| 'datasource-conan'
| 'datasource-conda'
| 'datasource-cpan'
| 'datasource-crate-metadata'
| 'datasource-crate'
| 'datasource-deno'
| 'datasource-docker-architecture'
| 'datasource-docker-hub-cache'
| 'datasource-docker-digest'
| 'datasource-docker-hub-tags'
| 'datasource-docker-imageconfig'
| 'datasource-docker-labels'
| 'datasource-docker-releases-v2'
| 'datasource-docker-tags'
| 'datasource-dotnet-version'
| 'datasource-endoflife-date'
| 'datasource-galaxy-collection'
| 'datasource-galaxy'
| 'datasource-git-refs'
| 'datasource-git-tags'
| 'datasource-git'
| 'datasource-gitea-releases'
| 'datasource-gitea-tags'
| 'datasource-github-release-attachments'
| 'datasource-gitlab-packages'
| 'datasource-gitlab-releases'
| 'datasource-gitlab-tags'
| 'datasource-glasskube-packages'
| 'datasource-go-direct'
| 'datasource-go-proxy'
| 'datasource-go'
| 'datasource-golang-version'
| 'datasource-gradle-version'
| 'datasource-helm'
| 'datasource-hermit'
| 'datasource-hex'
| 'datasource-hexpm-bob'
| 'datasource-java-version'
| 'datasource-jenkins-plugins'
| 'datasource-maven:head-requests-timeout'
| 'datasource-maven:head-requests'
| 'datasource-maven:index-html-releases'
| 'datasource-maven:metadata-xml'
| 'datasource-node-version'
| 'datasource-npm:data'
| 'datasource-nuget-v3'
| 'datasource-orb'
| 'datasource-packagist'
| 'datasource-pod'
| 'datasource-python-version'
| 'datasource-releases'
| 'datasource-repology'
| 'datasource-ruby-version'
| 'datasource-rubygems'
| 'datasource-terraform-module'
| 'datasource-terraform-provider'
| 'datasource-terraform'
| 'datasource-unity3d'
| 'github-releases-datasource-v2'
| 'github-tags-datasource-v2'
| 'merge-confidence'
| 'preset'
| 'terraform-provider-hash'
| 'url-sha256';
export type PackageCacheNamespace = string;

export type CombinedKey = `global%%${PackageCacheNamespace}%%${string}`;

0 comments on commit 2466add

Please sign in to comment.