Velocity — a rockin’ distributed in memory cache for ASP.NET

Velocity, the code-name for a new in-memory distributed caching system for ASP.NET was released as a Community Tech Preview today.

What is it? It’s described in the documentation:

Microsoft project code named “Velocity” provides a highly scalable in-memory application cache for all kinds of data. By using cache, your application performance can improve significantly by avoiding unnecessary calls to the data source.

By using distributed cache, your application can scale to match an increasing demand with increasing throughput. “Velocity” distributed cache is provided in the form of a cache cluster, simplifying your application code by managing the complexities of load balancing behind the scenes.

When you use “Velocity,” you can retrieve data by using keys or other identifiers, called tags.

Download for CTP 1 is here.

Run the installation — and at the end you’ll be asked a few simple starter questions:

image

The settings above can be adjusted later if you don’t like them. Unblock the EXE in your firewall settings if needed (I didn’t need to when using localhost as my destination server as shown below).

image

Installed. Create a new web site in Visual Studio 2008:

image

Add a reference to pull in the necessary settings into your web site:

image

You’ll need to reference a few assemblies. The simplest way to get them all is to reference them directly (rather than copying them as the instructions say). VS 2008 won’t complain.The following files are needed:

CacheBaseLibrary.dll, ClientLibrary.dll, FabricCommon.dll, CASBase.dll, and CASClient.dll

 

image

Next, adjust the web.config file per the instructions.

I tweaked the dcacheClient settings to match those I set when the first installed Velocity.

<dcacheClient deployment="simple" localCache="false">
    <hosts>
        <!--List of hosts -->
        <host name="localhost" cachePort="22233" cacheHostName="MyCluster"/>
    </hosts>
</dcacheClient>

I added the required sections to the configSections in web.config, and also the “fabric” section:

<configuration>
    <configSections>

        <section name="dcacheClient" type="System.Configuration.IgnoreSectionHandler" allowLocation="true" allowDefinition="Everywhere"/>
        <section name="fabric" type="System.Fabric.Common.ConfigFile, FabricCommon" allowLocation="true" allowDefinition="Everywhere"/>

    </configSections>

    <fabric>
        <section name="logging" path="">
            <collection name="sinks" collectionType="list">
                <customType className="System.Fabric.Common.EventLogger,FabricCommon" sinkName="System.Fabric.Common.ConsoleSink,FabricCommon" sinkParam="" defaultLevel="-1"/>
                <customType className="System.Fabric.Common.EventLogger,FabricCommon" sinkName="System.Fabric.Common.FileEventSink,FabricCommon" sinkParam="CacheClientLog" defaultLevel="1"/>
                <customType className="System.Fabric.Common.EventLogger,FabricCommon" sinkName="System.Data.Caching.ETWSink, CacheBaseLibrary" sinkParam="" defaultLevel="-1"/>
            </collection>
        </section>
    </fabric>

</configuration>

Added some code, then run (the code I used is shown below).

 

Unfortunately, this is the first error I encountered:

Access to the path ‘C:\Windows\system32\CacheClientLog.log’ is denied.

Since I’m using the ASP.NET Development Server, I started an administrative instance of notepad and created a blank CacheClientLog.log file in the directory. I then set full permissions on the file for my user account (aaron in my case).

Next, I discovered that the service EXE hadn’t been properly installed. After some false-starts, I rebooted, and again, as an admin user, used the installutil.exe utility from the .NET SDK to install the service (as I had uinstalled it). Using the Services management console, I started the service, “DistributedCacheService.”

Using the Velocity Administration tool, I verified that the host was now running:

image

(Silverwindow is the name of my laptop, a MacBook Pro running Windows Vista).

For reasons I don’t understand, after rebooting, the location of the CacheClientLog.log location changed to here:

C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE.

So, I repeated the task of creating the empty file and associating the proper permissions as described above (again).

Here’s what my sample application looked like.

image

In the default.aspx page for my web site, I added a few controls:

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <
html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>Untitled Page</title> </head> <body> <form id="form1" runat="server"> <div> <asp:TextBox ID="txtToSet" runat="server"></asp:TextBox> <asp:Button ID="btnSetIntoCache" runat="server"
onclick="btnSetIntoCache_Click" Text="Set" /> <br /> <asp:Label ID="lblValue" runat="server"></asp:Label> <asp:Button ID="btnGet" runat="server"
onclick="btnGet_Click" Text="Get" /> </div> </form> </body> </html>

And, then in the code-behind:

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.Caching;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnSetIntoCache_Click(object sender, EventArgs e)
    {
        CacheFactory CacheCluster1 = new CacheFactory();
        Cache cache1 = CacheCluster1.GetCache("default");

        cache1.Add("cacheThis", txtToSet.Text);

    }
    protected void btnGet_Click(object sender, EventArgs e)
    {
        CacheFactory CacheCluster1 = new CacheFactory();
        Cache cache1 = CacheCluster1.GetCache("default");

        lblValue.Text = (string) cache1.Get("cacheThis");
    }
}

 

The default cache I referred to in the test code is called default — as that’s the name that is specified in the ClusterConfig.xml file that was created during the installation. 

    <caches>
      <cache name="default" type="partitioned">
        <policy>
          <eviction type="lru" />
          <expiration isExpirable="true" defaultTTL="10" />
        </policy>
      </cache>
    </caches>    

The location of the ClusterConfig.xml file is currently specified in the DistributedCache.exe.config file, which is installed here by default:

C:\Program Files\Microsoft Distributed Cache\V1.0\DistributedCache.exe.config

Run the web application. Set some value into the cache (using the “Set” button). Get it back out (“Get” button). Shut down the ASP.NET development server. Run the application again — you’ll see how the cached value is still there (assuming you haven’t waited too long). Cool.

The cache can also store any .NET serializable object — not just strings.

Microsoft project code named “Velocity” provides a highly scalable in-memory application cache for all kinds of data. By using cache, your application performance can improve significantly by avoiding unnecessary calls to the data source.

By using distributed cache, your application can scale to match an increasing demand with increasing throughput. “Velocity” distributed cache is provided in the form of a cache cluster, simplifying your application code by managing the complexities of load balancing behind the scenes.

When you use “Velocity,” you can retrieve data by using keys or other identifiers, called tags.

Check it out for yourself. This is a great addition to ASP.NET and truly puts ASP.NET into the competitive ‘big-scale’ enterprise application market without the need for expensive third party products.

4 Comments

  1. Although Velocity has made progress from CTP1 to CTP2, it still leaves much to be desired. It will be some time before they provide all the important features in a distributed cache and even longer before it is tested in the market. I wish them good luck.

    In the meantime, NCache already provides all CTP2 & V1, and many more features. NCache is the first, the most mature, and the most feature-rich distributed cache in the .NET space. NCache is an enterprise level in-memory distributed cache for .NET and also provides a distributed ASP.NET Session State. Check it out at http://www.alachisoft.com.

    NCache Express is a totally free version of NCache. Check it out at http://www.alachisoft.com/rp.php?dest=/ncache/ncache_express.html

    1. “Jack” — I removed your URL.

      They can’t be compared as they don’t serve the same purposes. It’s like asking if grabbing a value from RAM is faster than pulling it from disk. It all depends. Velocity is a distributed cache, designed to provide high availability. ASP.NET Cache — not at all.

Comments are closed.