Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cache-flush step in Twenty upgrade command #7521 #7553

Merged
merged 3 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import { Logger } from '@nestjs/common';

import { Command, CommandRunner } from 'nest-commander';
import { Command, CommandRunner, Option } from 'nest-commander';

import { InjectCacheStorage } from 'src/engine/core-modules/cache-storage/decorators/cache-storage.decorator';
import { CacheStorageService } from 'src/engine/core-modules/cache-storage/services/cache-storage.service';
import { CacheStorageNamespace } from 'src/engine/core-modules/cache-storage/types/cache-storage-namespace.enum';

// TODO: implement dry-run
@Command({
name: 'cache:flush',
description: 'Completely flush cache',
description: 'Flush cache for specific keys matching the pattern',
})
export class FlushCacheCommand extends CommandRunner {
private readonly logger = new Logger(FlushCacheCommand.name);
Expand All @@ -21,9 +20,28 @@ export class FlushCacheCommand extends CommandRunner {
super();
}

async run(): Promise<void> {
this.logger.log('Flushing cache...');
await this.cacheStorage.flush();
async run(
passedParams: string[],
options?: Record<string, any>,
): Promise<void> {
const pattern = options?.pattern || '*';

this.logger.log(`Flushing cache for pattern: ${pattern}...`);

if (pattern === '*') {
await this.cacheStorage.flush();
} else {
await this.cacheStorage.flushByPattern(pattern);
}

this.logger.log('Cache flushed');
}

@Option({
flags: '-p, --pattern <pattern>',
description: 'Pattern to flush specific cache keys (e.g., engine:*)',
})
parsePattern(val: string): string {
return val;
}
Comment on lines +44 to +46
Copy link
Contributor

Choose a reason for hiding this comment

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

style: parsePattern method can be simplified to a one-liner arrow function

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Inject, Injectable } from '@nestjs/common';
import { CACHE_MANAGER, Cache } from '@nestjs/cache-manager';
import { Inject, Injectable } from '@nestjs/common';

import { RedisCache } from 'cache-manager-redis-yet';

Expand Down Expand Up @@ -67,6 +67,31 @@ export class CacheStorageService {
return this.cache.reset();
}

async flushByPattern(scanPattern: string): Promise<void> {
if (!this.isRedisCache()) {
throw new Error('flushByPattern is only supported with Redis cache');
}

const redisClient = (this.cache as RedisCache).store.client;
let cursor = 0;

do {
const result = await redisClient.scan(cursor, {
MATCH: scanPattern,
COUNT: 100,
});

const nextCursor = result.cursor;
const keys = result.keys;

if (keys.length > 0) {
await redisClient.del(keys);
}

cursor = nextCursor;
} while (cursor !== 0);
}

private isRedisCache() {
return (this.cache.store as any)?.name === 'redis';
}
Expand Down
Loading