Skip to content

Commit

Permalink
[PINDiskCache] Respect small byteLimit settings by checking object si…
Browse files Browse the repository at this point in the history
…ze in setObject: (#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.
  • Loading branch information
appleguy authored and garrettmoon committed Sep 21, 2017
1 parent 3a95246 commit c8a0d67
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* Add your own contributions to the next release on the line below this with your name.

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

## 2.2.1 -- 2016 Mar 5
Expand Down
31 changes: 22 additions & 9 deletions Source/PINDiskCache.m
Original file line number Diff line number Diff line change
Expand Up @@ -838,7 +838,11 @@ - (id)objectForKeyedSubscript:(NSString *)key
{
NSDate *now = [[NSDate alloc] init];

if (!key)
[self lock];
BOOL isEmpty = (_dates.count == 0 && _sizes.count == 0);
[self unlock];

if (!key || isEmpty)
return nil;

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

NSURL *fileURL = [self encodedFileURLForKey:key];

// Remain unlocked here so that we're not locked while serializing.
NSData *data = _serializer(object, key);
NSURL *fileURL = nil;

NSUInteger byteLimit = self.byteLimit;
if (data.length <= byteLimit || byteLimit == 0) {
// The cache is large enough to fit this object (although we may need to evict others).
fileURL = [self encodedFileURLForKey:key];
} else {
// The cache isn't large enough to fit this object (even if all others were evicted).
// We should not write it to disk because it will be deleted immediately after.
if (outFileURL) {
*outFileURL = nil;
}
return;
}

[self lock];
PINCacheObjectBlock willAddObjectBlock = self->_willAddObjectBlock;
if (willAddObjectBlock) {
Expand All @@ -946,13 +965,7 @@ - (void)setObject:(id <NSCoding>)object forKey:(NSString *)key fileURL:(NSURL **
[self lock];
}

//We unlock here so that we're not locked while serializing.
[self unlock];
NSData *data = _serializer(object, key);
[self lock];

NSError *writeError = nil;

BOOL written = [data writeToURL:fileURL options:writeOptions error:&writeError];
PINDiskCacheError(writeError);

Expand Down

0 comments on commit c8a0d67

Please sign in to comment.