levibostian / Teller-iOS

iOS library that manages your app's cached data with ease.
MIT License
12 stars 0 forks source link

Clean up old caches #84

Open levibostian opened 4 years ago

levibostian commented 4 years ago

After using Teller a while, there is the potential to have lots of caches saved to the local device.

As an example, you have a DataSource that retrieves the user profile of a user's social media account. If you simply call saveCache() for every retrieval of every social media account, you could end up retrieving an infinite amount of cache. Teller at this does not provide a convenient way to delete cache that is obsolete. It's a manual process at this time.

Proposal: Teller tells your app what cache is (1) a certain age old or (2) the oldest of the cache collection so that you can delete your cache, easily.

Proposed API:

class UserProfileDataSource: DataSource {

  var obsoleteAgeOfCache: Period = Period(unit: 5, component: .months)
  // or...
  var numberOfCacheResultsToKeep = 100

  func deleteOldCache(_ requirements: Requirements) {
    // Will be called for cache results that are > 5 months old or...
    // will be called for cache that is the 101 oldest cache result or older. 
  }

}

You can specify neither, both, or one of the obsoleteAgeOfCache or numberOfCacheResultsToKeep properties. Teller will call deleteOldCache() with requirements that meets at least 1 of the criteria. deleteOldCache() does not allow throwing so it's recommended that a delete call error is recorded manually by the developer. Also, if the optional function deleteOldCache() is not defined in the DataSource, Teller will skip the delete request and will be called again the next time the DataSource is initialized.

levibostian commented 4 years ago

I encountered a use case today for this.

I wrote some migration code in my app where I needed to delete the old Teller cache data of 1 of my repositories so when the app installs the new update, just that 1 cache gets re-created.

I propose adding a clear() function to the Repository class that will (1) call deleteOldCache() on the data source to delete all of the old cache and (2) delete the Teller metadata associated with that repo.