Skip to content

Commit

Permalink
Add clear key pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
hanydd committed Nov 4, 2024
1 parent 27ab1ce commit 1729389
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
7 changes: 6 additions & 1 deletion src/utils/queryCacher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,10 @@ function clearKey(key: string): void {
redis.del(key).catch((err) => Logger.error(err));
}

function clearKeyPattern(keyPattern: string): void {
redis.delPattern(keyPattern).catch((err) => Logger.error(err));
}

function clearSegmentCache(videoInfo: {
videoID: VideoID;
hashedVideoID: VideoIDHash;
Expand All @@ -157,7 +161,7 @@ function clearSegmentCache(videoInfo: {
}): void {
if (videoInfo) {
redis.del(skipSegmentsKey(videoInfo.videoID, videoInfo.service)).catch((err) => Logger.error(err));
redis.del(skipSegmentGroupsKey(videoInfo.videoID, videoInfo.service)).catch((err) => Logger.error(err));
clearKeyPattern(skipSegmentGroupsKey(videoInfo.videoID, "*", videoInfo.service));
redis.del(skipSegmentsHashKey(videoInfo.hashedVideoID, videoInfo.service)).catch((err) => Logger.error(err));
redis.del(videoLabelsKey(videoInfo.hashedVideoID, videoInfo.service)).catch((err) => Logger.error(err));
redis.del(videoLabelsHashKey(videoInfo.hashedVideoID, videoInfo.service)).catch((err) => Logger.error(err));
Expand Down Expand Up @@ -220,6 +224,7 @@ export const QueryCacher = {
getTraced,
getAndSplit,
clearKey,
clearKeyPattern,
clearSegmentCache,
clearSegmentCacheByID,
getKeyLastModified,
Expand Down
22 changes: 16 additions & 6 deletions src/utils/redis.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { config } from "../config";
import { Logger } from "./logger";
import { RedisClientType, SetOptions, createClient } from "redis";
import { RedisCommandArgument, RedisCommandArguments, RedisCommandRawReply } from "@redis/client/dist/lib/commands";
import { RedisClientOptions } from "@redis/client/dist/lib/client";
import { RedisCommandArgument, RedisCommandArguments, RedisCommandRawReply } from "@redis/client/dist/lib/commands";
import { ScanCommandOptions } from "@redis/client/dist/lib/commands/SCAN";
import { LRUCache } from "lru-cache";
import { compress, uncompress } from "lz4-napi";
import { RedisReply } from "rate-limit-redis";
import { RedisClientType, SetOptions, createClient } from "redis";
import { config } from "../config";
import { db } from "../databases/databases";
import { Postgres } from "../databases/Postgres";
import { compress, uncompress } from "lz4-napi";
import { LRUCache } from "lru-cache";
import { Logger } from "./logger";
import { shouldClientCacheKey } from "./redisKeys";

export interface RedisStats {
Expand All @@ -31,6 +32,7 @@ interface RedisSB {
setEx(key: RedisCommandArgument, seconds: number, value: RedisCommandArgument): Promise<string>;
setExWithCache(key: RedisCommandArgument, seconds: number, value: RedisCommandArgument): Promise<string>;
del(...keys: [RedisCommandArgument]): Promise<number>;
delPattern(pattern: RedisCommandArgument): Promise<number>;
increment?(key: RedisCommandArgument): Promise<RedisCommandRawReply[]>;
sendCommand(args: RedisCommandArguments, options?: RedisClientOptions): Promise<RedisReply>;
ttl(key: RedisCommandArgument): Promise<number>;
Expand All @@ -45,6 +47,7 @@ let exportClient: RedisSB = {
setEx: () => Promise.resolve(null),
setExWithCache: () => Promise.resolve(null),
del: () => Promise.resolve(null),
delPattern: () => Promise.resolve(null),
increment: () => Promise.resolve(null),
sendCommand: () => Promise.resolve(null),
quit: () => Promise.resolve(null),
Expand Down Expand Up @@ -199,6 +202,13 @@ if (config.redis?.enabled) {
}
};

const scan = client.scan.bind(client);
exportClient.delPattern = async (pattern) => {
const keys = await scan(0, { MATCH: pattern } as ScanCommandOptions);
await Promise.allSettled(keys.keys.map((key) => exportClient.del(key)));
return keys.keys.length;
};

const ttl = client.ttl.bind(client);
exportClient.ttl = async (key) => {
if (cache && cacheClient && ttlCache.has(key)) {
Expand Down

0 comments on commit 1729389

Please sign in to comment.