Fork me on GitHub

When you generate dynamic images on a website, you almost certainly want to cache the resulting images. DynamicImage defaults to using the built-in XmlCacheProvider, but you can write your own. Using the default provider, DynamicImage will store cached images in ~/App_Data/DynamicImage/. When a request comes in for the same set of layers and filters, DynamicImage will server the cached image instead of generating a new one. You can customise the cache path in web.config:

<?xml version="1.0"?>
<configuration>
	<configSections>
		<sectionGroup name="soundInTheory">
			<section name="dynamicImage" 
				type="SoundInTheory.DynamicImage.Configuration.DynamicImageSection" />
		</sectionGroup>
	</configSections>
	<soundInTheory>
		<dynamicImage>
			<caching mode="Custom" customProvider="XmlCacheProvider" storeMissingImagesInCache="false">
				<providers>
					<add name="XmlCacheProvider"
						type="SoundInTheory.DynamicImage.Caching.XmlCacheProvider, SoundInTheory.DynamicImage"
						cachePath="~/MyCustomCacheFolder" />
				</providers>
			</caching>
		</dynamicImage>
	</soundInTheory>
</configuration>

It is safe to delete the contents of the cache folder (i.e. ~/App_Data/DynamicImage/). DynamicImage will recreate and re-cache images as needed.

To implement your own cache provider, you'll need to inherit from the following class:

public abstract class DynamicImageCacheProvider : ProviderBase
{
	public abstract bool ExistsInCache(string cacheKey);
	public abstract void AddToCache(string cacheKey, GeneratedImage generatedImage, 
		Dependency[] dependencies);
	public abstract ImageProperties GetPropertiesFromCache(string cacheKey);
	public abstract void RemoveAllFromCache();
	public abstract void RemoveFromCache(Dependency dependency);

	public abstract DateTime GetImageLastModifiedDate(HttpContext context, 
		string cacheKey, string fileExtension);
	public abstract void SendImageToHttpResponse(HttpContext context, 
		string cacheKey, string fileExtension);
}