views:

96

answers:

2

I'm writing a program in C# using the WPF framework. I need to display images, and I'd like to cache them to avoid downloading them constantly.

I can code my own cache, however, IE already has a caching system. I can find code to read entries out of the IE cache, however I've found nothing dealing with the issue of adding items to the cache. Is there a good way to do it, or should I just implement a separate cache?

+2  A: 

I would suggest you implement your own. You can't rely on the IE cache remaining consistent, or not being cleared by the user, or anything else. Unless you're actually embedding the IE browser in a control in your app, I see no reason to use it. Also, using your own cache allows you to create one that could be more fit for purpose (e.g. if you need to store thumbs, or extra metadata, or anything else).

Dan Puzey
Make sure you take steps to prevent the cache from taking up a large amount of disk space and/or allow the user to clear the cache.
Eric J.
+2  A: 

Depending on how you're downloading the files, they may already be getting added to the cache. How are you downloading them now?

You can add items to the IE cache. Natively you have a couple of options: URLDownloadToCacheFile() will do it in one nice "easy" step. CommitUrlCacheEntry() is the hardcore way of doing it. I assume the sample you found uses FindFirst/FindNextUrlCacheEntry() to enumerate the cache, so you should be able to add the interop you need for CommitUrlCacheEntry() fairly easily.

However, as a former member of the IE team, I cannot recommend enough that you should not use the Wininet cache for anything. It is not reliable, it can be cleared out from underneath you, it frequently gets corrupted, it has some hard limits on how many things it can store, it's subject to various rules you don't understand, and it's going to be optimized for IE's usage, not yours.

Seriously, don't do this. If you really need a cache, write your own.

jeffamaphone