Velocity Usages and its advantages
Using Velocity and its advantages
It’s a distributed system. Velocity provides highly scalable distributed caching environment for Microsoft .net framework. By using this we can store frequently used data for faster access and also avoids unnecessary calls to the database. Velocity also supports optimistic and pessimistic locking as well as automatic load balancing.
We can use velocity either in windows application or web application.
Microsoft provides velocity in the form of a cluster. In this section we look at what are all those useful elements having cluster, how cluster works and some useful programming with velocity.
In velocity one of the major element is named cache by using this we can store logical grouping of data like a database. A cluster can have any no of named caches. We can use one or more named caches in our applications. We can directly store objects in a cache without creating named cache. In this case those objects are stored in default named cache of the cluster.
We can create or delete the cache using velocity administration tool
Example: Create Cache CacheSample
Delete Cache CacheSample
Here CacheSample is the name of the cache
Another major (optional) element is regions. Actually regions are logical grouping of objects in a named cache like database tables. Regions provide additional searching capabilities. We can create any no of regions on a named cache. Regions are not possible to create using administration tool. We must be creating a region using explicitly run time using application.
We can also check what are all those regions inside the caches and regions available in a cluster using this below command
Example: List Host CacheSample
Velocity allows blindly any time of CLR serialized object in the form of key value pairs
Regions can store any no of objects but depend on size of cluster installation. Anyway once we are added object in a cache that object resides in a cache including version, time to live (TTL) and tags.
The below example describes how to add item to the cache and get the item from cache.
Step 1 First run velocity setup and then references the following .dll from the Program Files\Microsoft Distributed Cache folder: CacheBaseLibrary.dll, ClientLibrary.dll, FabricCommon.dll, CASBase.dll, and CASClient.dll to visual studio .net project.
Step 2 We also need to add the 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 Needs to create a named cache from velocity administration tool
Create Cache CacheSample
Here ‘CacheSample’ 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 main 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
// Name of the region in a named cache
// Key for uniquely identify the cache object
// Cached object(in this case ds)
// Searchable information
// Version information
// Time to alive in cache interms of minutes
cache.Add(“SampleRegion”, “Id”, ds, searchInfo,15);
Put Method
// Name of the region in a named cache
// Key for uniquely identify the cache object
// Cached object(in this case ds)
// Searchable information
// Version information
// Time to alive in cache interms of minutes
cache.Put(“SampleRegion”, “Id”, ds, searchInfo, null, 15);
Get Method
// Name of the region in a named cache
// Key for uniquely identify the cache object
// It returns a cache Item Object.
// That need to cast into appropriate type
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
// Unique key associated with the object.
cache.Remove(“Id”);
2. Removes the Object with specified key from specified region
// Name of the region, cannot be null.
// Unique key associated with the object.
cache.Remove(“SampleRegion”, “Id”);
3. Drop the region with the specified name.
// Name of the region, cannot be null.
// If it returns true then successful other wise returns false.
Cache.RemoveRegion(string region);
Did you enjoy this post? Why not leave a comment below and continue the conversation, or subscribe to my feed and get articles like this delivered automatically to your feed reader.


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 Distributed Cache. NCache Express is a totally free version of NCache. Check it out at Free Distributed Cache.