Skip to content

Commit

Permalink
Cache requests for top streams
Browse files Browse the repository at this point in the history
  • Loading branch information
nukeop committed Aug 13, 2023
1 parent 3bbdfa0 commit b3880b2
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 5 deletions.
35 changes: 35 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"class-transformer": "^0.5.1",
"class-validator": "^0.14.0",
"lodash": "^4.17.21",
"node-cache": "^5.1.2",
"reflect-metadata": "^0.1.13",
"rimraf": "^3.0.2",
"rxjs": "^7.2.0"
Expand Down
2 changes: 1 addition & 1 deletion src/stream-mappings/stream-mappings.e2e.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { INestApplication, ValidationPipe } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { Test } from '@nestjs/testing';
import * as request from 'supertest';
import request from 'supertest';
import { StreamMappingBuilder, SupabaseMock } from 'test/supabase-mock';

import { TestFixture } from 'test/test-fixture';
Expand Down
19 changes: 17 additions & 2 deletions src/stream-mappings/stream-mappings.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Injectable, Logger } from '@nestjs/common';
import { SupabaseClient } from '@supabase/supabase-js';
import { groupBy, sortBy } from 'lodash';
import NodeCache from 'node-cache';
import { CreateStreamMappingDto } from './dto/create-stream-mapping.dto';
import { DeleteStreamMappingDto } from './dto/delete-stream-maping.dto';

Expand All @@ -24,12 +25,15 @@ export type StreamIdWithScore = {
@Injectable()
export class StreamMappingsService {
private client: SupabaseClient;
private cache: NodeCache;

constructor() {
this.client = new SupabaseClient(
process.env.SUPABASE_URL ?? '',
process.env.SUPABASE_KEY ?? '',
);

this.cache = new NodeCache({ stdTTL: 60, checkperiod: 60 });
}

private async findAllByArtistTitleAndSource(
Expand Down Expand Up @@ -82,6 +86,13 @@ export class StreamMappingsService {
source: 'Youtube',
author_id?: string,
): Promise<StreamIdWithScore | null> {
const cacheKey = `${artist}:${title}:${source}:`;
const cachedStream: StreamIdWithScore | undefined =
this.cache.get(cacheKey);
if (cachedStream) {
return cachedStream;
}

const streamMappings = await this.findAllByArtistTitleAndSource(
artist,
title,
Expand All @@ -105,17 +116,21 @@ export class StreamMappingsService {
(streamMapping) => streamMapping.author_id === author_id,
);

let topStream: StreamIdWithScore;
if (verifiedByCurrentUser) {
return {
topStream = {
...(streamIdsWithScores.find(
(streamIdWithScore) =>
streamIdWithScore.stream_id === verifiedByCurrentUser.stream_id,
) as StreamIdWithScore),
self_verified: true,
};
} else {
return sortBy(streamIdsWithScores, 'score').reverse()[0];
topStream = sortBy(streamIdsWithScores, 'score').reverse()[0];
}

this.cache.set(cacheKey, topStream);
return topStream;
}

async verifyTrack(
Expand Down
2 changes: 1 addition & 1 deletion test/supabase-mock.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { matches } from 'lodash';
import * as nock from 'nock';
import nock from 'nock';
import { CreateStreamMappingDto } from 'src/stream-mappings/dto/create-stream-mapping.dto';
import { DeleteStreamMappingDto } from 'src/stream-mappings/dto/delete-stream-maping.dto';

Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"noImplicitAny": false,
"strictBindCallApply": true,
"forceConsistentCasingInFileNames": true,
"noFallthroughCasesInSwitch": true
"noFallthroughCasesInSwitch": true,
"esModuleInterop": true
}
}

0 comments on commit b3880b2

Please sign in to comment.