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

[IDLE-000] 이미지 캐싱 로직 수정 #74

Merged
merged 6 commits into from
Sep 24, 2024
Merged

Conversation

J0onYEong
Copy link
Contributor

@J0onYEong J0onYEong commented Sep 24, 2024

변경된점

  • 이미지 캐싱로직 수정

이미지 캐싱로직 수정및 테스트 코드 작성

파일매니저를 여러 공간에서 동시에 접근할 시 동시성문제가 발생할 수 있습니다.
따라서 파일매니저에 접근하는 코드를 단일쓰레드에서 처리되도록 구현하였습니다.

이미지 캐싱은 다음과 같은 단계로 진행됩니다.

  1. 메모리 및 디스크 캐싱확인 (직렬 스케줄려)
  2. 캐시된 이미지가 없다면 다운로드 (동시 스케줄려)
  3. 다운로드 완려된 이미지를 디스크및 메모리에 캐싱 (직렬 스케줄려)

이미지 다운로드 작업의 경우 여러곳에서 접근시 동시에 처리되도록 하기위해 아래코드처럼 각단계를 나누고 다른 스케줄러를 사용하도록 설계하였습니다.

// MARK: 이미지 캐싱정보 확인
let findCacheResult = findCache(imageInfo: imageInfo)
    .subscribe(on: fileManagerScheduler)
    .asObservable()
    .share()
let cacheFound = findCacheResult.compactMap { $0 }
let cacheNotFound = findCacheResult.filter { $0 == nil }
// MARK: 이미지 다운로드
let imageDownloadResult = cacheNotFound
    .observe(on: downloadScheduler)
    .map { [imageInfo] _ -> Data? in
        try? Data(contentsOf: imageInfo.imageURL)
    }
    .share()
let downloadSuccess = imageDownloadResult.compactMap { $0 }
// MARK: 다운로드된 이미지 캐싱
let downloadedImage = downloadSuccess
    .observe(on: fileManagerScheduler)
    .compactMap { [imageInfo, weak self] data -> UIImage? in
        
        // 이미지 캐싱
        _ = self?.cacheImage(imageInfo: imageInfo, contents: data)
        
        return self?.createUIImage(data: data, format: imageInfo.imageFormat)
    }
return Observable
    .merge(
        downloadedImage.asObservable(),
        cacheFound.asObservable()
    )
    .asSingle()

테스트 코드 작성

동시성 환경에 문제가 발생하는 지 확인하기 위해 60개의 이미지를 동시에 다운로드하는 테스트를 진행했습니다.

스크린샷 2024-09-24 오후 1 16 01

CI 환경에서(Github action) 이미지 다운로드 속도가 너무 늦어, 테스트를 통과하지 못했지만 로컬에서 테스트가 통가된것을 확인하였습니다.
스크린샷 2024-09-24 오후 2 04 09

@J0onYEong J0onYEong merged commit dd87e62 into develop Sep 24, 2024
@J0onYEong J0onYEong deleted the fix/image_caching branch October 11, 2024 00:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant