2023-11-17 06:58:50 -08:00
|
|
|
import { Service } from 'typedi';
|
2024-09-12 09:07:18 -07:00
|
|
|
|
perf(core): Improve caching service (#8213)
Story: https://linear.app/n8n/issue/PAY-1188
- Implement Redis hashes on the caching service, based on Micha's work
in #7747, adapted from `node-cache-manager-ioredis-yet`. Optimize
workflow ownership lookups and manual webhook lookups with Redis hashes.
- Simplify the caching service by removing all currently unused methods
and options: `enable`, `disable`, `getCache`, `keys`, `keyValues`,
`refreshFunctionEach`, `refreshFunctionMany`, `refreshTtl`, etc.
- Remove the flag `N8N_CACHE_ENABLED`. Currently some features on
`master` are broken with caching disabled, and test webhooks now rely
entirely on caching, for multi-main setup support. We originally
introduced this flag to protect against excessive memory usage, but
total cache usage is low enough that we decided to drop this setting.
Apparently this flag was also never documented.
- Overall caching service refactor: use generics, reduce branching, add
discriminants for cache kinds for better type safety, type caching
events, improve readability, remove outdated docs, etc. Also refactor
and expand caching service tests.
Follow-up to: https://github.com/n8n-io/n8n/pull/8176
---------
Co-authored-by: Michael Auerswald <michael.auerswald@gmail.com>
2024-01-05 02:52:44 -08:00
|
|
|
import { CacheService } from '@/services/cache/cache.service';
|
2023-11-17 06:58:50 -08:00
|
|
|
|
|
|
|
@Service()
|
|
|
|
export class ActivationErrorsService {
|
|
|
|
private readonly cacheKey = 'workflow-activation-errors';
|
|
|
|
|
|
|
|
constructor(private readonly cacheService: CacheService) {}
|
|
|
|
|
2024-01-05 04:06:42 -08:00
|
|
|
async register(workflowId: string, errorMessage: string) {
|
|
|
|
await this.cacheService.setHash(this.cacheKey, { [workflowId]: errorMessage });
|
2023-11-17 06:58:50 -08:00
|
|
|
}
|
|
|
|
|
2024-01-05 04:06:42 -08:00
|
|
|
async deregister(workflowId: string) {
|
|
|
|
await this.cacheService.deleteFromHash(this.cacheKey, workflowId);
|
2023-11-17 06:58:50 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
async get(workflowId: string) {
|
2024-01-05 04:06:42 -08:00
|
|
|
const activationError = await this.cacheService.getHashValue<string>(this.cacheKey, workflowId);
|
2023-11-17 06:58:50 -08:00
|
|
|
|
2024-01-05 04:06:42 -08:00
|
|
|
if (!activationError) return null;
|
2023-11-17 06:58:50 -08:00
|
|
|
|
2024-01-05 04:06:42 -08:00
|
|
|
return activationError;
|
2023-11-17 06:58:50 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
async getAll() {
|
2024-01-05 04:06:42 -08:00
|
|
|
const activationErrors = await this.cacheService.getHash<string>(this.cacheKey);
|
2023-11-17 06:58:50 -08:00
|
|
|
|
2024-01-05 04:06:42 -08:00
|
|
|
if (!activationErrors) return {};
|
2023-11-17 06:58:50 -08:00
|
|
|
|
2024-01-05 04:06:42 -08:00
|
|
|
return activationErrors;
|
2023-11-17 06:58:50 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
async clearAll() {
|
|
|
|
await this.cacheService.delete(this.cacheKey);
|
|
|
|
}
|
|
|
|
}
|