I'm doing a document viewer for some document format. To make it easier, let's say this is a PDF viewer, a Desktop application. One requirement for the software is the speed in rendering. So, right now, I'm caching the image for the next pages while the user is scrolling through the document.
This works, the UI is very responsive and it seems like the application is able to render the pages almost instantly....at a cost : the memory usage sometimes goes to 600MB. I cache it all in memory.
Now, I can cache to disk, I know, but doing that all the time is noticeably slower. What I would like to do is implement some cache (LRU?), where some of the cached pages (image objects) are on memory and most of them are on disk.
Before I embark on this, is there something in the framework or some library out there that will do this for me? It seems a pretty common enough problem. (This is a desktop application, not ASP.NET)
Alternatively, do you have other ideas for this problem?
The .NET Framework has always had the ability to keep weak references to objects.
Basically, weak references are references to objects that the runtime considers "unimportant" and that may be removed by a garbage collection run at any point in time. This can be used, for example, to cache things, but you'd have no control over what gets colected and what not.
On the other hand, it's very simple to use and it may just be what you need.
Dave
How are you implementing your cache?
You can use the
Cache
class fromSystem.Web.Caching
, even in non-web applications, and it will purge items on an LRU basis if/when it needs the memory.In a non-web application you'll need to use
HttpRuntime.Cache
to access theCache
instance.Note that the documentation states that the
Cache
class isn't intended to be used outside of ASP.NET, although it's always worked for me. (I've never relied on it in any mission-critical app though.)I wrote an LRU Cache and some test cases, feel free to use it.
You can read through the source on my blog.
For the lazy (here it is minus the test cases):
LRUCache