PBMining

Searching...
Tuesday, February 7, 2012

Create-Read-Update-Delete Operations in Azure Caching Velocity

3:28 AM

what if you want to update a single item in object that is in distributed cache,as with default functinality of Azure Caching you have to get whole object from the cache and updating its subItem and again store it back in cache. when stored object is small it doesn't make a sence,but what is object contain 1000000 items and you want to update only single item of them,it create performance issue.
problem with updating object's item in azure caching

Because azure caching not pass the reference of particular object,We have to reference it and updating it by referece.In this post i am going to explain how to reference caching object by simple CRUD example using MVC3 And Azure Distributed Cache Velocity.

you can download example code here.AppFabricCachingDemo

Let's start by creating a Simple Cache using velocity Powershell Administration Tool.

start administration tool and Type this Command: New-Cache -N peoples -TTL 30 -Notif .  We are going to use peoples Cache for storing data for our example with Notification nable,and with lifetime 30 minutes.

Step-1:Create new MVC3 Empty Web Application.
Creating new MVC3 Application

Select empty template from project dialog.with Razor view engine and Html5 markup support enable.

with empty project template

Step-2:Add Cache library Dll reference to your application.

add cache library dll reference

add below Azure caching configuration settings to configuration Section on first Web.config file relative to your project.

   
  <!--configSections must be the FIRST element -->
  <configSections>

<!-- required to read the <dataCacheClient> element -->
<section name="dataCacheClient" type="Microsoft.Data.Caching.DataCacheClientSection,
   CacheBaseLibrary" allowLocation="true" allowDefinition="Everywhere"/>

<!-- required to read the <fabric> element, when present -->
<section name="fabric" type="System.Data.Fabric.Common.ConfigFile,
   FabricCommon" allowLocation="true" allowDefinition="Everywhere"/>

  </configSections>

  <!-- routing client-->
  <dataCacheClient deployment="routing">

<!-- (optional) specify local cache  -->
<localCache isEnabled="true" sync="notificationbased" objectCount="100000" ttlValue="300"/>


<!--(optional) specify cache notifications poll interval,pollinterval parameter(in second) -->
<clientNotification pollInterval="10"/>


<!-- cache host(s) -->
<hosts>
  <host name="OM" cachePort="22233" cacheHostName="DistributedCacheService"/>

</hosts>
  </dataCacheClient>




Step-3: Right Click on Controller folder on solution explorer -> Add -> Controller.(Give name CacheTest ,Select empty controller template.)

Add new controller
Step-4: Create Three Classes in model folder,one is CacheHelper.vb,second is PeopleInfo.vb,and third is for Shared DataCacheFactory Dcf.vb .

