在Umbraco中,HttpContext.Current.Cache和ApplicationContext.ApplicationCache.RuntimeCache有什么不同?在效率方面,哪一种更好呢?
我在我的项目和ASP.NET MVC中使用了Umbraco 7.4.x。在我的项目中,我有一个可以包含这么多项的产品列表,为此我想使用缓存。具体来说,我希望将数据放入缓存中,当一个新项添加到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>*@
}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);
}
}
}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;
}
}
}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)
{
}
}发布于 2016-08-31 15:16:07
您可以在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
https://stackoverflow.com/questions/39250784
复制相似问题