본문 바로가기
내일배움 과제/최종 프로젝트

발키에서 복수 데이터 읽어오기

by GREEN나무 2025. 2. 25.
728x90

 발키 서비스에 코드 추가

  async getKeysByPattern(
    pattern: string,
    count: number = 100,
  ): Promise<string[]> {
    let cursor = '0';
    let keys: string[] = [];

    do {
      const [newCursor, foundKeys]: [string, string[]] = await this.client.scan(
        cursor,
        'MATCH',
        pattern,
        'COUNT',
        count.toString(),
      );

      cursor = newCursor;
      if (Array.isArray(foundKeys)) {
        keys = keys.concat(foundKeys);
      }
    } while (cursor !== '0');

    return keys;
  }

 

읽어오기

import {
  Injectable,
  NotFoundException,
  BadRequestException,
  ParseIntPipe,
} from '@nestjs/common';
import { SubAchievement } from '../sub-achievement/entities/sub-achievement.entity';
import { PinkmongAppearLocation } from '../pinkmong-appear-location/entities/pinkmong-appear-location.entity';
import { CreateDirectionDto } from './dto/create-direction.dto';
import { UpdateDirectionDto } from './dto/update-direction.dto';
import { S3Service } from '../s3/s3.service';
import { ValkeyService } from '../valkey/valkey.service';
import { Entity } from 'typeorm';

@Injectable()
export class DirectionService {
  constructor(
    private readonly subEntity: SubAchievement,
    private readonly pinkEntity: PinkmongAppearLocation,
    private readonly s3Service: S3Service,
    private readonly valkeyService: ValkeyService,
  ) {}

  async createBookmarks() {
    // ✅ Redis SCAN을 사용하여 패턴에 맞는 키들을 가져옴

    // 서브업적 빌키에서 읽어오기
    const keyS = await this.valkeyService.getKeysByPattern(`sub-achievement:*`);
    console.log('🔍 keysS 확인:', keyS);
    
    ...