Which type of cache is suitable for using in Umbraco project and how can I implement an intelligent cache?
Asked Answered
M

1

8

What is different between HttpContext.Current.Cache and ApplicationContext.ApplicationCache.RuntimeCachein Umbraco? And which one is better to use in terms of efficiency?

I've used Umbraco 7.4.x in my project and ASP.NET MVC. In my project I have a list of products that can be contain so many items and I want to use the cache for this reason. Specifically I want to put my data in cache and when a new item added to Products node, the cache automatically be update. How can I implement it?

Modified: Please give me an implementation with details for my code.

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

}
}
Mccarron answered 31/8, 2016 at 13:19 Comment(0)
H
7

There are several caches you can use in Umbraco. You have the following available:

ApplicationContext.ApplicationCache.RuntimeCache - this is a cache that is available to ALL requests.

ApplicationContext.ApplicationCache.RequestCache - this is a cache that lasts for the current request ONLY. Use if you want to cache an object for use in several views say.

ApplicationContext.ApplicationCache.StaticCache - this is a static cache and should be used very rarely and with caution, as incorrect usage can cause memory issues.

If the items that you are caching need to be updated when a node is changed in the back office, you will need to write an ICacheRefresher to handle it.

Here's some info on the three caches, and how to get items into the cache: https://our.umbraco.org/documentation/reference/cache/updating-cache

There's an example of an ICacheRefresher here: https://github.com/Jeavon/SEOCheckerCacheRefresher

Harvester answered 31/8, 2016 at 15:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.