CacheKit is a lightweight, thread-safe disk caching library for Apple platforms (iOS, macOS, tvOS, visionOS). It provides a simple and efficient way to cache Codable data, commonly used for storing network response data.
Key Features:
- Thread-safe using Swift's Actor model
- Support for both in-memory and disk caching
- Automatic cache expiration
- Generic type support for any
Codabletype
- Language: Swift 5.9+
- Package Manager: Swift Package Manager (SPM)
- Platforms: macOS 11+, iOS 13+, tvOS 13+, visionOS 1+
- Documentation: DocC
CacheKit11/
├── Package.swift # SPM manifest
├── Sources/
│ └── CacheKit/
│ ├── Cache.swift # Cache protocol definition
│ ├── CacheEntry.swift # Cache entry model
│ ├── CacheImplementation.swift # InMemoryCache & DiskCache
│ ├── KeysTracker.swift # NSCacheDelegate for tracking keys
│ └── CacheKit.docc/ # DocC documentation
├── Tests/
│ └── CacheTests/
│ └── CacheTests.swift # Unit tests
├── .docs/ # Generated documentation
└── .github/workflows/ # CI/CD workflows
# Clone and navigate to project
cd CacheKit11
# Build the project
swift build
# Build for release
swift build -c release# Run all tests
swift test
# Run tests with verbose output
swift test --verbose
# Run specific test
swift test --filter CacheKitTests- Ensure all tests pass before committing changes
- Add or update tests for any new functionality
- Test on multiple platforms when possible
# Generate DocC documentation
./build-docc.sh
# Or manually
swift package generate-documentation- Use Swift's native naming conventions (camelCase for variables/functions, PascalCase for types)
- All public APIs must have documentation comments using
///format - Use Swift's Actor model for thread safety
- Prefer protocol-oriented design
- Use generics where appropriate
- Keep functions small and focused
- Use
Codablefor serialization
The caching system follows a protocol-oriented design:
CacheProtocol - Base protocol defining caching operations (Actor-based for thread safety)NSCacheTypeProtocol - Extension with NSCache-based implementationInMemoryCache- In-memory only cache implementationDiskCache- Persistent disk cache with in-memory caching layerCacheEntry- Wrapper for cached values with expiration timestampKeysTracker- NSCacheDelegate to track cache keys
- Define the protocol/interface first
- Implement the feature with proper Actor isolation
- Add documentation comments
- Write unit tests
- Update DocC documentation if needed
- Cache implementations are in
CacheImplementation.swift - Respect the Actor model for thread safety
- Ensure expiration logic is maintained
- Title format:
[CacheKit] <Brief description> - Ensure all tests pass:
swift test - Build successfully:
swift build - Update documentation for API changes
- Follow existing code style
- Cache files are stored in the app's sandboxed directory
- Sensitive data should be encrypted before caching
- Consider cache expiration for security-sensitive data
- The library does not handle encryption - implement at the application level if needed
This project has no external dependencies. It only uses Apple's Foundation framework.