Section 5/171 menit

5. Binary Caching — Mekanisme Internal dan Strategi

5. Binary Caching — Mekanisme Internal dan Strategi

Cara Kerja Cache

Ketika menjalankan tuist cache, Tuist:

swift
1. Hitung hash setiap target (lihat seksi 3)
2. Cek apakah XCFramework dengan hash tersebut ada di cache
   ├── Cache Hit → skip build, gunakan cached XCFramework
   └── Cache Miss → build target, simpan XCFramework ke cache
3. Saat `tuist generate`:
   → Target dengan cache tersedia di-inject sebagai pre-built framework
   → Hanya target tanpa cache (biasanya first-party yang sedang dikembangkan) yang dikompilasi

Lokasi Cache

swift
# Default cache location
~/.tuist/Cache/

# Struktur cache
~/.tuist/Cache/
├── BuildCache/
│   └── [hash]/
│       └── [TargetName].xcframework/
└── ProjectGenerationCache/
    └── [manifest-hash]/
        └── project-description.json

Strategi Warming di CI

swift
#!/bin/bash
# scripts/ci-cache-strategy.sh

set -euo pipefail

# Strategi 1: Warm semua external dependencies
# (jarang berubah — cocok untuk warming di awal)
tuist cache --external-only

# Strategi 2: Warm semua kecuali target yang sedang dikembangkan
# (di PR workflow, hanya compile target yang berubah)
tuist cache --targets-to-skip "FeatureHome,FeatureProfile"

# Strategi 3: Warm spesifik targets
tuist cache --targets NetworkKit,AuthKit,SharedUI,AnalyticsKit

# Cek hashes untuk debugging cache miss
tuist cache --print-hashes

File .xcode-version untuk Konsistensi Cache

Cache Tuist memasukkan versi Xcode ke hash. Jika developer pakai Xcode 16.2 tapi CI pakai Xcode 16.0, cache tidak akan pernah match. Solusi:

swift
# .xcode-version — di root repository
16.2

# Ini dibaca oleh:
# - Tuist (untuk hash calculation)
# - mise: `mise install` otomatis install Xcode versi ini
# - xcpretty, fastlane, dan tools lain yang menghormati .xcode-version

Debug Cache Miss

swift
# Lihat hash semua targets
tuist cache --print-hashes

# Output:
# NetworkKit: a3f8b2c1d4e5f6a7
# AuthKit: b4g9c3d2e5f6g7h8
# FeatureHome: c5h0d4e3f6g7h8i9

# Jika hash berbeda antara local dan CI:
# 1. Cek versi Xcode: xcodebuild -version
# 2. Cek Swift version: swift --version
# 3. Cek build settings yang mungkin environment-dependent
# 4. Cek apakah ada file yang ter-include secara tidak sengaja (glob pattern)

# Verbose cache operation untuk melihat detail
tuist cache --verbose

Selective Cache: Hanya Cache Dependencies

Untuk feature module yang sering berubah, tidak perlu di-cache. Hanya cache stable dependencies:

swift
# Cache semua core dan shared modules, biarkan feature dikompilasi fresh
STABLE_TARGETS=(
    "NetworkKit"
    "AuthKit"
    "AnalyticsKit"
    "SharedUI"
    "SharedModels"
    "DesignSystem"
)

tuist cache --targets "${STABLE_TARGETS[@]}"