Skip to content

Conversation

@ahmetuysal
Copy link
Contributor

@ahmetuysal ahmetuysal commented Sep 23, 2023

Hi @mcollina, thanks for the amazing library. I was getting wrong results and weird cache keys and noticed I was using the library wrong since TypeScript types are incorrect.

Looking at the source code, it seems like func is expected to have a single argument called args. However, TS types are defined as if it is supporting multiple arguments and references option is getting an array of func parameters. Therefore, only the first argument is actually called and used in key calculations.

PS: I am really glad that you embraced TS :)

Here is my code for your reference:

@Injectable()
export class OrganizationMemberService {
  private readonly logger: Logger;
  private readonly cache;

  constructor(
    private prisma: PrismaService,
    @InjectRedis() private readonly redis: Redis,
  ) {
    this.logger = new Logger(OrganizationMemberService.name);
    this.cache = createCache({
      ttl: 60 * 60 * 24,
      storage: {
        type: 'redis',
        options: {
          client: redis,
          invalidation: { referencesTTL: 60 * 60 * 24 },
        },
      },
    }).define(
      'fetchOrganizationMember',
      {
        references: (args) => {
          this.logger.log(`references args: %o`, args);
          return [`organization-member~${args[0]}:${args[1]}`];
        },
      },
      async (
        organizationId: string,
        memberId: string,
      ): Promise<OrganizationMemberDto | null> => {
        const organizationMember =
          await this.prisma.organizationMember.findUnique({
            where: {
              organizationId_userId: {
                organizationId,
                userId: memberId,
              },
            },
          });

        this.logger.log(
          'Fetched organization member for organizationId: %s, memberId: %s: %o',
          organizationId,
          memberId,
          organizationMember,
        );

        return organizationMember
          ? new OrganizationMemberDto(organizationMember)
          : null;
      },
    );
  }

  async getOrganizationMember(
    organizationId: string,
    memberId: string,
  ): Promise<OrganizationMemberDto | null> {
    const organizationMember = await this.cache.fetchOrganizationMember(
      organizationId,
      memberId,
    );

    if (!organizationMember) {
      this.logger.log(
        'Organization member not found for organizationId: %s, memberId: %s',
        organizationId,
        memberId,
      );
    }

    return organizationMember;
  }
}

image

Copy link
Owner

@mcollina mcollina left a comment

Choose a reason for hiding this comment

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

Could you add a test for this type? We use tsd.

@ahmetuysal ahmetuysal requested a review from mcollina September 23, 2023 11:55
Copy link
Owner

@mcollina mcollina left a comment

Choose a reason for hiding this comment

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

lgtm

@simone-sanfratello simone-sanfratello merged commit f621991 into mcollina:main Oct 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants