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. bzoj 2739 最远点

    Description 给你一个N个点的凸多边形,求离每一个点最远的点. Input 本题有多组数据,第一行一个数T,表示数据组数. 每组数据第一行一个数N,表示凸多边形点的个数,接下来N对数,依次表 ...

  2. Azure Redis Cache

    将于 2014 年 9 月 1 日停止Azure Shared Cache服务,因此你需要在该日期前迁移到 Azure Redis Cache.Azure Redis Cache包含以下两个层级的产品 ...

  3. CSS3魔法堂:CSS3滤镜及Canvas、SVG和IE滤镜替代方案详解

    一.前言    IE特有的滤镜常常作为CSS3各种新特性的降级处理补充,而Adobe转向HTML5后与Chrome合作推出CSS3的Filter特性,因此当前仅Webkit内核的浏览器支持CSS3 F ...

  4. LibSVM for Python 使用

    经历手写SVM的惨烈教训(还是太年轻)之后,我决定使用工具箱/第三方库 Python libsvm的GitHub仓库 LibSVM是开源的SVM实现,支持C, C++, Java,Python , R ...

  5. [.net 面向对象程序设计进阶] (18) 多线程(Multithreading)(三) 利用多线程提高程序性能(下)

    [.net 面向对象程序设计进阶] (18) 多线程(Multithreading)(二) 利用多线程提高程序性能(下) 本节导读: 上节说了线程同步中使用线程锁和线程通知的方式来处理资源共享问题,这 ...

  6. 神兵利器——Alfred

    有人的地方就有江湖,有江湖就有纷争. 很多人说我的文字风格相对轻松和温和,那是因为我很早就认识到,我们没有教育脑残和喷子的义务.在网际多年,看过太多虚拟的刀锋和鲜血,很多人被彻头彻尾的粉碎,挫骨扬灰, ...

  7. 解决vue与传统jquery插件冲突

    比如基于jquery的select2插件,在vue下单独用有很多问题,其实对于这类插件,可以用vue的自定义指令和组件来包装,解决冲突的问题.引用官方vue1.0和2.0的两个例子,学习一下. 例子1 ...

  8. AMD规范与CMD规范的区别

    AMD规范与CMD规范的区别是什么?    在比较之前,我们得先来了解下什么是AMD规范?什么是CMD规范?当然先申明一下,我个人也是总结下而已,也是网上看到的资料,自己总结下或者可以说整理下而已,供 ...

  9. MonogDB初探增加和删除

    1.插入并保存文档       在插入数据之前,首先用mongodb Shell命令db.baseUser.find() 查找集合的数据.      想必大家能猜到结果,什么东西都没有,那接着来说说怎 ...

  10. 无法启动WP Emulator

    记得以前Vware不能运行设置的东西了吗?http://www.cnblogs.com/dunitian/p/4480750.html 如果不清楚可以参考上面的链接 重启的时候选择第二项 重新打开就o ...