【问题标题】:Which type of cache is suitable for using in Umbraco project and how can I implement an intelligent cache?Umbraco 项目中适合使用哪种类型的缓存,如何实现智能缓存?
【发布时间】:2017-01-08 02:43:26
【问题描述】:

Umbraco 中的 HttpContext.Current.CacheApplicationContext.ApplicationCache.RuntimeCache 有什么不同? 就效率而言,使用哪一个更好?

我在我的项目中使用了Umbraco 7.4.xASP.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)
{

}
}

【问题讨论】:

    标签: asp.net-mvc performance caching umbraco umbraco7


    【解决方案1】:

    您可以在 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

    【讨论】:

      猜你喜欢
      • 2010-11-11
      • 1970-01-01
      • 2011-10-12
      • 1970-01-01
      • 1970-01-01
      • 2012-05-22
      • 1970-01-01
      • 2019-12-10
      • 1970-01-01
      相关资源
      最近更新 更多