Skip to content

Shrink the API and simplify storage for 5.0 - #5

Merged
gabors merged 3 commits into
masterfrom
v5
Nov 7, 2026
Merged

gabors merged 3 commits into
masterfrom
v5

Conversation

@gabors

@gabors gabors commented Nov 7, 2026 •

Copy link
Copy Markdown
Member

Breaking changes for 5.0.0. This PR doesn't tag a version: the changelog lists everything under "Unreleased".

Why

About a third of the public API was unused by both PocketLion and Screeners, and some of it didn't work as documented: Entry.meta was always empty, and maxSize was only enforced during a purge. Swift source goes from 1,083 lines to 788.

What changed

Removed

  • ImageWrapper, Image, the image extensions, JSONArrayWrapper, JSONDictionaryWrapper, JSONArray and JSONDictionary. The package no longer depends on UIKit or AppKit, which also ends the clash with SwiftUI's Image.
  • DiskConfig.maxSize and Entry.meta.
  • TypeWrapperStorage. JSONEncoder handles top-level values now, and Data goes to disk as it is instead of base64 in JSON, which is about a quarter smaller.
  • The public StorageAware protocol, which is now internal. Its docs moved to Storage.

Changed

  • entry(ofType:forKey:) treats an expired object as missing, like object(ofType:forKey:). Every caller in both apps already checked for that. Disk reads check the expiry first, so an expired object isn't read or decoded.
  • Files are now named <hash>.cache.

Added

  • Storage.diskSize(), because Screeners currently works out the folder path itself to measure it. It counts hidden files, and it throws if a folder can't be read rather than reporting a partial size.

README

  • Cut from about 230 lines to 90: install, usage, configuration, expiry, errors and file protection. The install line says from: "5.0.0", which won't work until that tag exists.

What to look at

  • On-disk format: caches start empty after upgrading. 4.x files are never read, so an old {"object":...} file can't decode into the wrong type. removeExpiredObjects() removes them during its normal scan, even if they haven't expired, so upgrading adds no launch-time work. Until then they stay on disk, and that includes PocketLion's, because it only calls removeAll().
  • Everything in the folder that isn't a .cache file is deleted by a purge, hidden files included. The folder belongs to the cache, but this is new behaviour.
  • Data reads: with no wrapper, asking for Data on a key that holds another type returns its raw JSON bytes instead of a type error.

Upgrading the apps

  • Screeners: remove maxSize: 0 from Caches.swift in two places, or it won't compile. The !entry.expiry.isExpired checks still compile, but they're now redundant, and getFolderSize can use diskSize().
  • PocketLion: no code changes. It re-downloads its saved totals once.

Testing

  • swift test on macOS: 32 tests pass.
  • xcodebuild test on the iOS Simulator: 33 tests pass.
  • The tests for the second commit's fixes fail when run against the first commit.
  • Builds for watchOS, tvOS, visionOS and Mac Catalyst.
  • A copy of both apps' Cache calls compiles against this branch for iOS, in a file that also imports SwiftUI and uses Image.

Not verified

  • Neither app has been built against this branch.
  • Not run on a real device. The file protection checks on gabiPhone were on 4.10.0. The code paths for protection didn't change here.
  • Storage.playground was edited to remove its image code, but not run.

Removed
- ImageWrapper, the Image typealias, the JSON wrappers and their
  typealiases. Neither PocketLion nor Screeners uses them, and the package
  no longer depends on UIKit or AppKit.
- DiskConfig.maxSize, which was only enforced by removeExpiredObjects()
  and which neither app sets
- Entry.meta, which was always empty
- TypeWrapperStorage. JSONEncoder handles top-level values, and Data now
  goes to disk as it is instead of base64 in JSON.
- The public StorageAware protocol. Its docs moved to Storage.

Changed
- entry(ofType:forKey:) treats an expired object as missing. Every caller
  was already checking for that. Disk reads check the expiry first, so an
  expired object isn't read or decoded.
- Files are named <hash>.cache. Files from 4.x are never read, and
  removeExpiredObjects() removes them.

Added
- Storage.diskSize()

Docs
- Cut the README down to what's needed to use the package

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Disk storage still mishandles optional Data serialization, discards enumeration errors, and skips hidden entries, causing read failures, misleading size results, and incomplete cleanup.

