When working with ASP.NET Web Application, dynamic image generation requires time and server resources. In order to reduce response time and server resources, ImageDraw controls provide built-in caching support based on ASP.NET Caching Technology as well as alternate temporary storages for the generated images such as Disk and SQL Server Database.
In This Section:
Caching and Alternate Temporary Storages for Output Images
Memory Caching
Using this caching mechanism, ImageDraw controls will generate the output composite image once per caching period, saving it in the server memory cache. When caching feature is enabled, all image requests will be served from the memory cache. Your application will gain in performance because on each image request, rather than generating the composite image from scratch - a task that consumes time and server resources, the output image is ready to be served from the memory producing faster responses.
How to configure Memory Caching feature:
How to configure Memory Caching feature:
- Set the CachingMethod property to Memory
- The output composite image will be stored in the cache for a given period of time which can be specified by the following properties:
- CacheExpiresInSeconds
Set this property to the number of seconds that the output image will be stored in cache. It means that if you set this property to 3600 (1 hour), the output image will be stored in the cache for 3600 seconds. When the period of time ends, the output image is removed from the cache; the involved ImageDraw control re-generates the composite image, and finally the composite image is stored in the cache for another 3600 seconds. - CacheExpiresAtDateTime
Set this property to a DateTime value. It means that the output image will be stored in the memory cache until the specified DateTime value. After that time, the output image is removed from the cache.
- CacheExpiresInSeconds
Note
If both properties CacheExpiresAtDateTime and CacheExpiresInSeconds are specified, the first one takes precedence. To disable CacheExpiresAtDateTime property set it to DateTime.MinValue
If both properties CacheExpiresAtDateTime and CacheExpiresInSeconds are specified, the first one takes precedence. To disable CacheExpiresAtDateTime property set it to DateTime.MinValue
Disk Temporary Storage (*)
Sometimes you won't be able to leverage server's memory for output image storage. ImageDraw features Disk temporary storage that you can use for saving output generated images in server's disk instead of using memory. When Disk caching feature is enabled, ImageDraw will generate the output image and save it on disk once, and on subsequent requests the image file is directly served from disk (rather than generating the composite image from scratch) producing faster responses.
How to configure Disk Caching feature:
How to configure Disk Caching feature:
- Set the CachingMethod property to Disk
- The output composite image will be stored in a local folder under your web app root that you must set up in the appSettings section of web.config file. For example, the following setting is for storing image files in a folder named MyDiskCache which is located under web app root:
<configuration> <appSettings> <add key="ImageDrawDiskCacheDirectory" value="~/MyDiskCache"/> </appSettings> ... </configuration>
- You must grant write permissions for the ASP.NET account (which your web app is running on) on the folder used for Disk caching.
- Always set a unique value to OutputImageName property of ImageDraw controls, otherwise a random token will be used for the image file name.
SQL Server Database Temporary Storage (*)
If you won't be able to leverage server's memory or disk for output image storage, then you can try SQL Server Database option. ImageDraw features SQLServerDb temporary storage that you can use for saving output generated images in a SQL Server Database Table. You can use an existing SQL Server Table or create a new one for this purpose. ImageDraw uses ADO.NET SQL Server provider behind the scenes for data access, and thus why the SQL Server must be Version 7.0 or greater (including Expression Editions). When SQL Server caching feature is enabled, ImageDraw will generate the output image and store it on the selected SQL Server Database once, and on subsequent requests the binary content of the image is directly served from the database server (avoiding the composite image being generating from scratch). SQL Server option can be considered as a solution for Web Farm scenarios.
How to configure SQL Server Caching feature:
How to configure SQL Server Caching feature:
- Set the CachingMethod property to SQLServerDb
- The output composite image will be stored in a SQL Server Table that you must set up in the appSettings section of web.config file.
The following entries must be set up for SQL Server caching option:
- ImageDrawDBCacheConnectionString
This must refer to the name of the database connection string stored in the connectionStrings section of web.config file - ImageDrawDBCacheTableName
This is the name of the SQL Table located in the database which the above setting points out - ImageDrawDBCacheImageIdColumn
This is the name of the table's column used for identifying each image that will be stored. This column must be a varchar SQL Type which size will depend on the length of the strings used for image identifiers. - ImageDrawDBCacheImageFormatColumn
This is the name of the table's column used for storing the MIME image format (Examples: images/png, images/jpeg, etc) for each image. This column must be a varchar SQL Type which size can be set to 50. - ImageDrawDBCacheImageBlobColumn
This is the name of the table's column used for storing binary content for each image. This column must be a image SQL Type (BLOB).
For example, the following setting is for storing images in a table called ImageCache of a SQL Server 2005 Express database DbTest.mdf which is located under App_Data folder of web app:NOTE
You can use an existing SQL Server Database Table. However, the three needed columns ImageDrawDBCacheImageIdColumn, ImageDrawDBCacheImageFormatColumn, and ImageDrawDBCacheImageBlobColumn must be created in the existing table.
A SQL Server Database sample for SQL Server caching option
The following settings are based on the database sample:
<configuration> <appSettings> <add key="ImageDrawDBCacheConnectionString" value="DbTest"/> <add key="ImageDrawDBCacheTableName" value="ImageCache"/> <add key="ImageDrawDBCacheImageIdColumn" value="Id"/> <add key="ImageDrawDBCacheImageFormatColumn" value="MIME"/> <add key="ImageDrawDBCacheImageBlobColumn" value="ImageData"/> </appSettings> <connectionStrings> <add name="DbTest" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\DbTest.mdf;Integrated Security=True;User Instance=True" providerName="System.Data.SqlClient"/> </connectionStrings> ... </configuration>
- ImageDrawDBCacheConnectionString
- Always set a unique value to OutputImageName property of ImageDraw controls, otherwise a random token will be used for the image id which will be stored in the column specified by ImageDrawDBCacheImageIdColumn setting.
(*) Both Disk and SQLServerDb "caching" options are not truly caching features because there's no built-in support for caching dependencies (the ability to invalidate the cache if data in it is changed) and thus why we call them "Temporary Storages" instead of "Caching" in this documentation.