2024-07-22 13:34:10 +08:00
|
|
|
import { Injectable } from '@nestjs/common';
|
2024-08-02 19:48:38 +08:00
|
|
|
import { ConfigService } from '@nestjs/config';
|
2024-07-22 13:34:10 +08:00
|
|
|
import Redis from 'ioredis';
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class RedisService {
|
|
|
|
private readonly redisClient: Redis;
|
|
|
|
|
2024-08-02 19:48:38 +08:00
|
|
|
constructor(private readonly configService: ConfigService) {
|
2024-07-22 13:34:10 +08:00
|
|
|
this.redisClient = new Redis({
|
2024-08-02 19:48:38 +08:00
|
|
|
host: configService.get<string>('REDIS_HOST'),
|
|
|
|
port: configService.get<number>('REDIS_PORT'), // Redis 服务器的端口
|
2024-07-22 13:34:10 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
setValue(key: string, value: string) {
|
|
|
|
return this.redisClient.set(key, value);
|
|
|
|
}
|
|
|
|
getValue(key: string) {
|
|
|
|
return this.redisClient.get(key);
|
2024-08-02 19:48:38 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
keys(pattern: string) {
|
|
|
|
return this.redisClient.keys(pattern)
|
|
|
|
}
|
|
|
|
setWithExpiry(key: string, value: string, time: number) {
|
|
|
|
return this.redisClient.setex(key, time, value);
|
|
|
|
}
|
|
|
|
deleteKey(key: string) {
|
|
|
|
return this.redisClient.del(key);
|
|
|
|
}
|
|
|
|
setHashField(key: string, field: string, value: string) {
|
|
|
|
return this.redisClient.hset(key, field, value);
|
|
|
|
}
|
|
|
|
//获取key中的field字段数据
|
|
|
|
getHashField(key: string, field: string) {
|
|
|
|
return this.redisClient.hget(key, field);
|
|
|
|
}
|
|
|
|
//获取key中所有数据
|
|
|
|
getAllHashFields(key: string) {
|
|
|
|
return this.redisClient.hgetall(key);
|
|
|
|
}
|
|
|
|
publishMessage(channel: string, message: string) {
|
|
|
|
return this.redisClient.publish(channel, message);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 订阅消息,需要提供一个回调函数来处理接收到的消息
|
|
|
|
subscribeToMessages(channel: string, messageHandler: (channel: string, message: string) => void) {
|
|
|
|
this.redisClient.subscribe(channel, (err, count) => {
|
|
|
|
if (err) {
|
|
|
|
console.error('Subscription error', err);
|
|
|
|
} else {
|
|
|
|
console.log(`Subscribed to ${count} channels`);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
this.redisClient.on('message', (channel, message) => {
|
|
|
|
console.log(`Received message ${message} from channel ${channel}`);
|
|
|
|
messageHandler(channel, message);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// 取消订阅指定的频道
|
|
|
|
unsubscribeFromChannel(channel: string) {
|
|
|
|
return this.redisClient.unsubscribe(channel);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 取消订阅所有频道
|
|
|
|
unsubscribeAll() {
|
|
|
|
return this.redisClient.quit();
|
2024-07-22 13:34:10 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|