Umbraco项目适合使用哪种缓存,如何实现智能缓存?
Which type of cache is suitable for using in Umbraco project and how can I implement an intelligent cache?
在 Umbraco 中 HttpContext.Current.Cache
和 ApplicationContext.ApplicationCache.RuntimeCache
有什么不同?
从效率上来说,哪个更好用?
我在我的项目中使用了 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>*@
}
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)
{
}
}
您可以在 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
在 Umbraco 中 HttpContext.Current.Cache
和 ApplicationContext.ApplicationCache.RuntimeCache
有什么不同?
从效率上来说,哪个更好用?
我在我的项目中使用了 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>*@
}
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)
{
}
}
您可以在 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