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

refactor(cache): Deprecate namespace parameter in decorators #31079

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
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
15 changes: 14 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,15 @@ interface CacheParameters {
ttlMinutes?: number;
}

function getClassName(instance: unknown): string | undefined {
// istanbul ignore if: should never happen
if (!is.object(instance)) {
return undefined;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should throw if it's happening. undefined is a bad namespace.

or at least log a stack trace at debug level to see how it happens.

}

return instance.constructor.name;
viceice marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* caches the result of a decorated method.
*/
Expand All @@ -66,6 +77,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
85 changes: 1 addition & 84 deletions lib/util/cache/package/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,89 +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-deb'
| '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}`;