So far I've been unable to find an answer to this question:
Is it possible to force cached content to be written to the disk in addition
to be kept in memory?
Here is what I'm trying to do:
We have about 100,000 images (e-commerce) to handle. The app will create
100px, 250px, 400px, etc - size images. That all works very well, except
that it doesn't sound feasible to have the Cache keep all that in memory.
So, I'm trying to determine how to write those altered images (altered via
querystring) onto the disk while still linked to the cache. I could think of
several ways to accomplish a home-cooked cache application, but I'd like to
concentrate on the .NET Cache object for this question.
Here's my ideal solution:
- inside the app all I'm doing is checking against the cache whether an
image exists or not and re-create depending on the result
- if an item does NOT exist, I will create one and write it into the cache.
- the cache will store it in memory AND write it onto disk.
- now, if the object gets purged out of memory next time I request that
image the cache could immediately go back to the disk and grab the object
from there without me having to go through the expensive creation process.
- the biggest thing is that this way I could still use all of the
dependencies that are so handy!!
Any ideas?
Alvin Bruney - 04 Jan 2004 00:30 GMT
You need a couple of steps here. You would need a cache call back delegate.
This allows you to repopulate the cache when the runtime removes items from
the cache. When this happens, your call back function triggers and you would
then be responsible for repopulating the cache with the item from disk.
You will need to enhance your logic (wrapping the cache object in a class
for instance) so that cache accesses check the cache object first and on a
miss, goes searching for the item on disk. Finding the item on disk
replenishes the cache and returns the info at the same time.
You can either overload the add and insert methods of the cache object or
provide your own wrapper class. It's up to you.
//code provided complements MSDN
static bool itemRemoved = false;
static CacheItemRemovedReason reason;
CacheItemRemovedCallback onRemove = null;
public void RemovedCallback(String k, Object v, CacheItemRemovedReason
r){
itemRemoved = true;
reason = r;
}
public void AddItemToCache(Object sender, EventArgs e) {
itemRemoved = false;
onRemove = new CacheItemRemovedCallback(this.RemovedCallback);
if (Cache["Key1"] == null)
Cache.Add("Key1", "Value 1", null, DateTime.Now.AddSeconds(60),
TimeSpan.Zero, CacheItemPriority.High, onRemove);
}
public void RemoveItemFromCache(Object sender, EventArgs e) {
if(Cache["Key1"] != null)
Cache.Remove("Key1");
}
//code complements MSDN

Signature
Regards,
Alvin Bruney
Got tidbits? Get it here...
http://tinyurl.com/2bz4t
> So far I've been unable to find an answer to this question:
>
[quoted text clipped - 25 lines]
>
> Any ideas?