一、基础介绍

1.什么是cache
     Web缓存是指一个Web资源(如html页面,图片,js,数据等)存在于Web服务器和客户端(浏览器)之间的副本。
2.为什么要用cache
     即cache的作用,有以下几点:
     2.1.减少网络带宽消耗;
     2.2.降低服务器压力;
     2.3.减少网络延迟、加快页面打开速度。
3.cache的分类
     常见分类如下:
     3.1.数据库数据缓存;
     3.2.服务器端缓存;
          a.代理服务器缓存
          b.CDN缓存
     3.3.浏览器缓存;
     3.4.Web应用缓存

二、NopCommerce的缓存分析

1.UML
     接口ICacheManager;
     实现MemoryCacheManager(HTTP请求缓存,生命周期长),NopNullCache(仅实现接口,不提供缓存机制),PerRequestCacheManager(HTTP请求缓存,生命周期短),RedisCacheManager(Redis缓存);
     扩展CacheExtensions;
2.代码分析
     2.1.Nop.Web.Framework.DependencyRegistrar类,配置依赖注入的ICacheManager。

             //cache managers
if (config.RedisCachingEnabled)
{
builder.RegisterType<RedisCacheManager>().As<ICacheManager>().Named<ICacheManager>("nop_cache_static").InstancePerLifetimeScope();
}
else
{
builder.RegisterType<MemoryCacheManager>().As<ICacheManager>().Named<ICacheManager>("nop_cache_static").SingleInstance();
}

2.2.RedisCachingEnabled通过读取配置文件,见Nop.Core.Configuration.NopConfig

             var redisCachingNode = section.SelectSingleNode("RedisCaching");
if (redisCachingNode != null && redisCachingNode.Attributes != null)
{
var enabledAttribute = redisCachingNode.Attributes["Enabled"];
if (enabledAttribute != null)
config.RedisCachingEnabled = Convert.ToBoolean(enabledAttribute.Value); var connectionStringAttribute = redisCachingNode.Attributes["ConnectionString"];
if (connectionStringAttribute != null)
config.RedisCachingConnectionString = connectionStringAttribute.Value;
}

3.应用

     通过依赖注入,生成缓存管理类实例,通过方法读取、设置缓存。

        [NonAction]
protected virtual List<int> GetChildCategoryIds(int parentCategoryId)
{
string cacheKey = string.Format(ModelCacheEventConsumer.CATEGORY_CHILD_IDENTIFIERS_MODEL_KEY,
parentCategoryId,
string.Join(",", _workContext.CurrentCustomer.GetCustomerRoleIds()),
_storeContext.CurrentStore.Id);
return _cacheManager.Get(cacheKey, () =>
{
var categoriesIds = new List<int>();
var categories = _categoryService.GetAllCategoriesByParentCategoryId(parentCategoryId);
foreach (var category in categories)
{
categoriesIds.Add(category.Id);
categoriesIds.AddRange(GetChildCategoryIds(category.Id));
}
return categoriesIds;
});
}

