Nop中定义了ICacheManger接口,它有几个实现,其中MemoryCacheManager是内存缓存的一个实现。

MemoryCacheManager:

using System;
using System.Collections.Generic;
using System.Runtime.Caching;
using System.Text.RegularExpressions; namespace Nop.Core.Caching
{
/// <summary>
/// Represents a manager for caching between HTTP requests (long term caching)
/// </summary>
public partial class MemoryCacheManager : ICacheManager
{
/// <summary>
/// Cache object
/// </summary>
protected ObjectCache Cache
{
get
{
return MemoryCache.Default;
}
} /// <summary>
/// Gets or sets the value associated with the specified key.
/// </summary>
/// <typeparam name="T">Type</typeparam>
/// <param name="key">The key of the value to get.</param>
/// <returns>The value associated with the specified key.</returns>
public virtual T Get<T>(string key)
{
return (T)Cache[key];
} /// <summary>
/// Adds the specified key and object to the cache.
/// </summary>
/// <param name="key">key</param>
/// <param name="data">Data</param>
/// <param name="cacheTime">Cache time</param>
public virtual void Set(string key, object data, int cacheTime)
{
if (data == null)
return; var policy = new CacheItemPolicy();
policy.AbsoluteExpiration = DateTime.Now + TimeSpan.FromMinutes(cacheTime);
Cache.Add(new CacheItem(key, data), policy);
} /// <summary>
/// Gets a value indicating whether the value associated with the specified key is cached
/// </summary>
/// <param name="key">key</param>
/// <returns>Result</returns>
public virtual bool IsSet(string key)
{
return (Cache.Contains(key));
} /// <summary>
/// Removes the value with the specified key from the cache
/// </summary>
/// <param name="key">/key</param>
public virtual void Remove(string key)
{
Cache.Remove(key);
} /// <summary>
/// Removes items by pattern
/// </summary>
/// <param name="pattern">pattern</param>
public virtual void RemoveByPattern(string pattern)
{
var regex = new Regex(pattern, RegexOptions.Singleline | RegexOptions.Compiled | RegexOptions.IgnoreCase);
var keysToRemove = new List<String>(); foreach (var item in Cache)
if (regex.IsMatch(item.Key))
keysToRemove.Add(item.Key); foreach (string key in keysToRemove)
{
Remove(key);
}
} /// <summary>
/// Clear all cache data
/// </summary>
public virtual void Clear()
{
foreach (var item in Cache)
Remove(item.Key);
} /// <summary>
/// Dispose
/// </summary>
public virtual void Dispose()
{
}
}
}

缓存的添加,在需要的地方构建cache key然后调用ICacheManger接口存储起来:

 var cachedModel = _cacheManager.Get(cacheKey, () =>
{
var model = new List<BlogPostYearModel>(); var blogPosts = _blogService.GetAllBlogPosts(_storeContext.CurrentStore.Id,
_workContext.WorkingLanguage.Id);
if (blogPosts.Count > )
{
var months = new SortedDictionary<DateTime, int>(); var first = blogPosts[blogPosts.Count - ].CreatedOnUtc;
while (DateTime.SpecifyKind(first, DateTimeKind.Utc) <= DateTime.UtcNow.AddMonths())
{
var list = blogPosts.GetPostsByDate(new DateTime(first.Year, first.Month, ), new DateTime(first.Year, first.Month, ).AddMonths().AddSeconds(-));
if (list.Count > )
{
var date = new DateTime(first.Year, first.Month, );
months.Add(date, list.Count);
} first = first.AddMonths();
} int current = ;
foreach (var kvp in months)
{
var date = kvp.Key;
var blogPostCount = kvp.Value;
if (current == )
current = date.Year; if (date.Year > current || model.Count == )
{
var yearModel = new BlogPostYearModel
{
Year = date.Year
};
model.Add(yearModel);
} model.Last().Months.Add(new BlogPostMonthModel
{
Month = date.Month,
BlogPostCount = blogPostCount
}); current = date.Year;
}
}
return model;
});

这个ICacheManger的Get方法其实是个扩展方法,当获取不到缓存的时候调用Func<T>获取值,然后缓存起来:

using System;

