0

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
1
  • you want us to convert this to c# for you? :/ Commented Mar 10, 2009 at 9:25

2 Answers 2

2

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");
}
Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.