์๋ ํ์ญ๋๊น, ๋ฏผํธ์ ๋๋ค. ๐
๋ฐ์ดํฐ๋ค์ ์ด๋์ ์ ์ฅ๋๊ณ ์ด๋ป๊ฒ ์ฌ์ฉ๋ ๊น์?
๋จผ์ ํ๋ ๋์คํฌ์ ์ฐ๋ฆฌ์ ๋ฐ์ดํฐ๋ค์ด ์ ๋ถ ์ ์ฅ๋ฉ๋๋ค. ๋๋ฌธ์ ํ๋ ๋์คํฌ๋ ์ฉ๋์ด ๋งค์ฐ ํฌ๊ณ , ๊ธฐ์ต์ ํ๋ ๊ฒ์ ์น์ค๋์ด ์์ต๋๋ค.
RAM, ๋ฉ๋ชจ๋ฆฌ๋ ๊ทธ๋ฌํ ํ๋ ๋์คํฌ์์ ์ฑ์ ์คํํ๋๋ฐ์ ํ์ํ ์ค์ ๋ฐ์ดํฐ๋ค์ ๋ค๊ณ ์์ต๋๋ค. ๊ทธ๋ฌ๋ ์ฌ์ ํ ๋ด์ฉ์ ๋ฐฉ๋ํ๊ณ , RAM์ ๋์คํฌ๋ณด๋จ ๊ธฐ์ตํ ์ ์๋ ์์ด ์ ๊ณ , CPU์ ๊ฐ๊น์์ ๋น ๋ฅด์ง๋ง ์ด์ฐ ๋์๊ฑด ๊ธฐ์ต์ ํ๋ ๊ฒ์๋ง ๋ฅ๋ ฅ์ด ์น์ค๋์ด ์์ต๋๋ค.
CPU๋ RAM์์ ๋ฐ์ ๋ฐ์ดํฐ๋ค์ ์ด์ฉํด ์ฑ์ ์คํํฉ๋๋ค. ๊ธฐ์ต๋ณด๋ค๋ ์ฑ๋ฅ์ด ์ค์ํด, ๊พธ์คํ๊ฒ ์ฑ๋ฅ์ด ์ ๊ทธ๋ ์ด๋ ๋์ด ์์ต๋๋ค. ๋๋ฌธ์ RAM๊ณผ CPU๋ ๊ธฐ์ต๊ณผ ์ฑ๋ฅ์ผ๋ก ๊ฐ์์ ๋ชฉ์ ์ด ๋ค๋ฅด๊ธฐ์ ์๋ก์๋ก ์ฑ๋ฅ์ฐจ์ด๊ฐ ์ ์ ์ฌํ๊ฒ ๋๊ฒ ๋ฉ๋๋ค. RAM์ด CPU๋ฅผ ์ซ์๊ฐ์ง ๋ชปํด CPU๊ฐ RAM์ด ๋ฐ์ดํฐ๋ฅผ ์ฐพ์์ ์ฃผ๊ธฐ๋ฅผ ๊ธฐ๋ค๋ ค์ผ ํ๋ ๋ณ๋ชฉ ํ์์ด ๋ฐ์ํ ๊ฒ์ ๋๋ค.
์ด๋ฅผ ํด๊ฒฐํ๊ธฐ ์ํด ์์ถฉ๊ธฐ์ ์ญํ ์ ํ๋ ์บ์ ๋ฉ๋ชจ๋ฆฌ๊ฐ ๋ฑ์ฅํ์ต๋๋ค. ์บ์ ๋ฉ๋ชจ๋ฆฌ๋ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์ค๋ ์๋๋ฅผ ๋์ด๊ณ ๋น์ฉ์ ๋ฎ์ถ๊ธฐ ์ํด ์ฌ์ฉํฉ๋๋ค. CPU์ ์ผ๋ถ๋ผ๊ณ ๋ ๋ณผ ์ ์๊ฒ ์์ ์นฉ์ผ๋ก CPU์ ์ง์ ๋ถ์ด ์๊ฑฐ๋ ์ฐ๊ฒฐ๋์ด ์์ต๋๋ค.
- ์บ์ ๋ฉ๋ชจ๋ฆฌ ์ ์ฅ ๊ท์น
- ์๊ฐ ์ง์ญ์ฑ : ์ต๊ทผ์ ์ ๊ทผ๋ ๋ฐ์ดํฐ, ์งง์ ์๊ฐ์ ๋์ผํ ๋ฐ์ดํฐ์ ๋ฐ๋ณต์ ์ผ๋ก ์ฌ์ฉ
- ๊ณต๊ฐ ์ง์ญ์ฑ : ์ต๊ทผ์ ์ ๊ทผ๋ ๋ฐ์ดํฐ์ ์ฃผ๋ณ ๋ฐ์ดํฐ
๊ทธ๋ฌ๋ ์ด๋ฌํ ์บ์ ๋ฉ๋ชจ๋ฆฌ๋ ํ๋์จ์ด์ ์ธ ๋ถ๋ถ์ ๋๋ค.
๐ก ์์ฃผ ์ฌ์ฉํ๋ ๋ฐ์ดํฐ๋ ๊ฐ์ ๋ฏธ๋ฆฌ ๋ณต์ฌํด๋๋ ์์ ๊ณต๊ฐ.
๋๋ฌธ์ ์ด๋ฌํ ์บ์์ ๊ฐ๋ ์ ์ถฉ์กฑํ๊ธฐ๋ง ํ๋ค๋ฉด ์บ์๋ CPU, ๋ฉ๋ชจ๋ฆฌ, ํ๋ ๋์คํฌ ์ด๋์์๋ ๋ง๋ค์ด์ง ์ ์์ต๋๋ค.
- CDN(Contents Delivery Network): ์๋ฒ๊ฐ ์ธ๊ตญ์ ์๋ ๊ฒฝ์ฐ ๋ก์ปฌ์ฉ์ผ๋ก ๋ฐ๋ก ๋ฐ์ดํฐ๋ฅผ ๋ฏธ๋ฆฌ ์ ์ฅํด๋๋. (์บ์์ ๊ฐ๋ ์ ์ ์ฌ)
๐ฅ NS Cache
class NSCache<KeyType, ObjectType> : NSObject where KeyType : AnyObject, ObjectType : AnyObject
- ios์์ ์ฌ์ฉํ ์ ์๋ ์บ์ฑ ๋ฐฉ์ ์ค ํ๋.
- key - value
- ๋ฉ๋ชจ๋ฆฌ ์บ์
- ์๋ ์ ๊ฑฐ ์ ์ฑ
๐ฅ URL Cache
class URLCache : NSObject
class var shared: URLCache { get set }
- ios์์ ์ฌ์ฉํ ์ ์๋ ์บ์ฑ ๋ฐฉ์ ์ค ํ๋.
- request - response
- ์บ์๋์ด ์๋ ํ์ผ๋ค์ ํด๋ผ์ด์ธํธ์ ์์ฒญ์ ๋ฐ๋ผ ์ฐพ์์ ๋งค์นญ์์ผ์ค๋ค.
- ๋ฉ๋ชจ๋ฆฌ/ ๋์คํฌ ์บ์ ๋ชจ๋ ๊ฐ๋ฅ.
๋ฉ๋ชจ๋ฆฌ ๋์คํฌ ์๋ ๋น ๋ฆ ๋ฆ์ ์ฉ๋ ์ ์ ๋ง์ ์ ์ฅ ๊ธฐ๊ฐ ๊ธฐ๊ธฐ ์ข ๋ฃ์ ์ญ์ ๋จ ๊ธฐ๊ธฐ ์ข ๋ฃ๋๋ ๋จ์์์.
์ด์ URL Cache๊ฐ ์ด๋ฏธ์ง ํ์ผ์ ๋ถ๋ฌ์ค๋ ๊ฒ์ ์์ด์ ๋ฉ๋ชจ๋ฆฌ์ ์บ์ฑํ๊ณ ๊ฐ์ง๊ณ ์ค๊ฒ ํด์ฃผ๊ณ ์ถ์์ต๋๋ค. ๊ทธ๋ฐ๋ฐ policy๋ฅผ allowedInMemoryOnly๋ก ํด์ฃผ์๋๋ ๋งค๋ฒ ์๋ก์ด ์ด๋ฏธ์ง๋ง ๊ฐ์ ธ์์ต๋๋ค.
์์ผ๊น์? Policies์ defalut๊ฐ์ผ๋ก๋ URLCache๋ request์ ๋ํ response๋ฅผ ์จ๋์คํฌ์ ์ธ๋ฉ๋ชจ๋ฆฌ ๋ ๋ค์ ์ ์ฅํ ์ ์๋ค๋๋ฐ. ์ด๋ ์ฉ๋ ์ฐจ์ด์ ์์์ต๋๋ค. URL Cache์ ๋ด๋ถ ๊ตฌํ์ ์ดํด๋ณด๋ฉด ๋ฉ๋ชจ๋ฆฌ 4MB, ๋์คํฌ 20MB๋ฅผ ๊ฐ์ง๊ณ ์์ต๋๋ค. ๋ํ ๊ด๋ จ ์ค๋ช ์ response๊ฐ ์บ์ ์ฉ๋์ 5% ์ด์์ธ ๊ฒฝ์ฐ๋ ์ ์ฅ๋์ง ์๊ณ ๋งค๋ฒ ์๋ก์ด request๋ฅผ ๋ณด๋ ๋๋ค.
๊ทธ๋ ๋ค๋ฉด ์ฉ๋์ ๋ฐ๊ฟ์ฃผ๋ฉด ์ด๋ป๊ฒ ๋ ๊น์?
policy๋ ์ฌ์ ํ allowedInMemoryOnly์ง๋ง, memory์ ์ฉ๋์ ๋๋ ธ๋๋ ์บ์๋ ๋ฐ์ดํฐ๊ฐ ์กด์ฌํ๋ ๊ฒ์ ๋ณผ ์ ์์์ต๋๋ค. ์ฆ, ์ฉ๋ ์ฐจ์ด ๋๋ฌธ์ด์์ต๋๋ค! ์ด๋ฏธ์ง, ์์ ๋ฑ๊ณผ ๊ฐ์ ์ฉ๋์ด ๋ง์ด ํ์ํ ๋ฐ์ดํฐ๋ค์ ์ฌ์ฉํ๊ณ ์ถ์ ๋๋ NSCache๋ณด๋ค URLCache์ชฝ์ด ๋ ์ ์ ํ๊ฒ ์ต๋๋ค.
https://developer.apple.com/documentation/foundation/nscache
NSCache | Apple Developer Documentation
A mutable collection you use to temporarily store transient key-value pairs that are subject to eviction when resources are low.
developer.apple.com
https://developer.apple.com/documentation/foundation/urlcache
URLCache | Apple Developer Documentation
An object that maps URL requests to cached response objects.
developer.apple.com
https://github.com/apple/swift-corelibs-foundation/blob/main/Sources/Foundation/NSCache.swift
https://developer.apple.com/documentation/foundation/nsurlrequest/cachepolicy/useprotocolcachepolicy
NSURLRequest.CachePolicy.useProtocolCachePolicy | Apple Developer Documentation
Use the caching logic defined in the protocol implementation, if any, for a particular URL load request.
developer.apple.com
https://tech.justeattakeaway.com/2021/02/01/urlcache-default-size-is-not-enough/
URLCache – Default size is not enough – Tech Blog
Even though networks are getting faster every day, the amount of data transferred between your device and the cloud is also increasing. Here at Just Eat, we always aim to write reliable, and efficient code that results in fast, problem free transactions. H
tech.justeattakeaway.com
'๐ iOS > ๐ถ ๊ถ๊ธ์ฆ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[iOS] ARC_๊ฐ์ฒด ์๋ช , weak์ unowned ์ฐจ์ด (0) | 2024.03.21 |
---|---|
[iOS] Swift์ ์ปดํ์ผ๋ฌ _ LLVM (1) | 2024.03.12 |
[UIKit]loadView์ view๋ ์ธ์ ํ ๋น๋ ๊น? (0) | 2024.01.27 |
[Swift]super.init()์ด ๋ฐ๋์ ํ์ํ๋ ๊ฒฝ์ฐ (0) | 2023.11.08 |