Speeding up ISAPI applications with request caching

Posted: (EET/GMT+2)

 

ISAPI extensions in the IIS server allow fast web applications to be implemented. But sometimes, you need to avoid that SQL database query or file system access to improve performance. Reducing response times in an ISAPI extension can be done with caching, especially if you repeatedly return the same data. Caching frequently requested content in memory can make a big difference in performance.

ISAPI extensions run inside the IIS worker process, so you can store data in process memory and reuse it across requests.

A simple approach is to cache generated output for repeated requests:

// pseudocode
if (cache.Contains(key))
{
    WriteResponse(cache[key]);
    return;
}

data = GenerateResponse();
cache[key] = data;
WriteResponse(data);

The cache key can be based on the requested URL or query string.

For thread safety, protect shared data structures:

CRITICAL_SECTION cs;
EnterCriticalSection(&cs);
// ...access cache here

LeaveCriticalSection(&cs);

You can also add a simple expiration policy (again pseudocode):

if (Now - cache[key].timestamp > 60 seconds)
{
    refresh entry;
}

This avoids serving stale data for too long while still reducing load.

Caching is very handy for pages that are expensive to generate but do not change on every request. And in ISAPI applications, caching is easy to implement, too.