2016-08-31 34 views
8

Điều gì khác nhau giữa HttpContext.Current.CacheApplicationContext.ApplicationCache.RuntimeCache trong Umbraco? Và cái nào tốt hơn để sử dụng về hiệu quả?Loại bộ nhớ cache nào phù hợp để sử dụng trong dự án Umbraco và làm cách nào tôi có thể triển khai bộ nhớ cache thông minh?

Tôi đã sử dụng Umbraco 7.4.x trong dự án của mình và ASP.NET MVC. Trong dự án của tôi, tôi có một danh sách các sản phẩm có thể chứa rất nhiều mục và tôi muốn sử dụng bộ nhớ cache vì lý do này. Cụ thể tôi muốn đặt dữ liệu của mình vào bộ nhớ cache và khi một mục mới được thêm vào nút Products, bộ nhớ cache sẽ tự động cập nhật. Làm cách nào để triển khai?

Đã sửa đổi: Vui lòng cho tôi triển khai chi tiết mã của tôi.

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) 
{ 

} 
} 

Trả lời

7

Có một số lưu trữ, bạn có thể sử dụng trong Umbraco. Bạn có sẵn các sản phẩm sau:

ApplicationContext.ApplicationCache.RuntimeCache - đây là bộ nhớ cache có sẵn cho TẤT CẢ yêu cầu.

ApplicationContext.ApplicationCache.RequestCache - đây là bộ nhớ cache kéo dài cho yêu cầu hiện tại CHỈ. Sử dụng nếu bạn muốn cache một đối tượng để sử dụng trong một vài khung nhìn nói.

ApplicationContext.ApplicationCache.StaticCache - đây là bộ nhớ cache tĩnh và nên được sử dụng rất hiếm khi và thận trọng vì việc sử dụng không chính xác có thể gây ra vấn đề về bộ nhớ.

Nếu các mục mà bạn đang lưu vào bộ nhớ đệm cần được cập nhật khi nút được thay đổi trong văn phòng phía sau, bạn sẽ cần phải viết một ICacheRefresher để xử lý nó.

Dưới đây là một số thông tin về ba cache, và làm thế nào để có được các mục vào bộ nhớ cache: https://our.umbraco.org/documentation/reference/cache/updating-cache

Có một ví dụ về một ICacheRefresher đây: https://github.com/Jeavon/SEOCheckerCacheRefresher

Các vấn đề liên quan