Files
cnpmcore/app/infra/QueueAdapter.ts
MK (fengmk2) e5162f20aa fix: improve TypeScript type definitions across codebase (#844)
This commit enhances type safety and fixes type-related issues
throughout the project including:
- Updated type definitions in entities, repositories, and models
- Improved type annotations in services and controllers
- Fixed type issues in adapters and utilities
- Enhanced test file type definitions
- Added typings/index.d.ts for global type declarations

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-23 00:58:59 +08:00

49 lines
1.2 KiB
TypeScript

import { AccessLevel, Inject, SingletonProto } from '@eggjs/tegg';
import type { Redis } from 'ioredis';
import type { QueueAdapter } from '../common/typing.ts';
/**
* Use sort set to keep queue in order and keep same value only insert once
*/
@SingletonProto({
accessLevel: AccessLevel.PUBLIC,
name: 'queueAdapter',
})
export class RedisQueueAdapter implements QueueAdapter {
@Inject()
private readonly redis: Redis; // 由 redis 插件引入
private getQueueName(key: string) {
return `CNPMCORE_Q_V2_${key}`;
}
private getQueueScoreName(key: string) {
return `CNPMCORE_Q_S_V2_${key}`;
}
/**
* If queue has the same item, return false
* If queue not has the same item, return true
*/
async push<T>(key: string, item: T): Promise<boolean> {
const score = await this.redis.incr(this.getQueueScoreName(key));
const res = await this.redis.zadd(
this.getQueueName(key),
score,
JSON.stringify(item)
);
return res !== 0;
}
async pop<T>(key: string) {
const [json] = await this.redis.zpopmin(this.getQueueName(key));
if (!json) return null;
return JSON.parse(json) as T;
}
async length(key: string) {
return await this.redis.zcount(this.getQueueName(key), '-inf', '+inf');
}
}