Blog Post

Introducing the Couchbase ASP.NET OutputCache Provider

John Zablocki Published

As was reiterated in a recent post by A Main Hobbies CEO Kendall Bennett, Couchbase is a great solution for improving the performance of your ASP.NET applications.  Beyond offering sub-millisecond CRUD operations for your application's persistence  needs, Couchbase also offers many benefits for your application's transient data needs. 

In addition to managing your ASP.NET session state with Couchbase Server, you can now use Couchbase Server as the backing store for your application's output cache.  The latest commits to the Couchbase.AspNet project on Couchbase Labs includes the CouchbaseOutputCacheProvider.

A brief aside for those interested in implementation details or for those who have implemented an output cache provider...  It turns out that Couchbase Server is well suited for use as an output cache backing store.  With its time-to-live capabilities, creating the CouchbaseOutputCacheProvider class required very few lines of code.  There was no need to create a wrapper around the cache item just to add a persisted expiration field.  There was no need to check the expiration of an item after retrieving it, since it wouldn't be returned by Couchbase Server if expired. 

To use the new provider, you'll configure the client as you always do:

<section name="couchbase" type="Couchbase.Configuration.CouchbaseClientSection, Couchbase"/>

<couchbase>
    <servers bucket="default" bucketPassword="">
        <add uri="http://127.0.0.1:8091/pools"/>
    </servers>
</couchbase>

Then configure the outputCache provider section:

<code><outputCache defaultProvider="CouchbaseCache">
    <providers>
        <add name="CouchbaseCache" type="Couchbase.AspNet.OutputCache.CouchbaseOutputCacheProvider, Couchbase.AspNet" section="couchbase-caching"/>
    </providers>
</outputCache></code><outputCache defaultProvider="CouchbaseCache">
 <providers>
 <add name="CouchbaseCache" type="Couchbase.AspNet.OutputCache.CouchbaseOutputCacheProvider, Couchbase.AspNet" section="couchbase-caching"/>
 </providers>
</outputCache>

Then configure OutputCache on your actions as you normally would (ASP.NET MVC):

[OutputCache(Duration = 60, VaryByParam="foo")]
public ActionResult Time(string foo)
{
    return Content(DateTime.Now.ToString());
}

Or in ASP.NET WebForms, use the page directive:

<%@ OutputCache Duration="60" VaryByParam="foo" %>

If you're already using Couchbase in your app and need to specify a custom section, simply provide a new section name and set a "section" attribute on the "add" node of the "providers" element as below:

<section name="couchbaseSession" type="Couchbase.Configuration.CouchbaseClientSection, Couchbase"/>

<couchbaseSession>
    <servers bucket="sessionState" bucketPassword="">
        <add uri="http://127.0.0.1:8091/pools"/>
    </servers>
</couchbaseSession>

<outputCache defaultProvider="CouchbaseCache">
  <providers>
    <add name="Couchbase" type="Couchbase.AspNet.SessionState.CouchbaseSessionStateProvider, Couchbase.AspNet" section="couchbaseSession" />
  </providers>
</outputCache><section name="couchbaseSession" type="Couchbase.Configuration.CouchbaseClientSection, Couchbase"/>

<couchbaseSession>
 <servers bucket="sessionState" bucketPassword="">
 <add uri="http://127.0.0.1:8091/pools"/>
 </servers>
</couchbaseSession>

<outputCache defaultProvider="CouchbaseCache">
 <providers>
 <add name="Couchbase" type="Couchbase.AspNet.SessionState.CouchbaseSessionStateProvider, Couchbase.AspNet" section="couchbaseSession" />
 </providers>
</outputCache>

You're free to use the code or the Nuget package.  This is a Couchbase Labs project with the  Apache License 2.0.  Just remember though, that this isn't an officially supported Couchbase product.  Feel free to post questions to the forums though.   

Happy output-caching!