Add imageFromMemoryCacheForKey: method to synchronously query the memory cache (fix #263)

This commit is contained in:
Olivier Poitrey 2013-01-16 11:41:59 +01:00
parent 555a320b9e
commit 1e53e91513
3 changed files with 14 additions and 3 deletions

View File

@ -145,8 +145,7 @@ SDImageCache *imageCache = [SDImageCache.alloc initWithNamespace:@"myNamespace"]
```
By default SDImageCache will lookup the disk cache if an image can't be found in the memory cache.
You can prevent this from happening by calling the alternative method imageFromKey:fromDisk: with a
negative second argument.
You can prevent this from happening by calling the alternative method `imageFromMemoryCacheForKey:`.
To store an image into the cache, you use the storeImage:forKey: method:

View File

@ -87,6 +87,13 @@ typedef enum SDImageCacheType SDImageCacheType;
*/
- (void)queryDiskCacheForKey:(NSString *)key done:(void (^)(UIImage *image, SDImageCacheType cacheType))doneBlock;
/**
* Query the memory cache.
*
* @param key The unique key used to store the wanted image
*/
- (UIImage *)imageFromMemoryCacheForKey:(NSString *)key;
/**
* Remove the image from memory and disk cache synchronousely
*

View File

@ -150,6 +150,11 @@ static const NSInteger kDefaultCacheMaxCacheAge = 60 * 60 * 24 * 7; // 1 week
[self storeImage:image imageData:nil forKey:key toDisk:toDisk];
}
- (UIImage *)imageFromMemoryCacheForKey:(NSString *)key
{
return [self.memCache objectForKey:key];
}
- (void)queryDiskCacheForKey:(NSString *)key done:(void (^)(UIImage *image, SDImageCacheType cacheType))doneBlock
{
if (!doneBlock) return;
@ -161,7 +166,7 @@ static const NSInteger kDefaultCacheMaxCacheAge = 60 * 60 * 24 * 7; // 1 week
}
// First check the in-memory cache...
UIImage *image = [self.memCache objectForKey:key];
UIImage *image = [self imageFromMemoryCacheForKey:key];
if (image)
{
doneBlock(image, SDImageCacheTypeMemory);