mirror of
https://github.com/zhigang1992/RestKit.git
synced 2026-04-23 12:27:52 +08:00
Fix Cocoa 512 errors logged from RKRequestCache. fixes #246
The following changes were made: * Added isCacheable to RKRequest * Return nil for cacheKey on non-cacheable RKRequests * Updated RKRequestCache to ensure attempts to cache uncacheable requests has no effect * Added basic unit tests and expanded comments on some parts of the cache API
This commit is contained in:
@@ -180,11 +180,36 @@ typedef enum RKRequestBackgroundPolicy {
|
||||
/// @name Cacheing
|
||||
/////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/**
|
||||
Returns the cache key for getting/setting the cache entry for this request
|
||||
in the cache.
|
||||
|
||||
The cacheKey is an MD5 value computed by hashing a combination of the destination
|
||||
URL, the HTTP verb, and the request body (if possible)
|
||||
*/
|
||||
@property (nonatomic, readonly) NSString* cacheKey;
|
||||
|
||||
/**
|
||||
The cache policy used when storing this request into the request cache
|
||||
*/
|
||||
@property (nonatomic, assign) RKRequestCachePolicy cachePolicy;
|
||||
|
||||
/**
|
||||
The request cache to store and load responses for this request
|
||||
|
||||
Generally configured by the RKClient instance that minted this request
|
||||
*/
|
||||
@property (nonatomic, retain) RKRequestCache* cache;
|
||||
|
||||
/**
|
||||
Returns YES if the request is cacheable
|
||||
|
||||
All requets are considered cacheable unless:
|
||||
1) The method is DELETE
|
||||
2) The request body is a stream (i.e. using RKParams)
|
||||
*/
|
||||
- (BOOL)isCacheable;
|
||||
|
||||
/**
|
||||
* The HTTP body as a NSData used for this request
|
||||
*/
|
||||
|
||||
@@ -562,20 +562,36 @@
|
||||
#endif
|
||||
}
|
||||
|
||||
- (NSString*)cacheKey {
|
||||
- (BOOL)isCacheable {
|
||||
// DELETE is not cacheable
|
||||
if (_method == RKRequestMethodDELETE) {
|
||||
return NO;
|
||||
}
|
||||
|
||||
// Multi-part file uploads are not cacheable
|
||||
if (_params && ![_params respondsToSelector:@selector(HTTPBody)]) {
|
||||
return NO;
|
||||
}
|
||||
|
||||
return YES;
|
||||
}
|
||||
|
||||
- (NSString*)cacheKey {
|
||||
if (! [self isCacheable]) {
|
||||
RKLogDebug(@"Asked to return cacheKey for uncacheable request: %@", self);
|
||||
return nil;
|
||||
}
|
||||
|
||||
// Use [_params HTTPBody] because the URLRequest body may not have been set up yet.
|
||||
NSString* compositCacheKey = nil;
|
||||
if (_params) {
|
||||
if ([_params respondsToSelector:@selector(HTTPBody)]) {
|
||||
compositCacheKey = [NSString stringWithFormat:@"%@-%d-%@", self.URL, _method, [_params HTTPBody]];
|
||||
}
|
||||
NSString* compositeCacheKey = nil;
|
||||
if (_params && [_params respondsToSelector:@selector(HTTPBody)]) {
|
||||
compositeCacheKey = [NSString stringWithFormat:@"%@-%d-%@", self.URL, _method, [_params HTTPBody]];
|
||||
} else {
|
||||
compositCacheKey = [NSString stringWithFormat:@"%@-%d", self.URL, _method];
|
||||
compositeCacheKey = [NSString stringWithFormat:@"%@-%d", self.URL, _method];
|
||||
}
|
||||
return [compositCacheKey MD5];
|
||||
|
||||
NSAssert(compositeCacheKey, @"Expected a cacheKey to be generated for request %@, but got nil", compositeCacheKey);
|
||||
return [compositeCacheKey MD5];
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
@@ -89,6 +89,10 @@ static NSDateFormatter* __rfc1123DateFormatter;
|
||||
}
|
||||
|
||||
- (NSString*)pathForRequest:(RKRequest*)request {
|
||||
if (! [request isCacheable]) {
|
||||
return nil;
|
||||
}
|
||||
|
||||
[_cacheLock lock];
|
||||
|
||||
NSString* pathForRequest = nil;
|
||||
@@ -109,6 +113,10 @@ static NSDateFormatter* __rfc1123DateFormatter;
|
||||
}
|
||||
|
||||
- (BOOL)hasResponseForRequest:(RKRequest*)request {
|
||||
if (! [request isCacheable]) {
|
||||
return NO;
|
||||
}
|
||||
|
||||
[_cacheLock lock];
|
||||
|
||||
BOOL hasEntryForRequest = NO;
|
||||
@@ -138,6 +146,10 @@ static NSDateFormatter* __rfc1123DateFormatter;
|
||||
}
|
||||
|
||||
- (void)storeResponse:(RKResponse*)response forRequest:(RKRequest*)request {
|
||||
if (! [request isCacheable]) {
|
||||
return;
|
||||
}
|
||||
|
||||
[_cacheLock lock];
|
||||
|
||||
if ([self hasResponseForRequest:request]) {
|
||||
@@ -186,6 +198,10 @@ static NSDateFormatter* __rfc1123DateFormatter;
|
||||
}
|
||||
|
||||
- (RKResponse*)responseForRequest:(RKRequest*)request {
|
||||
if (! [request isCacheable]) {
|
||||
return nil;
|
||||
}
|
||||
|
||||
[_cacheLock lock];
|
||||
|
||||
RKResponse* response = nil;
|
||||
@@ -206,6 +222,9 @@ static NSDateFormatter* __rfc1123DateFormatter;
|
||||
}
|
||||
|
||||
- (NSDictionary*)headersForRequest:(RKRequest*)request {
|
||||
if (! [request isCacheable]) {
|
||||
return nil;
|
||||
}
|
||||
NSString* cachePath = [self pathForRequest:request];
|
||||
|
||||
[_cacheLock lock];
|
||||
@@ -228,6 +247,9 @@ static NSDateFormatter* __rfc1123DateFormatter;
|
||||
}
|
||||
|
||||
- (NSString*)etagForRequest:(RKRequest*)request {
|
||||
if (! [request isCacheable]) {
|
||||
return nil;
|
||||
}
|
||||
NSString* etag = nil;
|
||||
|
||||
NSDictionary* responseHeaders = [self headersForRequest:request];
|
||||
@@ -247,6 +269,9 @@ static NSDateFormatter* __rfc1123DateFormatter;
|
||||
}
|
||||
|
||||
- (void)setCacheDate:(NSDate*)date forRequest:(RKRequest*)request {
|
||||
if (! [request isCacheable]) {
|
||||
return;
|
||||
}
|
||||
NSMutableDictionary* responseHeaders = [[self headersForRequest:request] mutableCopy];
|
||||
|
||||
[responseHeaders setObject:[[RKRequestCache rfc1123DateFormatter] stringFromDate:date]
|
||||
@@ -257,6 +282,9 @@ static NSDateFormatter* __rfc1123DateFormatter;
|
||||
}
|
||||
|
||||
- (NSDate*)cacheDateForRequest:(RKRequest*)request {
|
||||
if (! [request isCacheable]) {
|
||||
return nil;
|
||||
}
|
||||
NSDate* date = nil;
|
||||
NSString* dateString = nil;
|
||||
|
||||
@@ -278,6 +306,10 @@ static NSDateFormatter* __rfc1123DateFormatter;
|
||||
}
|
||||
|
||||
- (void)invalidateRequest:(RKRequest*)request {
|
||||
if (! [request isCacheable]) {
|
||||
return;
|
||||
}
|
||||
|
||||
[_cacheLock lock];
|
||||
RKLogDebug(@"Invalidating cache entry for '%@'", request);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user