首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >哪种类型的缓存适合在Umbraco项目中使用,以及如何实现智能缓存?

哪种类型的缓存适合在Umbraco项目中使用,以及如何实现智能缓存?
EN

Stack Overflow用户
提问于 2016-08-31 13:19:35
回答 1查看 574关注 0票数 8

在Umbraco中,HttpContext.Current.CacheApplicationContext.ApplicationCache.RuntimeCache有什么不同?在效率方面,哪一种更好呢?

我在我的项目和ASP.NET MVC中使用了Umbraco 7.4.x。在我的项目中,我有一个可以包含这么多项的产品列表,为此我想使用缓存。具体来说,我希望将数据放入缓存中,当一个新项添加到Products节点时,缓存将自动更新。我如何实现它呢?

修改后的:请给我一个包含代码详细信息的实现.

Products.cshtml:

代码语言:javascript
复制
@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:

代码语言:javascript
复制
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:

代码语言:javascript
复制
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:

代码语言:javascript
复制
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)
{

}
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-08-31 15:16:07

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

票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39250784

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档