namespace Nop.Core.Caching
{
/// <summary>
/// Extensions
/// </summary>
public static class CacheExtensions
{
/// <summary>
/// Get a cached item. If it's not in the cache yet, then load and cache it
/// </summary>
/// <typeparam name="T">Type</typeparam>
/// <param name="cacheManager">Cache manager</param>
/// <param name="key">Cache key</param>
/// <param name="acquire">Function to load item if it's not in the cache yet</param>
/// <returns>Cached item</returns>
public static T Get<T>(this ICacheManager cacheManager, string key, Func<T> acquire)
{
return Get(cacheManager, key, , acquire);
} /// <summary>
/// Get a cached item. If it's not in the cache yet, then load and cache it
/// </summary>
/// <typeparam name="T">Type</typeparam>
/// <param name="cacheManager">Cache manager</param>
/// <param name="key">Cache key</param>
/// <param name="cacheTime">Cache time in minutes (0 - do not cache)</param>
/// <param name="acquire">Function to load item if it's not in the cache yet</param>
/// <returns>Cached item</returns>
public static T Get<T>(this ICacheManager cacheManager, string key, int cacheTime, Func<T> acquire)
{
if (cacheManager.IsSet(key))
{
return cacheManager.Get<T>(key);
} var result = acquire();
if (cacheTime > )
cacheManager.Set(key, result, cacheTime);
return result;
}
}
}

Cache的移除。Nop缓存的移除比较有意思,它使用Pub/Sub模式来实现。

当你缓存一个Blog的列表,如果后面对某个Blog进行Update的时候,你就有两个选择:1.更新这个Blog的cache 2.移除所有关于Blog的cache。Nop选择的是后者,因为第一种方案实现起来的代价有点大,你可能需要给单独每个Blog指定一个Key来缓存起来,或者遍历所有关于Blog的cache。

当发生Blog的Update的时候,会发送一个通知事件:

  public virtual void UpdateBlogPost(BlogPost blogPost)
{
if (blogPost == null)
throw new ArgumentNullException("blogPost"); _blogPostRepository.Update(blogPost); //event notification
_eventPublisher.EntityUpdated(blogPost);
}

看一下EventPublish的实现 :

 public interface IEventPublisher
{
/// <summary>
/// Publish event
/// </summary>
/// <typeparam name="T">Type</typeparam>
/// <param name="eventMessage">Event message</param>
void Publish<T>(T eventMessage);
} using System;
using System.Linq;
using Nop.Core.Infrastructure;
using Nop.Core.Plugins;
using Nop.Services.Logging; namespace Nop.Services.Events
{
/// <summary>
/// Evnt publisher
/// </summary>
public class EventPublisher : IEventPublisher
{
private readonly ISubscriptionService _subscriptionService; /// <summary>
/// Ctor
/// </summary>
/// <param name="subscriptionService"></param>
public EventPublisher(ISubscriptionService subscriptionService)
{
_subscriptionService = subscriptionService;
} /// <summary>
/// Publish to cunsumer
/// </summary>
/// <typeparam name="T">Type</typeparam>
/// <param name="x">Event consumer</param>
/// <param name="eventMessage">Event message</param>
protected virtual void PublishToConsumer<T>(IConsumer<T> x, T eventMessage)
{
//Ignore not installed plugins
var plugin = FindPlugin(x.GetType());
if (plugin != null && !plugin.Installed)
return; try
{
x.HandleEvent(eventMessage);
}
catch (Exception exc)
{
//log error
var logger = EngineContext.Current.Resolve<ILogger>();
//we put in to nested try-catch to prevent possible cyclic (if some error occurs)
try
{
logger.Error(exc.Message, exc);
}
catch (Exception)
{
//do nothing
}
}
} /// <summary>
/// Find a plugin descriptor by some type which is located into its assembly
/// </summary>
/// <param name="providerType">Provider type</param>
/// <returns>Plugin descriptor</returns>
protected virtual PluginDescriptor FindPlugin(Type providerType)
{
if (providerType == null)
throw new ArgumentNullException("providerType"); if (PluginManager.ReferencedPlugins == null)
return null; foreach (var plugin in PluginManager.ReferencedPlugins)
{
if (plugin.ReferencedAssembly == null)
continue; if (plugin.ReferencedAssembly.FullName == providerType.Assembly.FullName)
return plugin;
} return null;
} /// <summary>
/// Publish event
/// </summary>
/// <typeparam name="T">Type</typeparam>
/// <param name="eventMessage">Event message</param>
public virtual void Publish<T>(T eventMessage)
{
var subscriptions = _subscriptionService.GetSubscriptions<T>();
subscriptions.ToList().ForEach(x => PublishToConsumer(x, eventMessage));
} }
}

很简单,只是获取所有的订阅,然后依次调用其中的PublishToConsumer方法。

