Jah*_*han 8 asp.net-mvc performance caching umbraco umbraco7
Umbraco HttpContext.Current.Cache和ApplicationContext.ApplicationCache.RuntimeCacheUmbraco有什么不同?哪一个在效率方面更好用?
我用过Umbraco 7.4.x我的项目和ASP.NET MVC.在我的项目中,我有一个可以包含这么多项目的产品列表,因此我想使用缓存.具体来说,我想将我的数据放在缓存中,当一个新项添加到Products节点时,缓存会自动更新.我该如何实现它?
修改:请给我一个实现,详细说明我的代码.
Products.cshtml:
@using Jahan.Handicraft.Model.UModel.URenderModel
@using Jahan.Handicraft.Model.UModel
@inherits Umbraco.Web.Mvc.UmbracoViewPage<BaseRenderModel<Jahan.Handicraft.Model.UModel.Products>>
@foreach (var prod in Model.Model.ProductList.Skip((page - 1) * pageSize).Take(pageSize))
{
@*<div>LOAD DATA</div>*@
}
Run Code Online (Sandbox Code Playgroud)
ProductsController.cs:
namespace Jahan.Handicraft.Web.Mvc.UmbracoCms.App.Controllers
{
public class ProductsController : BaseRenderMvcController<Products>
{
// GET: Products
public override ActionResult Index(RenderModel model)
{
return base.Index(Instance);
}
}
}
Run Code Online (Sandbox Code Playgroud)
BaseRenderMvcController.cs:
public class BaseRenderMvcController<TBaseEntity> : RenderMvcController where TBaseEntity : BaseEntity
{
private BaseRenderModel<TBaseEntity> _instance;
public BaseRenderModel<TBaseEntity> Instance
{
get
{
if (_instance == null)
{
_instance = new BaseRenderModel<TBaseEntity>(CurrentContent, CurrentCultureInfo);
}
return _instance;
}
set
{
}
}
public virtual IPublishedContent CurrentContent
{
get
{
if (UmbracoContext.Current != null)
return UmbracoContext.Current.PublishedContentRequest.PublishedContent;
return null;
}
}
public virtual CultureInfo CurrentCultureInfo
{
get
{
if (UmbracoContext.Current != null)
return UmbracoContext.Current.PublishedContentRequest.PublishedContent.GetCulture();
return null;
}
}
}
Run Code Online (Sandbox Code Playgroud)
BaseRenderModel.cs:
public class BaseRenderModel<TBaseEntity> : RenderModel where TBaseEntity : BaseEntity
{
public TBaseEntity Model { get; set; }
public BaseRenderModel(IPublishedContent content, CultureInfo culture) : base(content, culture)
{
object[] args = new object[] { content, culture };
Model = (TBaseEntity)Activator.CreateInstance(typeof(TBaseEntity), args);
}
public BaseRenderModel(IPublishedContent content) : base(content)
{
object args = new object[] { content };
Model = (TBaseEntity)Activator.CreateInstance(typeof(TBaseEntity), args);
}
public BaseRenderModel()
: base(UmbracoContext.Current.PublishedContentRequest.PublishedContent)
{
}
}
Run Code Online (Sandbox Code Playgroud)
您可以在Umbraco中使用多个缓存.您有以下服务:
ApplicationContext.ApplicationCache.RuntimeCache - 这是一个可用于所有请求的缓存.
ApplicationContext.ApplicationCache.RequestCache - 这是一个仅为当前请求持续的缓存.如果要缓存对象以便在多个视图中使用,请使用.
ApplicationContext.ApplicationCache.StaticCache - 这是一个静态缓存,应该很少使用并谨慎使用,因为不正确的使用会导致内存问题.
如果在后台更改节点时需要更新要缓存的项目,则需要编写ICacheRefresher来处理它.
以下是三个缓存的一些信息,以及如何将项目放入缓存:https://our.umbraco.org/documentation/reference/cache/updating-cache
这里有一个ICacheRefresher的例子:https://github.com/Jeavon/SEOCheckerCacheRefresher
| 归档时间: |
|
| 查看次数: |
455 次 |
| 最近记录: |