From 45fd93ef48bee8387797289f45137c6a504323df Mon Sep 17 00:00:00 2001 From: Grant Date: Wed, 13 Mar 2024 10:54:20 -0600 Subject: [PATCH] add canvas_cache tool --- packages/server/package.json | 3 ++- packages/server/src/lib/redis.ts | 11 +++++++++++ packages/server/src/tools/canvas_cache.ts | 14 ++++++++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 packages/server/src/tools/canvas_cache.ts diff --git a/packages/server/package.json b/packages/server/package.json index f502c9b..7c72171 100644 --- a/packages/server/package.json +++ b/packages/server/package.json @@ -8,7 +8,8 @@ "lint": "eslint .", "prisma:studio": "prisma studio", "prisma:migrate": "prisma migrate deploy", - "prisma:seed:palette": "./tool.sh seed_palette" + "prisma:seed:palette": "./tool.sh seed_palette", + "tool": "./tool.sh" }, "keywords": [], "author": "", diff --git a/packages/server/src/lib/redis.ts b/packages/server/src/lib/redis.ts index 75ef6d7..be28088 100644 --- a/packages/server/src/lib/redis.ts +++ b/packages/server/src/lib/redis.ts @@ -51,6 +51,17 @@ class _Redis { this.isConnected = true; } + async disconnect() { + if (!this.isConnected) { + Logger.warn("Redis#disconnect called while not connected"); + return; + } + + await this.client.disconnect(); + Logger.info("Disconnected from Redis"); + this.isConnected = false; + } + async getClient() { if (!this.isConnected) { await this.connect(); diff --git a/packages/server/src/tools/canvas_cache.ts b/packages/server/src/tools/canvas_cache.ts new file mode 100644 index 0000000..325b29b --- /dev/null +++ b/packages/server/src/tools/canvas_cache.ts @@ -0,0 +1,14 @@ +import Canvas from "../lib/Canvas"; +import { Redis } from "../lib/redis"; + +const log = (...data: any) => { + // eslint-disable-next-line no-console + console.log(...data); +}; + +(async () => { + log("Caching pixels from database to Redis..."); + await Canvas.pixelsToRedis(); + await Redis.disconnect(); + log("Cached"); +})();