0

I am using vb.net and asp.net. My web application is used by multiple parallel users . I have a table named "Tracked_Table"

I want to cache this table for 10 seconds and after 10 seconds, i wanted to reload the cache again with the latest "Tracked_Table" data.

I have tried on the Below code.. (ASP.NET AND VB.NET)

-- In page Load:

If Cache("tracked") Is Nothing Then
    FillCache()
ElseIf (DateTime.Now() - Convert.ToDateTime(Cache("timestamp"))).TotalSeconds > 10 Then
    FillCache()
End If

DisplayTrackedTable() ' --Here i am using the Cache("tracked") table..

-- End of pageload

In the Fillcache function , the below code i hv written.

** vehicleTrackedDict --> The dictionary containing the "tracked_table" data

Cache.Remove("tracked")
Cache.Remove("timestamp")
Cache("timestamp") = Now.ToString("yyyy/MM/dd HH:mm:ss")
Cache("tracked") = vehicleTrackedDict

This is the whole thing i am trying to use cache..

But this code seems to be not working perfectly for Parallel multiple users.

Can any one helps me out for this..

Pravin Kumar
  • 693
  • 1
  • 9
  • 35
  • Have you tried to use [Cache.insert](http://msdn.microsoft.com/en-us/library/vstudio/4y13wyk9(v=vs.100).aspx) Method with slidingExpiration=10s? – Fabio Oct 07 '14 at 06:50
  • @Fabio : Will this be applied for Parallel users.. I mean to ask.. How many Cache will create for 4 users accessing at the same time ? I need only 1 cache to be created for all the 4 users and after 10 seconds, the cache will get latest Data from Database.. – Pravin Kumar Oct 07 '14 at 07:05
  • [Cache](http://msdn.microsoft.com/en-us/library/vstudio/System.Web.Caching.Cache(v=vs.100).aspx) This type is thread safe. Your model is one producer and 4 consumers, right? – Fabio Oct 07 '14 at 07:11
  • Cache is application wide scope, i guess only one instance of cache will be there irrespective of the no of users/sessions – Thangadurai Oct 07 '14 at 07:35
  • just a thought, it is better to use locking when you try to create the cache object (i.e. in your Fillcache method) – Thangadurai Oct 07 '14 at 07:39
  • @Fabio: Yes..Its 1-->4 relationship. – Pravin Kumar Oct 07 '14 at 07:52
  • @Thangadurai: Yes But when i am checking the timestamp of Cache with the NOW Timestamp, the cache fills multiple times already..:( :( '( – Pravin Kumar Oct 07 '14 at 07:53
  • Just synchronize cache building to avoid multiple users triggering building the same cache? – Allan S. Hansen Oct 07 '14 at 08:26
  • @Pravin, any specifc reason for not using the overloaded Add method of the Cache object which takes an absolute expiration as an argument. This way you dont have to check for the expiry manually. And include the function within a lock statement, so you just have to check the existence of the cached object,if present use it, else create it – Thangadurai Oct 07 '14 at 08:52

0 Answers0