那么订阅是在哪里呢?

首先这是Blog消息消费者的定义:

using Nop.Core.Caching;
using Nop.Core.Domain.Blogs;
using Nop.Core.Domain.Catalog;
using Nop.Core.Domain.Configuration;
using Nop.Core.Domain.Directory;
using Nop.Core.Domain.Localization;
using Nop.Core.Domain.Media;
using Nop.Core.Domain.News;
using Nop.Core.Domain.Orders;
using Nop.Core.Domain.Polls;
using Nop.Core.Domain.Topics;
using Nop.Core.Domain.Vendors;
using Nop.Core.Events;
using Nop.Core.Infrastructure;
using Nop.Services.Events; namespace Nop.Web.Infrastructure.Cache
{
/// <summary>
/// Model cache event consumer (used for caching of presentation layer models)
/// </summary>
public partial class ModelCacheEventConsumer: //blog posts
IConsumer<EntityInserted<BlogPost>>,
IConsumer<EntityUpdated<BlogPost>>,
IConsumer<EntityDeleted<BlogPost>> {
/// <summary>
/// Key for blog tag list model
/// </summary>
/// <remarks>
/// {0} : language ID
/// {1} : current store ID
/// </remarks>
public const string BLOG_TAGS_MODEL_KEY = "Nop.pres.blog.tags-{0}-{1}";
/// <summary>
/// Key for blog archive (years, months) block model
/// </summary>
/// <remarks>
/// {0} : language ID
/// {1} : current store ID
/// </remarks>
public const string BLOG_MONTHS_MODEL_KEY = "Nop.pres.blog.months-{0}-{1}";
public const string BLOG_PATTERN_KEY = "Nop.pres.blog"; private readonly ICacheManager _cacheManager; public ModelCacheEventConsumer()
{
//TODO inject static cache manager using constructor
this._cacheManager = EngineContext.Current.ContainerManager.Resolve<ICacheManager>("nop_cache_static");
} //Blog posts
public void HandleEvent(EntityInserted<BlogPost> eventMessage)
{
_cacheManager.RemoveByPattern(BLOG_PATTERN_KEY);
}
public void HandleEvent(EntityUpdated<BlogPost> eventMessage)
{
_cacheManager.RemoveByPattern(BLOG_PATTERN_KEY);
}
public void HandleEvent(EntityDeleted<BlogPost> eventMessage)
{
_cacheManager.RemoveByPattern(BLOG_PATTERN_KEY);
}
}
}

所有的Blog的key都采用统一的前缀,Nop.pres.blog。这样只要使用这个前缀就能清楚所有关于blog的缓存了。

这个类继承了3个接口所以有3个HandleEvent的实现,都是清楚blog相关的缓存。

这些消费者其实并未主动的去注册订阅,而是通过反射在启动的时候自动加载进IoC容器里的,当需要使用的时候通过接口直接取出来使用。

            //Register event consumers
var consumers = typeFinder.FindClassesOfType(typeof(IConsumer<>)).ToList();
foreach (var consumer in consumers)
{
builder.RegisterType(consumer)
.As(consumer.FindInterfaces((type, criteria) =>
{
var isMatch = type.IsGenericType && ((Type)criteria).IsAssignableFrom(type.GetGenericTypeDefinition());
return isMatch;
}, typeof(IConsumer<>)))
.InstancePerLifetimeScope();
}
builder.RegisterType<EventPublisher>().As<IEventPublisher>().SingleInstance();
builder.RegisterType<SubscriptionService>().As<ISubscriptionService>().SingleInstance();

其中Pub/Sub是其中的精髓,非常值得学习。