我的NopCommerce之旅(5): 缓存的更多相关文章

  1. 我的NopCommerce之旅(8): 路由分析

    一.导图和基础介绍 本文主要介绍NopCommerce的路由机制,网上有一篇不错的文章,有兴趣的可以看看NopCommerce源码架构详解--对seo友好Url的路由机制实现源码分析 SEO,Sear ...

  2. [转]NopCommerce之旅: 应用启动

    本文转自:http://www.cnblogs.com/devilsky/p/5359881.html 我的NopCommerce之旅(6): 应用启动   一.基础介绍 Global.asax 文件 ...

  3. 我的NopCommerce之旅(7): 依赖注入(IOC/DI)

    一.基础介绍 依赖注入,Dependency Injection,权威解释及说明请自己查阅资料. 这里简单说一下常见使用:在mvc的controller的构造方法中定义参数,如ICountryServ ...

  4. 我的NopCommerce之旅(9): 编写Plugin实例

    一.基础介绍 ——In computing, a plug-in (or plugin) is a set of software components that add specific abili ...

  5. 我的NopCommerce之旅(3): 系统代码结构分析

    一.概述 基于MVC 二.详细描述 \Libraries\Nop.Core 核心类,包括缓存.事件.帮助类.业务对象(订单.客户实体) \Libraries\Nop.Data 数据访问层,采用Enti ...

  6. 我的NopCommerce之旅(1): 系统综述

    一.概述 NopCommerce是一个开源的购物网站,它的特点是Pluggable modular/layered architecture(可插拔模块分层架构) 二.功能特色介绍 1.适配手机端 2 ...

  7. 我的NopCommerce之旅(6): 应用启动

    一.基础介绍 Global.asax 文件(也称为 ASP.NET 应用程序文件)是一个可选文件,该文件包含响应 ASP.NET 或 HTTP 模块所引发的应用程序级别和会话级别事件的代码. Appl ...

  8. 我的NopCommerce之旅(4): 定时任务之邮件

    一.功能简介 用户购买物品生成订单后,系统将发送邮件提醒给用户 二.操作步骤 后台配置一个系统的默认发送邮箱 启动定时任务,这里包括多个任务,只需要启动邮件任务 查看邮件发送情况 三.数据库分析 [d ...

  9. 我的NopCommerce之旅(2): 系统环境及技术分析

    一.系统环境 IIS7.0 or above ASP.NET 4.5(MVC 5.0) .NET Framework 4.5.1 or above VS 2012 or above 二.架构设计 Pl ...

随机推荐

  1. the art of seo(chapter three)

    SEO Planning: Customizing Your Strategy ***Developing an SEO Plan Prior to Site Development***Determ ...

  2. LoadRunner打开WebTours只显示头部解决办法

    LoadRunner打开WebTours只显示头部解决办法   1.遇到这种情况,先查看一下路径HP\LoadRunner\WebTours下的cgierr日志中是否有错误,比如Can't open ...

  3. Python: PS 图像调整--饱和度调整

    本文用 Python 实现 PS 图像调整中的饱和度调整算法,具体的算法原理和效果可以参考之前的博客: http://blog.csdn.net/matrix_space/article/detail ...

  4. Learning ReactNative (一) : JavaScript模块基本原理与用法

    在使用ReactNative进行开发的时候,我们的工程是模块化进行组织的.在npmjs.com几十万个库中,大部分都是遵循着CommonJS规则的.在ES6中引入了class的概念,从此JavaScr ...

  5. ubuntu下网络性能测试

    iperf的主要功能 TCP 测量网络带宽 报告MSS/MTU值的大小和观测值 支持TCP窗口值通过套接字缓冲 当P线程或Win32线程可用时,支持多线程.客户端与服务端支持同时多重连接 UDP 客户 ...

  6. ZOJ3201(树形DP)

    Tree of Tree Time Limit: 1 Second      Memory Limit: 32768 KB You're given a tree with weights of ea ...

  7. sqlserver 截取字符串

    **/*******/*****/1399/* 我要取第3个'/'与第4个'/'中的内容,就是1399 create table ta( col varchar(100)) insert ta sel ...

  8. OpenService 打开一个已经存在的服务

    SC_HANDLE WINAPI OpenService( _In_ SC_HANDLE hSCManager, _In_ LPCTSTR lpServiceName, _In_ DWORD dwDe ...

  9. ASP.NET Web应用程序修改页面Inherits示例

    <@page 中 Codebehind .Inherits 和aspx的关系 CodeBehind 指定包含与页关联的类的已编译文件的名称.该属性不能在运行时使用. 说明: 提供此属性是为了与以 ...

  10. java集合框架之ArrayList与LinkedList的区别

    参考http://how2j.cn/k/collection/collection-arraylist-vs-linkedlist/690.html#nowhere ArrayList和LinkedL ...