Tom
Tom

Reputation: 6707

Using Cache on C# ( VB code included)

How can I use web server cache in a similar way as the following VB code does:

   ////  FindData() returns real data
   //// FindCached() returns from cache (kept 20 minute)

    Protected Function RegisterCachedData(ByVal id As String) As Integer
        Dim onCacheRemove As CacheItemRemovedCallback
        onCacheRemove = New CacheItemRemovedCallback(AddressOf Me.CheckCallback)
        Cache.Insert("AverageData", FindData(1), Nothing, DateTime.Now.AddMinutes(20), TimeSpan.Zero, 1, onCacheRemove)
    End Function

    Sub CheckCallback(ByVal str As String, ByVal obj As Object, ByVal reason As CacheItemRemovedReason)
        RegisterCachedData(0)
    End Sub

   Protected Function FindCached() As Integer
        If Cache.Get("AverageData") Is Nothing Then RegisterCachedData(0)
        Return Cache.Get("AverageData")
    End Function

Upvotes: 0

Views: 498

Answers (2)

Guffa
Guffa

Reputation: 700910

The same way. This should do it:

protected int RegisterCachedData(string id) {
   CacheItemRemovedCallback onCacheRemove;
   onCacheRemove = new CacheItemRemovedCallback(CheckCallback);
   Cache.Insert("AverageData", FindData(1), null, DateTime.Now.AddMinutes(20), TimeSpan.Zero, 1, onCacheRemove);
}

void CheckCallback(string str, object obj, CacheItemRemovedReason reason) {
   RegisterCachedData("0");
}

protected int FindCached() {
   if (Cache.Get("AverageData") == null) RegisterCachedData("0");
   return Cache.Get("AverageData");
}

Upvotes: 2

ybo
ybo

Reputation: 17162

Asp.Net is Asp.Net, no matter what language you use. What you can do with VB.Net can be done with C# too.

Upvotes: 2

Related Questions