wali
wali

Reputation: 477

Can a linq table be added to the asp .net cache?

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...

Upvotes: 1

Views: 367

Answers (4)

wali
wali

Reputation: 477

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!!!

Upvotes: 0

RickNZ
RickNZ

Reputation: 18654

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.

Upvotes: 1

Eilon
Eilon

Reputation: 25704

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.

Upvotes: 3

G-Wiz
G-Wiz

Reputation: 7426

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.

Upvotes: 2

Related Questions