Nop中的Cache浅析的更多相关文章

  1. JavaScript中闭包之浅析解读

    JavaScript中的闭包真心是一个老生常谈的问题了,最近面试也是一直问到,我自己的表述能力又不能完全支撑起来,真是抓狂.在回来的路上,我突然想到了一个很简单的事情,其实我们在做项目时候,其实就经常 ...

  2. [转]学习Nop中Routes的使用

    本文转自:http://www.cnblogs.com/miku/archive/2012/09/27/2706276.html 1. 映射路由 大型MVC项目为了扩展性,可维护性不能像一般项目在Gl ...

  3. cache 浅析

    http://blog.chinaunix.net/uid-26817832-id-3244916.html   1. Cache Cache一词来源于法语,其原意是"藏匿处,隐秘的地方&q ...

  4. Linux内存中的Cache真的能被回收么?

    在Linux系统中,我们经常用free命令来查看系统内存的使用状态.在一个RHEL6的系统上,free命令的显示内容大概是这样一个状态: [root@tencent64 ~]# free       ...

  5. 在Spring中使用cache(EhCache的对象缓存和页面缓存)

    Spring框架从version3.1开始支持cache,并在version4.1版本中对cache功能进行了增强. spring cache 的关键原理就是 spring AOP,通过 spring ...

  6. 浅谈数据库系统中的cache

    Cache和Buffer是两个不同的概念,简单的说,Cache是加速“读”,而buffer是缓冲“写”,前者解决读的问题,保存从磁盘上读出的数据,后者是解决写的问题,保存即将要写入到磁盘上的数据.在很 ...

  7. Linux内核中的Cache段

    Linux内核中的Cache段 原文地址:http://blogold.chinaunix.net/u2/85263/showart_1743693.html 最近移植LEON3的内核时,了解了一些简 ...

  8. Linux 内存中的Cache,真的能被回收么?

    您真的了解Linux的free命令么? 在Linux系统中,我们经常用free命令来查看系统内存的使用状态.在一个RHEL6的系统上,free命令的显示内容大概是这样一个状态: 这里的默认显示单位是k ...

  9. Linux内核中内存cache的实现【转】

    Linux内核中内存cache的实现 转自:http://blog.chinaunix.net/uid-127037-id-2919545.html   本文档的Copyleft归yfydz所有,使用 ...

随机推荐

  1. 登陆数据库,界面一直保持正在登陆的状态,oracle使用界面无法登陆

    原因:关机时没有关闭oracle窗口. 导致在登陆数据库的时候,使用oracle的这个界面登陆时,界面一直保持''正在登陆''的状态,一旦点击就会卡住,使界面变得无法响应. 然后使用sqlplus仍然 ...

  2. ASP.NET Core 源码阅读笔记(1) ---Microsoft.Extensions.DependencyInjection

    这篇随笔主要记录一下ASP.NET Core团队实现默认的依赖注入容器的过程,我的理解可能并不是正确的. DependencyInjection这个项目不大,但却是整个ASP.NET Core的基础, ...

  3. Android安全开发之浅谈密钥硬编码

    Android安全开发之浅谈密钥硬编码 作者:伊樵.呆狐@阿里聚安全 1 简介 在阿里聚安全的漏洞扫描器中和人工APP安全审计中,经常发现有开发者将密钥硬编码在Java代码.文件中,这样做会引起很大风 ...

  4. Unity3D游戏开发初探—3.初步了解U3D物理引擎

    一.什么是物理引擎? 四个世纪前,物理学家牛顿发现了万有引力,并延伸出三大牛顿定理,为之后的物理学界的发展奠定了强大的理论基础.牛顿有句话是这么说的:“如果说我看得比较远的话,那是因为我站在巨人的肩膀 ...

  5. SSRS(rdl报表)分页显示表头和对表头的冻结处理

    基础环境 最近在公司做西门子某系统的二次开发,需要用到SQLServer Reporting Services(SSRS).我们用的SQL版本是SQLServer 2008 R2:在设计报表时,表格用 ...

  6. SQL Server 深入解析索引存储(上)

    标签:SQL SERVER/MSSQL SERVER/数据库/DBA/索引体系结构/堆/聚集索引 概述 最近要分享一个课件就重新把这块知识整理了一遍出来,篇幅有点长,想要理解的透彻还是要上机实践. 聚 ...

  7. 【干货】JS版汉字与拼音互转终极方案,附简单的JS拼音输入法

    前言 网上关于JS实现汉字和拼音互转的文章很多,但是比较杂乱,都是互相抄来抄去,而且有的不支持多音字,有的不支持声调,有的字典文件太大,还比如有时候我仅仅是需要获取汉字拼音首字母却要引入200kb的字 ...

  8. cookie自动登录的实现

         cookie自动登录是指把用户登录的信息按期限(自定)保存在客户端,当用户请求登录时判断客户端用没有cookie对象,有的话填充值,否则登录界面的输入框为空,不进行填充.      登录界面 ...

  9. 在JS中获取文件点之后的后缀字符

    var upFileName = $("#fileToUpload").val();var index1=upFileName.lastIndexOf(".") ...

  10. appledoc 使用

    1.安装 git clone git://github.com/tomaz/appledoc.git cd ./appledoc sudo sh install-appledoc.sh 2.使用 进入 ...