Skip to content

Commit

Permalink
feat(deezer-sdk): simple redis implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
bambanah committed Sep 11, 2024
1 parent 4cb3fe5 commit bbd3787
Show file tree
Hide file tree
Showing 8 changed files with 137 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/polite-clocks-pull.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"deezer-sdk": minor
---

Use redis to cache Deezer API calls
5 changes: 5 additions & 0 deletions .changeset/short-walls-appear.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"deemix-webui": minor
---

Significantly faster Deezer API calls by caching using Redis
2 changes: 1 addition & 1 deletion deemix/src/plugins/spotify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ export default class SpotifyPlugin extends BasePlugin {
}

callback();
}, settings.queueConcurrency * 20);
}, settings.queueConcurrency);

downloadObject.conversion_data.forEach((track, pos) => {
q.push({ track, pos }, () => {});
Expand Down
1 change: 1 addition & 0 deletions deezer-sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"license": "GPL-3.0-or-later",
"dependencies": {
"got": "14.4.2",
"redis": "^4.7.0",
"tough-cookie": "^4.0.0",
"zod": "^3.23.8"
},
Expand Down
31 changes: 28 additions & 3 deletions deezer-sdk/src/api.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import got from "got";
import { createClient, type RedisClientType } from "redis";
import { CookieJar } from "tough-cookie";
import {
APIError,
Expand All @@ -11,10 +12,8 @@ import {
PermissionException,
WrongParameterException,
} from "./errors.js";
import { Deezer, type APIOptions } from "./index.js";
import { albumSchema, type DeezerAlbum } from "./schema/album-schema.js";
import { type APIOptions } from "./index.js";
import { trackSchema, type DeezerTrack } from "./schema/track-schema.js";
import { artistSchema } from "./schema/contributor-schema.js";

// Possible values for order parameter in search
export const SearchOrder = {
Expand Down Expand Up @@ -204,16 +203,39 @@ export class API {
http_headers: { "User-Agent": string };
cookie_jar: CookieJar;
access_token: string | null;
redisClient?: RedisClientType;

constructor(cookie_jar: CookieJar, headers: { "User-Agent": string }) {
this.http_headers = headers;
this.cookie_jar = cookie_jar;
this.access_token = null;

const url = process.env.REDIS_URL || "redis://localhost:6379";
if (url && process.env.REDIS_PASSWORD) {
this.redisClient = createClient({
url: url,
password: process.env.REDIS_PASSWORD,
});

this.redisClient.on("error", (err) => {
console.error("Redis error", err);
});

this.redisClient.connect();
}
}

async call(endpoint: string, args: APIArgs = {}): Promise<unknown> {
if (this.access_token) args["access_token"] = this.access_token;

if (this.redisClient?.isReady) {
const cachedResponse = await this.redisClient.get(endpoint);

if (cachedResponse) {
return JSON.parse(cachedResponse);
}
}

let response;
try {
response = await got
Expand Down Expand Up @@ -291,6 +313,9 @@ export class API {
throw new APIError(response.error);
}

if (this.redisClient?.isReady)
this.redisClient.set(endpoint, JSON.stringify(response));

return response;
}

Expand Down
12 changes: 12 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
services:
cache:
image: redis:7.4-alpine
restart: always
ports:
- "6379:6379"
command: redis-server --save 20 1 --loglevel warning --requirepass eYVX7EwVmmxKPCDmwMtyKVge8oLd2t81
volumes:
- cache:/data
volumes:
cache:
driver: local
82 changes: 82 additions & 0 deletions pnpm-lock.yaml

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

3 changes: 3 additions & 0 deletions webui/src/server/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ import indexRouter from "./routes/index.js";
import type { Arguments, Listener } from "./types.js";
import { registerWebsocket } from "./websocket/index.js";

import dotenv from "dotenv";
dotenv.config({ path: join(import.meta.dirname, ".env") });

const MemoryStore = memorystore(session);

// TODO: Remove type assertion while keeping correct types
Expand Down

0 comments on commit bbd3787

Please sign in to comment.