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

optimize cache size calculation #35

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
20 changes: 18 additions & 2 deletions LKImageKit/Components/Cache/LKImageMemoryCache.mm
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
{
string key;
UIImage *image;
int64_t cost;
};

struct ImagePointer
Expand All @@ -38,6 +39,8 @@ @interface LKImageMemoryCache ()
map<string, ImagePointer *> imageMap;
}

@property (nonatomic, assign) int64_t currentCost;

@end

@implementation LKImageMemoryCache
Expand Down Expand Up @@ -84,6 +87,7 @@ - (void)clear
FIFOQueue.clear();
LRUQueue.clear();
imageMap.clear();
self.currentCost = 0;
}

- (void)clearWithURL:(NSString *)URL
Expand Down Expand Up @@ -111,7 +115,10 @@ - (void)deleteCache:(string)key
return;
}
ImagePointer *ptr = it->second;
ImageNode *node = *ptr->it;
self.currentCost -= node->cost;
delete *(ptr->it);
delete node;
if (ptr->isLRUQueue)
{
LRUQueue.erase(ptr->it);
Expand All @@ -136,7 +143,7 @@ - (NSString *)keyForURL:(NSString *)URL

- (void)limitCacheSize
{
while (self.cacheSize > self.cacheSizeLimit)
while (self.currentCost > self.cacheSizeLimit)
{
[self clearLastOne];
}
Expand All @@ -160,6 +167,7 @@ - (void)clearLastOneInLRU
{
ImageNode *node = *LRUQueue.begin();
LRUQueue.pop_front();
self.currentCost -= node->cost;
auto it = imageMap.find(node->key);
delete it->second;
delete node;
Expand All @@ -173,6 +181,7 @@ - (void)clearLastOneInFIFO
{
ImageNode *node = *FIFOQueue.begin();
FIFOQueue.pop_front();
self.currentCost -= node->cost;
auto it = imageMap.find(node->key);
delete it->second;
delete node;
Expand All @@ -185,28 +194,35 @@ - (void)cacheImage:(UIImage *)image URL:(NSString *)URL
NSString *key = [self keyForURL:URL];
ImagePointer *ptr = NULL;
auto it = imageMap.find([key cStringUsingEncoding:NSUTF8StringEncoding]);
int64_t cost = [self imageSize:image accurate:NO];
if (it == imageMap.end())
{
ImageNode *node = new ImageNode();
ptr = new ImagePointer();
node->image = image;
node->cost = cost;
node->key = [key cStringUsingEncoding:NSUTF8StringEncoding];
ptr->it = FIFOQueue.insert(FIFOQueue.end(), node);
imageMap[node->key] = ptr;
if (FIFOQueue.size() > self.maxLengthForFIFO)
{
ImageNode *node = *FIFOQueue.begin();
FIFOQueue.pop_front();
self.currentCost -= node->cost;
auto it = imageMap.find(node->key);
delete it->second;
delete node;
imageMap.erase(it);
}
self.currentCost += cost;
}
else
{
ptr = it->second;
(*ptr->it)->image = image;
self.currentCost -= (*ptr->it)->cost;
(*ptr->it)->cost = cost;
self.currentCost += cost;
[self visit:key];
}
[self limitCacheSize];
Expand Down Expand Up @@ -247,7 +263,7 @@ - (int64_t)imageSize:(UIImage*)image accurate:(BOOL)accurate

- (int64_t)cacheSize
{
return [self cacheSize:NO];
return self.currentCost;
}

- (int64_t)cacheSize:(BOOL)accurate
Expand Down