3 open issues
What changed in this PR

Prepares Cache for 5.0 by reducing its public API and simplifying serialization across disk, memory, and hybrid storage.

Changes:

  • Removes image and JSON wrappers, public StorageAware, entry metadata, and disk-size eviction.
  • Stores values without wrappers, uses .cache filenames, and rejects expired entries before disk decoding.
  • Adds diskSize(), updates regression tests, and streamlines documentation and examples.
File Description
Tests/​CacheTests/​TestSupport.swift Updates storage setup and adds a filename helper.
Tests/​CacheTests/​StorageTests.swift Tests raw data, expiry, legacy cleanup, and disk size.
Sources/​Cache/​Storage/​TypeWrapperStorage.swift Removes the serialization wrapper layer.
Sources/​Cache/​Storage/​StorageAware.swift Makes the protocol internal and adds disk-size reporting.
Sources/​Cache/​Storage/​Storage.swift Exposes documented APIs without wrapper storage.
Sources/​Cache/​Storage/​MemoryStorage.swift Rejects expired entries and reports zero disk usage.
Sources/​Cache/​Storage/​HybridStorage.swift Delegates disk-size reporting under its lock.
Sources/​Cache/​Storage/​DiskStorage.swift Updates file format, expiry checks, scanning, and size reporting.
Sources/​Cache/​Library/​Types.swift Removes the platform image alias.
Sources/​Cache/​Library/​JSONDictionaryWrapper.swift Removes the JSON dictionary wrapper and alias.
Sources/​Cache/​Library/​JSONArrayWrapper.swift Removes the JSON array wrapper and alias.
Sources/​Cache/​Library/​ImageWrapper.swift Removes image serialization support.
Sources/​Cache/​Library/​Entry.swift Removes entry metadata.
Sources/​Cache/​Extensions/​UIImage+Extensions.swift Removes UIKit image conversion helpers.
Sources/​Cache/​Extensions/​NSImage+Extensions.swift Removes AppKit image conversion helpers.
Sources/​Cache/​Configuration/​DiskConfig.swift Removes the maximum disk-size setting.
README.md Condenses usage, configuration, and error documentation.
Playgrounds/​Storage.playground/​Contents.swift Removes image examples.
CHANGELOG.md Records unreleased breaking changes and upgrade guidance.

Review effort: Balanced

Comment thread Sources/Cache/Storage/DiskStorage.swift Resolved
Comment thread Sources/Cache/Storage/DiskStorage.swift Resolved
Comment thread Sources/Cache/Storage/DiskStorage.swift Resolved
- Only write raw bytes for exactly Data. A Data? was written raw, because
  as? Data unwraps it, but read back through JSON, which failed.
- Include hidden files in scans, so diskSize() counts them and
  removeExpiredObjects() removes them
- Keep directory enumeration errors, so diskSize() can't report 0 or a
  partial size as if it were correct when a folder can't be read
@gabors
gabors requested a balanced review from Copilot November 7, 2026 01:08
@gabors gabors self-assigned this Nov 7, 2026

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Disk reads can return stale values when entries expire during reading or decoding, and the purge documentation omits that unrelated non-.cache files are deleted.

1 open issue
1 previously missed issue

In code that hasn't changed since last review

Medium severity Recheck expiry after disk read and decoding

Sources/​Cache/​Storage/​DiskStorage.swift:70

If an object expires during the disk read or decoding, this returns an expired entry. object(...) also returns that stale value now that StorageAware.object no longer checks expiry after reading. This is reproducible with a custom decoder that runs past the expiry deadline. Keep the early check to skip already-expired files, but recheck the recorded expiry before returning.

3 resolved since last review

Review effort: Balanced

Comment thread Sources/Cache/Storage/Storage.swift Outdated
}

/**
Clears all expired objects, along with any files left behind by an older version of this package.
removeExpiredObjects() deletes every file that isn't one of the storage's
objects, whatever its age, so the folder must be used only by the storage.
@gabors
gabors merged commit 6e87cba into master Nov 7, 2026
6 checks passed
@gabors
gabors deleted the v5 branch November 7, 2026 06:17
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.

2 participants