Velocity Usages and advantages

By Ashish Khandelwal, July 8, 2011

Velocity provides highly scalable distributed caching environment for Microsoft .net framework. By using Velocity, frequently used data can be stored in distributed cached environment for faster access and to avoid unnecessary calls to the database. Velocity also supports locking as well as automatic load balancing.

Velocity can be used for windows application or web application.

Velocity is provided in the form of a cluster. In this section I will cover what are all useful elements, how cluster works and some programming with velocity.

About “Cache” in Velocity

Cache is a one of the major elements of Velocity. Cache can be used to store logical grouping of data like a database.

A cluster can have any number of named caches. We can use one or more named caches in an application. Objects can directly be stored in a cache without creating named cache. In this case those objects are stored in default named cache of the cluster.

Cache can be deleted and crated by using velocity administration tool.Example:
              Create Cache AKCacheSample
              Delete Cache AKCacheSample
                                Here AKCacheSample is the name of the cache

About Region in Velocity

Another major (optional) element is regions. Regions are logical grouping of objects in a named cache like the database tables. It provides additional searching capabilities. Any number of regions can be created on a named cache.

Region can only be created by application runtime. Regions cannot be created using Velocity administration tool.

Below command can be used to check regions available in a cluster
           Example:List Host AKCacheSample

Velocity allows any CLR serialized object and stores in the form of key value pairs. Regions can store any number of objects depend on size of cluster installation. Once we add object in a cache that object resides in a cache including version, time to live (TTL) and tags.

Add item to the cache and get the item from cache

Step 1 Once you have completed the velocity setup add following .dll references from the Program Files\Microsoft Distributed Cache folder:

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

Step 2 Add below configuration information to app.Config file or Web.Config file.

Example:

 

<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>

<dcacheClient deployment=simple localCache=false>

<hosts>

                <!–List of hosts >

                <host name=NameOfCacheServerSystem cachePort=22233

                          cacheHostName=DistributedCacheService />

        </hosts>

</dcacheClient>

<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>

Step 3 Create a named cache from velocity administration tool
            Create Cache AKCacheSample
                     Here ‘AKCacheSample’ is the named cache.

Step 4 Create the instance of the cache factory class and get the named cache from that instance.
           CacheFactory cacheFactory = new CacheFactory();
           Cache cache = cacheFactory.GetCache(“CacheSample “);

 Step 5 create the region
          Cache.CreateRegion(“SampleRegion”,true);

Step 6 Add the item (object) to the region

We can add any CLR serialized object by using Add and Put methods.The difference between Add and Put is Add will throw an error if given key is already exists but put will not throw any exception if the given key is already exists its replace with newer one. On the other hand if the given key is does not exist the object will be added to the cluster.

 ADD Method
         Cache.Add(“SampleRegion”, “Id”, ds, searchInfo,15);

Put Method
        Cache.Put(“SampleRegion”, “Id”, ds, searchInfo, null, 15);

Get Method
         CacheItem item = cache.GetCacheItem(“SampleRegion”, “Id”)

Remove the item from Cache

Remove method deletes object with specified key from the cache.

              1. Remove the object with specified key from cache
                        Cache.Remove(“Id”);

            2. Removes the Object with specified key from specified region
                       Cache.Remove(“SampleRegion”, “Id”);

            3. Drop the region with the specified name.
                      Cache.RemoveRegion(string region);

Here are some of the useful Links for AppFabric – Volacity

Introduction to Caching with Windows Server AppFabric (Beta 1)
http://msdn.microsoft.com/en-us/library/cc645013.aspx
Configure Windows Server AppFabric
http://msdn.microsoft.com/en-us/library/ff637694.aspx
Cache Administration with Windows PowerShell (Windows Server AppFabric Caching)
http://msdn.microsoft.com/en-us/library/ff718177.aspx
Windows Server AppFabric Caching Concepts
http://msdn.microsoft.com/en-us/library/ee790849.aspx
Preparing the Cache Client Development Environment (Windows Server AppFabric Caching)
http://msdn.microsoft.com/en-us/library/ee790876.aspx

One Response to “Velocity Usages and advantages”

  1. How is good at Graphic Design that can design something for me?

Leave a Reply