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

Huge cache size #45

Closed
MattesGroeger opened this issue Dec 15, 2015 · 5 comments
Closed

Huge cache size #45

MattesGroeger opened this issue Dec 15, 2015 · 5 comments

Comments

@MattesGroeger
Copy link

We are using AwesomeCache and it's great.

Now I implemented it for some bigger images and suddenly noticed a crazy amount of disk usage.

A picture with 414kb takes up almost 5MB on disk (Caches/com.aschuch.cache/images/xxx.cache). Is this something expected?

Help would be much appreciated!

@aschuch
Copy link
Owner

aschuch commented Dec 15, 2015

Hm, this indeed sounds a bit strange. I've not experienced this.
AwesomeCache uses NSCoding and NSKeyedArchiver to persist objects under the hood.

Could you try and persist the picture using NSKeyedArchiver to try and see if the persisted object is of similar size?

@MattesGroeger
Copy link
Author

I'll try that tomorrow.

When I inspected the cached binary I saw NSKeyedArchiver annotations already. Maybe this problem is also specific to JPEG files. Will investigate further tomorrow.

@mbuchetics
Copy link

NSKeyedArchiver will persist the uncompressed UIImage and that's why you see the difference in size compared to the JPEG file. You would need to cache the data returned by UIImageJPEGRepresentation.

@MattesGroeger
Copy link
Author

@mbuchetics Good point! Thanks a lot, that makes sense and I just changed our implementation accordingly:

Use NSData as cached type:

struct Images {
    static let cache = Cache<NSData>(name: "images")
}

Writing to cache:

if let data = UIImageJPEGRepresentation(processedImage, 0.6) {
    Images.cache.setObject(data,
        forKey: imageId,
        expires: cacheExpiry)
}

Reading from cache:

if let imageData = Images.cache[imageId] {
    return UIImage(data: imageData)
}

Now file sizes are under 1MB. Issue can be closed then. Keep up the good work!

@aschuch
Copy link
Owner

aschuch commented Dec 16, 2015

@mbuchetics Thanks for the clarification.

@MattesGroeger Awesome, if you have further questions feel free to reopen. 🎇

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants