Skip to content

Commit c8a0d67

Browse files
appleguygarrettmoon
authored andcommitted
[PINDiskCache] Respect small byteLimit settings by checking object size in setObject: (pinterest#198)
* [PINDiskCache] Respect small byteLimit settings by checking object size in setObject: This serves as a mechanism for some clients, like users of the Texture framework, to effectively disable the PINDiskCache by setting a byteLimit of 1. Before this change, the cache would transiently exceed the byte limit by writing the file anyway, and then immediately deleting it. This change will not affect most users of PINDiskCache, but is important for this specific use case. * [PINDiskCache] Fast-path objectForKey: when the cache is empty. * Fill out changelog * Ensure lock is acquired before early return in read path; fix test.
1 parent 3a95246 commit c8a0d67

File tree

2 files changed

+23
-9
lines changed

2 files changed

+23
-9
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* Add your own contributions to the next release on the line below this with your name.
44

55
## 3.0.1 -- Beta 5
6+
- [fix] Respect small byteLimit settings by checking object size in setObject: [#198](https://github.com/pinterest/PINCache/pull/198)
67
- [new] Added an ability to set custom encoder/decoder for file names: [#192](https://github.com/pinterest/PINCache/pull/192)
78

89
## 2.2.1 -- 2016 Mar 5

Source/PINDiskCache.m

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -838,7 +838,11 @@ - (id)objectForKeyedSubscript:(NSString *)key
838838
{
839839
NSDate *now = [[NSDate alloc] init];
840840

841-
if (!key)
841+
[self lock];
842+
BOOL isEmpty = (_dates.count == 0 && _sizes.count == 0);
843+
[self unlock];
844+
845+
if (!key || isEmpty)
842846
return nil;
843847

844848
id <NSCoding> object = nil;
@@ -936,8 +940,23 @@ - (void)setObject:(id <NSCoding>)object forKey:(NSString *)key fileURL:(NSURL **
936940
NSDataWritingOptions writeOptions = NSDataWritingAtomic;
937941
#endif
938942

939-
NSURL *fileURL = [self encodedFileURLForKey:key];
940-
943+
// Remain unlocked here so that we're not locked while serializing.
944+
NSData *data = _serializer(object, key);
945+
NSURL *fileURL = nil;
946+
947+
NSUInteger byteLimit = self.byteLimit;
948+
if (data.length <= byteLimit || byteLimit == 0) {
949+
// The cache is large enough to fit this object (although we may need to evict others).
950+
fileURL = [self encodedFileURLForKey:key];
951+
} else {
952+
// The cache isn't large enough to fit this object (even if all others were evicted).
953+
// We should not write it to disk because it will be deleted immediately after.
954+
if (outFileURL) {
955+
*outFileURL = nil;
956+
}
957+
return;
958+
}
959+
941960
[self lock];
942961
PINCacheObjectBlock willAddObjectBlock = self->_willAddObjectBlock;
943962
if (willAddObjectBlock) {
@@ -946,13 +965,7 @@ - (void)setObject:(id <NSCoding>)object forKey:(NSString *)key fileURL:(NSURL **
946965
[self lock];
947966
}
948967

949-
//We unlock here so that we're not locked while serializing.
950-
[self unlock];
951-
NSData *data = _serializer(object, key);
952-
[self lock];
953-
954968
NSError *writeError = nil;
955-
956969
BOOL written = [data writeToURL:fileURL options:writeOptions error:&writeError];
957970
PINDiskCacheError(writeError);
958971

0 commit comments

Comments
 (0)