[英]How to cache specific parts of the page with ASP.NET MVC?
I'm making a "reputation" type feature on my site similar to stack overflow. 我正在网站上进行类似于堆栈溢出的“信誉”类型功能。 I want to display their points on by their name in the header. 我想通过标题中的名称显示它们的点。
I don't want to have to do a db call just for this every time they move to another page. 我不想每次他们移动到另一个页面时都必须为此进行数据库调用。
Can anyone suggest a nice way to implement this? 谁能建议一种实现此目标的好方法?
Don't make a db call just for this but get the data from the database while you're getting everything else. 不要仅仅为此进行数据库调用,而是在获取其他所有内容时从数据库获取数据。
Follow the principle of One Request Response cycle, one database call. 遵循一个请求响应周期,一个数据库调用的原则。 It's really just a matter of joining in the other tables in your query. 实际上,这只是联接查询中的其他表的问题。
For instance, if you look at a page that has a question and a bunch of answers. 例如,如果您查看的页面上有一个问题和一堆答案。 You know the "user" of the original question and you know the "user" of each of the answers and comments. 您知道原始问题的“用户”,并且知道每个答案和评论的“用户”。 So while you're getting the information also join in the pertinent information of each of the users. 因此,在获取信息的同时,还要加入每个用户的相关信息。 It's really just joining in additional tables and you can effectively get all of the data you require in one database call. 它实际上只是联接其他表,您可以在一个数据库调用中有效地获取所需的所有数据。
When you make a call to a database (like MSSQL server) you can get back multiple resultsets in one call, so you're also not limited to a single result set. 调用数据库(例如MSSQL Server)时,可以在一个调用中返回多个结果集,因此,您也不仅限于单个结果集。
Edit: 编辑:
Here is a sample Stored procedure (or just SQL statements) that results in 5 result sets in one database call. 这是一个示例存储过程(或只是SQL语句),它在一个数据库调用中产生5个结果集。
ALTER PROCEDURE [dbo].[GetPostWithSlug]
@PostSlug varchar(80)
AS
BEGIN
SET NOCOUNT ON;
DECLARE @PostId int;
SELECT @PostId = PostId
FROM dbo.Post
WHERE PostSlug = @PostSlug
SELECT PostId, PostDate, PostSlug, dbo.[User].UserFirstName + ' ' + dbo.[User].UserLastName AS PostedBy, PostTitle, PostText, PostIsPublished, PostIsPublic, PostTitleImg, PostUpdatedDate, dbo.[User].UserWebsite AS AuthorWebsite
FROM dbo.Post
INNER JOIN
dbo.[User]
ON dbo.[User].UserId = dbo.Post.UserId
WHERE PostId = @PostId
/* SubCategories for Post */
SELECT dbo.PostSubCategory.PostId, dbo.Category.CategoryId, dbo.SubCategory.SubCategoryId, dbo.Category.CategoryDescription, dbo.SubCategory.SubCategoryDescription
FROM dbo.Category
INNER JOIN
dbo.SubCategory ON dbo.Category.CategoryId = dbo.SubCategory.CategoryId
INNER JOIN
dbo.PostSubCategory ON dbo.SubCategory.SubCategoryId = dbo.PostSubCategory.SubCategoryId
INNER JOIN
dbo.Post ON dbo.Post.PostId = dbo.PostSubCategory.PostId
WHERE dbo.Post.PostId = @PostId
ORDER BY dbo.PostSubCategory.PostId
/* Tags for Post */
SELECT dbo.PostTag.PostId, dbo.Tag.TagId, dbo.Tag.TagDescription
FROM dbo.PostTag
INNER JOIN
dbo.Tag ON dbo.Tag.TagId = dbo.PostTag.TagId
INNER JOIN
dbo.Post ON dbo.Post.PostId = dbo.PostTag.PostId
WHERE dbo.Post.PostId = @PostId
ORDER BY dbo.PostTag.PostId, dbo.Tag.TagDescription
/* Custom Groups for Post */
SELECT dbo.PostCustomGroup.PostId, dbo.CustomGroupHeader.CustomGroupHeaderId, dbo.CustomGroup.CustomGroupId
FROM dbo.CustomGroup
INNER JOIN
dbo.CustomGroupHeader ON dbo.CustomGroupHeader.CustomGroupHeaderId = dbo.CustomGroup.CustomGroupHeaderId
INNER JOIN
dbo.PostCustomGroup ON dbo.PostCustomGroup.CustomGroupId = dbo.CustomGroup.CustomGroupId
WHERE dbo.PostCustomGroup.PostId = @PostId
EXEC GetCommentsForPost @PostId
END
In C# code, your DbDataReader will hold multiple result sets and these can be got by calling NextResult(). 在C#代码中,您的DbDataReader将保存多个结果集,可以通过调用NextResult()来获得这些结果集。 Or if you're filling a DataSet you can simply use the Fill method of a adapter. 或者,如果要填充数据集,则可以简单地使用适配器的Fill方法。
I use this technique on my blog Matlus - Internet Technology and Software Engineering and you can see how fast (responsive) the site is. 我在我的博客Matlus-Internet Technology and Software Engineering上使用了这种技术,您可以看到该站点有多快(响应)。 The same technique is used on ExposureRoom which is a public social networking website that has a huge amount of traffic. 在具有大量流量的公共社交网站ExposureRoom上使用了相同的技术。
You're looking for some way to do donut hole caching: 您正在寻找做甜甜圈孔缓存的方法:
https://stackoverflow.com/search?q=[asp.net-mvc]+donut+hole+caching https://stackoverflow.com/search?q=[asp.net-mvc]+donut+hole+caching
If it's only a small part of part of page that needs to be cached, you could use child action to render that part, and cache it. 如果仅需要缓存页面的一小部分,则可以使用子操作来呈现并缓存该部分。 Example: http://www.asp.net/learn/whitepapers/mvc3-release-notes#_Toc276711791 示例: http : //www.asp.net/learn/whitepapers/mvc3-release-notes#_Toc276711791
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.