views:

580

answers:

3

Hello,

I have the following situation on a webapp:

A table "Employees" contains column "Department" and "Function". Both are dropdownlists.

The "Function" dropdownlist options depend on the selected "department". (so each department has its own list of functions)

When changing the department, I do an ajax call to a controller action with parameter "DepartmentId". Theres an [outputcache] attribute on the controlleraction so the functions that it returns get cached for every department ID.

My problem is the initial loading of the page. Can you call a controlleraction in a view and take advantage of the caching?

Anyone? 30 views and no answers.. Any remarks about my question? Too obvious? too hard? too weird? something for google (altho I didn't find a solution there) ?

A: 

Do you mean that you want to call the controller action that generates the functions directly while generating the view and not with ajax? If I understood that correctly, I don't think it's possible to get at the data in the output cache (I may be wrong though). Anyway, if you could get it, you'd still have to decode the data again.

You could also try to

  • Cache the department ids yourself in the HttpContext.Cache and use them in both actions (maybe that's so fast you don't need the OutputCache anymore and don't have double caching)
  • Or if the number of departments/functions is not so large, you could create a json structure with all functions, store them in the view and don't use ajax at all.
chris166
+1  A: 

Phil Haack wrote a short blog post on a similar topic called Donut Hole Caching. It serve as a good starting point.

Rick
not sure anymore if this fixed it but I'm marking as answer anyway..
Thomas Stock
A: 

I would use subcontrollers or better still partial requests to do what you are asking. In a typical page I tend to not cache the whole page but instead break up areas into different action methods which are called via partial requests. That way I can have output caching on each area with differing expirations. It's more page life-cycles but when they are cached they really are not a tax on performance. It's also far easier to maintain and optimize a specific area if it starts to under perform.

In my experience this also fits very nicely with ajax patterns as you only every "get" your data from one action method.

Partial requests are discussed here and subcontrollers here

Hope this helps.

madcapnmckay