Copy this code in CacheHelper.vb

  Imports Microsoft.Data.Caching

   Public Class CacheHelper

   Private peoples As DataCache
   Private Shared z As Integer

     Sub New()
           
    peoples = Dcf.instance.GetCache("peoples")

    End Sub

     Public Property PeoplesInfo As List(Of PeopleInfo)
       Get
         Return CType(peoples.Get("peopleinfo"), List(Of PeopleInfo))

       End Get

       Set(ByVal value As List(Of PeopleInfo))

        peoples.Put("peopleinfo", value)

      End Set

      End Property


      Sub Delete(ByVal id As Integer)
      Delete2(PeoplesInfo, id)
      End Sub

     Private Sub Delete2(ByRef cache As IList(Of PeopleInfo), ByVal id As Integer)

     Dim pi As PeopleInfo = cache.Where(Function(x) x.PeopleID = id).FirstOrDefault
     cache.Remove(pi)

     End Sub

      Function GetPeoples() As IEnumerable(Of PeopleInfo)

     If PeoplesInfo Is Nothing OrElse PeoplesInfo.Count < 1 Then

        CacheHelper.z = 0
        PeoplesInfo = CreatePeoples()

      End If


      Return PeoplesInfo

      End Function
   
   Sub ClearCache()

   Clear(PeoplesInfo)

   End Sub

  Private Sub Clear(ByRef cache As IList(Of PeopleInfo))

    cache.Clear()
     CacheHelper.z = 0

  End Sub

  Public Sub addSomePeoples()

     Dim tempPeoples As List(Of PeopleInfo) = PeoplesInfo
     tempPeoples.AddRange(CreatePeoples())
     PeoplesInfo = tempPeoples

  End Sub

   Private Function CreatePeoples() As List(Of PeopleInfo)

      Dim peoplesInfo As New List(Of PeopleInfo)
   
   Dim p As PeopleInfo

    For i = CacheHelper.z + 1 To CacheHelper.z + 4

 p = New PeopleInfo With {.PeopleID = i, .Name = "pravin" & i, .Address = "madhapar", .ContactNo = "254321221"}
   peoplesInfo.Add(p)

   Next

   CacheHelper.z += 4
    Return peoplesInfo

   End Function

  Function GetInfoByID(ByVal id As Integer) As PeopleInfo
   Return PeoplesInfo.Where(Function(x) x.PeopleID = id).FirstOrDefault()
  End Function

  Sub update(ByVal pi As PeopleInfo)
     update2(PeoplesInfo, pi)
  End Sub

  Private Sub update2(ByRef cache As List(Of PeopleInfo), ByVal pi As PeopleInfo)

    Dim index As Integer = cache.FindIndex(Function(x) x.PeopleID = pi.PeopleID)
          If index >= 0 Then
            cache.Item(index) = pi
          End If

       End Sub


      Sub AddOne()
      AddOneTocache(PeoplesInfo)

      End Sub

     Private Sub AddOneTocache(ByRef cache As IList(Of PeopleInfo))
   cache.Add(New PeopleInfo With {.PeopleID = CacheHelper.z + 1, .Name = "people-N", .Gender = 2, .Address = "madhapar", .ContactNo = 5252552})
     CacheHelper.z += 1

   End Sub
   End Class


    


Copy this code in PeopleInfo.vb




Public Class PeopleInfo
    Public Property PeopleID As Integer
    Public Property Name As String
    Public Property Address As String
    Public Property ContactNo As String
    Public Property Gender As Int16
    Public Property Occupations As String

End Class




Copy this code in Dcf.vb


Imports Microsoft.Data.Caching
Public Class Dcf
    Public Shared instance As New DataCacheFactory
End Class



Copy this code in CacheTestController.vb

    Public Class CacheTestController
        Inherits System.Web.Mvc.Controller

        '
        ' GET: /CacheTest
        Private ph As CacheHelper
        Sub New()
            ph = New CacheHelper

        End Sub

        Function Index() As ActionResult
            Return RedirectToAction("GetPeoples")

        End Function
        Function addSomePeoples() As ActionResult
            ph.addSomePeoples()
            Return RedirectToAction("GetPeoples")
        End Function
        Function GetPeoples() As ViewResult



            Return View("PeoplesList", ph.GetPeoples)


        End Function
        Function Details(ByVal id As Integer) As ActionResult
            Return View(ph.GetInfoByID(id))

        End Function

        Function ClearCache() As ActionResult
            ph.ClearCache()

            Return RedirectToAction("GetPeoples")

        End Function

        Function Addone() As ActionResult
            ph.AddOne()
            Return RedirectToAction("GetPeoples")
        End Function
         _
        Function update(ByVal id As Integer) As ViewResult
            Return View(ph.GetInfoByID(id))
        End Function
         _
        Function update(ByVal pi As PeopleInfo) As RedirectToRouteResult
            ph.update(pi)
            Return (RedirectToAction("GetPeoples"))
        End Function

        Function Delete(ByVal id As Integer) As RedirectToRouteResult

            ph.Delete(id)
            Return RedirectToAction("GetPeoples")

        End Function
    End Class







Step-5: Open CacheTestController.vb -> Right Click on GetPeoples Action Method and Select Add View and set option showen in below image.

Add new view

This will create one subfolder in View folder with the same name as controller(CacheTest) and add your view to that folder.

Step-6: -> Right Click on Update Action Method and Select add view and set option as shown in below image.

add another view

Step-7: -> Right Click on Details Action Method and Select add view and set option as shown in below image.

add details view

Step-8: -> Open PeopleList.vbhtml View and add below code at start of the body tag.
     

@Html.ActionLink("Add some peoples Cache", "addSomePeoples") | @Html.ActionLink("Clear Cache","ClearCache") | @Html.ActionLink("Add one people to cache", "Addone") |