As you probably know, you can implement caching in an ASPX page using the OutputCache directive. However, you can't use this in an ASHX handler. So what if you are using a handler to server binary files from the database and you want to improve peformance by using server side output caching? You'll just need to add some of your own caching logic in your code-behind. Here is some sample code of how you can do this. In this very basic example we are passing the ID of our object on the query string.
// Get ID from query string
int myID = Convert.ToInt32(context.Request.QueryString["ID"]);
// Attempt to get item from cache if it exists
string myCacheKey = "MyKey" + myID; // Unique Identifier
MyObject myObject = context.Cache[myCacheKey] as MyObject;
if (myObject == null)
{
// Item isn't cached; Grab business object from the database
myObject = MyObject.GetMyObject(myID);
// Add object to Cache using the cache key
// In this example we are caching for 30 minutes
context.Cache.Insert(myCacheKey, myObject,
null, DateTime.UtcNow.AddMinutes(30),
Cache.NoSlidingExpiration);
}
// Code to generate file from binary data here
Below is the syntax you can use in the Properties window of the LINQ Designer to map to an enum. Set this under the Type property and use the fully qualified name of your enum.
Example: global::MyClassLibrary.MyStuff.MyClassName
Screenshot: