Asp.net-Mvc

Umbraco 項目中適合使用哪種類型的記憶體,如何實現智能記憶體?

  • September 3, 2016

HttpContext.Current.CacheUmbraco和Umbraco有什麼不同ApplicationContext.ApplicationCache.RuntimeCache?就效率而言,哪個更好用?

Umbraco 7.4.x在我的項目中使用過ASP.NET MVC. 在我的項目中,我有一個可以包含這麼多項目的產品列表,因此我想使用記憶體。具體來說,我想將我的數據放在記憶體中,當一個新項目添加到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>*@
   }

產品控制器.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

引用自:https://stackoverflow.com/questions/39250784