Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow backup opt out #121

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
All notable changes to this project will be documented in this file. SPTPersistentCache adheres to [Semantic Versioning](http://semver.org/).

--
## [1.1.2](https://github.com/spotify/SPTPersistentCache/releases/tag/1.1.2)

### Added
* SPTPersistentCacheOptions property allowing users to opt out of system backup.

## [1.1.1](https://github.com/spotify/SPTPersistentCache/releases/tag/1.1.1)
_Released on 2017-08-15._

Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ $ gem install cocoapods
```
Then simply add `SPTPersistentCache` to your `Podfile`.
```
pod 'SPTPersistentCache', '~> 1.1.1'
pod 'SPTPersistentCache', '~> 1.1.2'
```
Lastly let CocoaPods do its thing by running:
```shell
Expand All @@ -48,7 +48,7 @@ $ brew install carthage
```
You will also need to add `SPTPersistentCache` to your `Cartfile`:
```
github "spotify/SPTPersistentCache" ~> 1.1.1
github "spotify/SPTPersistentCache" ~> 1.1.2
```
After that is all said and done, let Carthage pull in SPTPersistentCache like so:
```shell
Expand All @@ -62,7 +62,7 @@ For an example of this framework's usage, see the demo application `SPTPersisten
### Creating the SPTPersistentCache
It is best to use different caches for different types of data you want to store, and not just one big cache for your entire application. However, only create one `SPTPersistentCache` instance for each cache, otherwise you might encounter anomalies when the two different caches end up writing to the same file.
```objc
NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).firstObject stringByAppendingString:@"com.spotify.demo.image.cache"];
NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).firstObject stringByAppendingPathComponent:@"com.spotify.demo.image.cache"];

SPTPersistentCacheOptions *options = [SPTPersistentCacheOptions new];
options.cachePath = cachePath;
Expand Down
2 changes: 1 addition & 1 deletion SPTPersistentCache.podspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Pod::Spec.new do |s|

s.name = "SPTPersistentCache"
s.version = "1.1.1"
s.version = "1.1.2"
s.summary = "SPTPersistentCache is a fast, binary, LRU cache used in the Spotify iOS app"

s.description = <<-DESC
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ - (void)viewDidLoad
NSString *cacheIdentifier = @"com.spotify.demo.image.cache";
NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory,
NSUserDomainMask,
YES).firstObject stringByAppendingString:cacheIdentifier];
YES).firstObject stringByAppendingPathComponent:cacheIdentifier];

SPTPersistentCacheOptions *options = [SPTPersistentCacheOptions new];
options.cachePath = cachePath;
Expand Down
4 changes: 2 additions & 2 deletions SPTPersistentCacheFramework/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>1.1.1</string>
<string>1.1.2</string>
<key>NSHumanReadableCopyright</key>
<string>Copyright © 2019 Spotify. All rights reserved.</string>
<string>Copyright © 2020 Spotify. All rights reserved.</string>
<key>NSPrincipalClass</key>
<string></string>
</dict>
Expand Down
11 changes: 10 additions & 1 deletion Sources/SPTPersistentCacheFileManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,16 @@ - (BOOL)createCacheDirectory
return NO;
}
}


NSError *error = nil;
[[NSURL fileURLWithPath:self.options.cachePath] setResourceValue:@(self.options.shouldExcludeFromBackup) forKey:NSURLIsExcludedFromBackupKey error:&error];

if (error) {
SPTPersistentCacheSafeDebugCallback([NSString stringWithFormat:
@"PersistentDataCache: Resource value: %@ could not be set for key 'NSURLIsExcludedFromBackupKey' because: %@",
@(self.options.shouldExcludeFromBackup), error], self.debugOutput);
}

return YES;
}

Expand Down
3 changes: 3 additions & 0 deletions Sources/SPTPersistentCacheOptions.m
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ - (instancetype)init
_cachePath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"/com.spotify.temppersistent.image.cache"];
_cacheIdentifier = @"persistent.cache";
_useDirectorySeparation = YES;
_shouldExcludeFromBackup = NO;
Copy link

@SPTElyasn SPTElyasn Jan 17, 2020

Choose a reason for hiding this comment

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

@aodhol You probably need to provide the deep copy of this. You have it down there in copyWithZone

Choose a reason for hiding this comment

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

Not super important but would be nice to include it in debugDescription.


_garbageCollectionInterval = SPTPersistentCacheDefaultGCIntervalSec;
_defaultExpirationPeriod = SPTPersistentCacheDefaultExpirationTimeSec;
Expand Down Expand Up @@ -113,6 +114,7 @@ - (id)copyWithZone:(NSZone *)zone
copy.cacheIdentifier = self.cacheIdentifier;
copy.cachePath = self.cachePath;
copy.useDirectorySeparation = self.useDirectorySeparation;
copy.shouldExcludeFromBackup = self.shouldExcludeFromBackup;

copy.garbageCollectionInterval = self.garbageCollectionInterval;
copy.defaultExpirationPeriod = self.defaultExpirationPeriod;
Expand Down Expand Up @@ -146,6 +148,7 @@ - (NSString *)debugDescription
return SPTPersistentCacheObjectDescription(self,
self.cacheIdentifier, @"cache-identifier",
self.cachePath, @"cache-path",
self.shouldExcludeFromBackup, @"exclude-from-backup",
self.identifierForQueue, @"identifier-for-queue",
@(self.useDirectorySeparation), @"use-directory-separation",
@(self.garbageCollectionInterval), @"garbage-collection-interval",
Expand Down
4 changes: 2 additions & 2 deletions Tests/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
<key>CFBundlePackageType</key>
<string>BNDL</string>
<key>CFBundleShortVersionString</key>
<string>1.1.1</string>
<string>1.1.2</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>1.1.1</string>
<string>1.1.2</string>
</dict>
</plist>
6 changes: 6 additions & 0 deletions include/SPTPersistentCache/SPTPersistentCacheOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,12 @@ FOUNDATION_EXPORT const NSUInteger SPTPersistentCacheMinimumExpirationLimit;
*/
@property (nonatomic, copy) NSString *cachePath;

/**
Excludes the cache directory from backup.
@discussion Some users may wish to have the cache directory excluded from backup.

Choose a reason for hiding this comment

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

@aodhol In order to avoid any confusion, would be nice to mention the default value here maybe.

*/
@property (nonatomic, assign) BOOL shouldExcludeFromBackup;

/**
Whether directory separation of cache records should be used.
@discussion When enabled cached records are separate into direcectories based on the first two (2) characters in
Expand Down