在asp.net MVC中缓存部分视图

时间:2021-05-19 03:17:13

I have the following situation on a webapp:

我在webapp上有以下情况:

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

表“Employees”包含“Department”和“Function”列。两者都是下拉列表。

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.

更改部门时,我使用参数“DepartmentId”对控制器操作执行ajax调用。控制分数上有一个[outputcache]属性,因此它返回的函数将被缓存为每个部门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) ?

任何人? 30次观看,没有答案..关于我的问题的任何评论?太明显了?太难?太奇怪了?谷歌的东西(虽然我没有在那里找到解决方案)?

3 个解决方案

#1


4  

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

菲尔·哈克(Phil Haack)撰写了一篇关于类似话题的短篇博客文章,名为Donut Hole Caching。它是一个很好的起点。

#2


4  

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.

根据我的经验,这也很适合ajax模式,因为你只能从一个动作方法“获取”你的数据。

Partial requests are discussed here and subcontrollers here

这里讨论部分请求和此处的子控制器

Hope this helps.

希望这可以帮助。

#3


0  

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.

你的意思是你想要在生成视图时调用直接生成函数的控制器操作而不是使用ajax吗?如果我理解正确,我认为不可能获得输出缓存中的数据(虽然我可能错了)。无论如何,如果你能得到它,你仍然需要再次解码数据。

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)
  • 在HttpContext.Cache中自己缓存部门ID并在两个操作中使用它们(可能这么快你不再需要OutputCache而且没有双缓存)

  • 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.
  • 或者,如果部门/功能的数量不是很大,您可以创建一个包含所有功能的json结构,将它们存储在视图中,并且根本不使用ajax。

#1


4  

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

菲尔·哈克(Phil Haack)撰写了一篇关于类似话题的短篇博客文章,名为Donut Hole Caching。它是一个很好的起点。

#2


4  

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.

根据我的经验,这也很适合ajax模式,因为你只能从一个动作方法“获取”你的数据。

Partial requests are discussed here and subcontrollers here

这里讨论部分请求和此处的子控制器

Hope this helps.

希望这可以帮助。

#3


0  

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.

你的意思是你想要在生成视图时调用直接生成函数的控制器操作而不是使用ajax吗?如果我理解正确,我认为不可能获得输出缓存中的数据(虽然我可能错了)。无论如何,如果你能得到它,你仍然需要再次解码数据。

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)
  • 在HttpContext.Cache中自己缓存部门ID并在两个操作中使用它们(可能这么快你不再需要OutputCache而且没有双缓存)

  • 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.
  • 或者,如果部门/功能的数量不是很大,您可以创建一个包含所有功能的json结构,将它们存储在视图中,并且根本不使用ajax。