views:

52

answers:

4

And if so is there an example in vb .net? I want to add a linq result to the cache and then allow a mvc post to search the cache for better performance...every way I implement it I receive an object not referenced error...

I'm confused, maybe I shouldn't be doing it this way but the mvc post will be checking up to 2000 records and it would seem best not to have it querying the database everytime.

Thanks for any help...

+2  A: 

Yeah. Use Cache.Insert. I don't know VB.NET, but here's some basic advice (see "Caching API, Using the Cache Object"). Just pass your Linq result into Insert. I would just be careful about not shaping your Linq result with anonymous types, which would make it impossible to properly cast the object when retrieving it from the cache.

gWiz
Thanks for the fast reply....I've been using Cache.Insert... linq resultDim r = From d In db.batches _ Where d.bDate > "09/30/2009 12:00:00 PM" _ Select dDim c As Cache c = New Cache c.Insert("batches", r)'this bit of code comes from MSDN Dim cs As New Object cs = c("batches") If cs Is Nothing Then c.Insert("batches", r) End IfThank you...But everytime I run it I get a object not referenced error
wali
You don't want to create a new `Cache` object. Each cache instance you create is separate of the others. Use the singleton cache instance. In ASP.NET (WebForms) it is exposed on the `Page` class as `Page.Cache`. In ASP.NET MVC it is accessible in a `Controller` via `Controller.HttpContext.Cache`.
gWiz
+3  A: 

Storing the LINQ query is probably not very useful and probably won't work.

However, what you can do relatively safely is to store the results of a LINQ query in the ASP.NET cache.

By "results" I mean that you take the query object and call ToList() on it, which will cause the query to be executed.

As far as any errors that you get, please show us some code samples and the exact error and stack trace and we can try to find the right solution for you.

Eilon
+1  A: 

First, don't insert the query itself into Cache (the "from" statement). Insert the results from the query instead.

Next, keep in mind that just because you insert an object into Cache, it doesn't guarantee that it will be there the next time you try to access it. In fact, with the default priority, the Cache object has a fairly aggressive policy of letting go of objects.

You might try increasing the priority to High, and see if that helps.

RickNZ
A: 

I'm not sure why; probably because I'm using .Net 4.0, but I could never get the Cache to work...kept getting object not referenced error.

In any case I found an article from the MVC tutorials titled "Improving Performance with Output Caching". In the tut there are several examples in which I used OutputCache(Duration:=Integer.MaxValue, VaryByParam:="none")...

Not sure if it helps or not but in Firebug I see responds coming back at 50ms vs over 100ms. The only issue now is to split the files up, because after about 500 records the browser craps out...

Thanks for everyone's help as usual!!!

wali