微软企业库的Cache

通常,应用程序可以将那些频繁访问的数据,以及那些需要大量处理时间来创建的数据存储在内存中,从而提高性能。基于微软的企业库,我们的快速创建一个缓存的实现。

新建PrismSample.Infrastructure.Cache

新建一个类库项目,将其命名为PrismSample.Infrastructure.Cache,然后从nuget中下载微软企业库的Cache。

然后新建我们的CacheManager类:

using Microsoft.Practices.EnterpriseLibrary.Caching;
using System.ComponentModel.Composition; namespace PrismSample.Infrastructure.Cache
{
[Export("PrismSampleCache", typeof(ICacheManager))]
public class CacheManager : ICacheManager
{
ICacheManager _cacheManager; public CacheManager()
{
_cacheManager = CacheFactory.GetCacheManager("MemoryCacheManager");
} public object this[string key]
{
get
{
return _cacheManager[key];
}
} public int Count
{
get
{
return _cacheManager.Count;
}
} public void Add(string key, object value)
{
_cacheManager.Add(key, value);
} public void Add(string key, object value, CacheItemPriority scavengingPriority, ICacheItemRefreshAction refreshAction, params ICacheItemExpiration[] expirations)
{
_cacheManager.Add(key, value, scavengingPriority, refreshAction, expirations);
} public bool Contains(string key)
{
return _cacheManager.Contains(key);
} public void Flush()
{
_cacheManager.Flush();
} public object GetData(string key)
{
return _cacheManager.GetData(key);
} public void Remove(string key)
{
_cacheManager.Remove(key);
}
}
}

其中MemoryCacheManager是我们稍后需要在App.config文件中配置的。

修改生成后事件:

xcopy "$(TargetPath)" "$(SolutionDir)\PrismSample\bin\Debug\" /Y

之所以添加生成后事件,是因为Cache的类库的引入不是通过Project间的项目引用来实现的,是在运行时MEF的容器去寻找指令集,所以我们把程序集都放置到运行目录下。

修改App.config配置文件

<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="cachingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Caching.Configuration.CacheManagerSettings, Microsoft.Practices.EnterpriseLibrary.Caching" />
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Microsoft.Practices.ServiceLocation" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-1.3.0.0" newVersion="1.3.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
<cachingConfiguration defaultCacheManager="MemoryCacheManager">
<cacheManagers>
<add name="MemoryCacheManager" type="Microsoft.Practices.EnterpriseLibrary.Caching.CacheManager, Microsoft.Practices.EnterpriseLibrary.Caching" expirationPollFrequencyInSeconds="60" maximumElementsInCacheBeforeScavenging="1000" numberToRemoveWhenScavenging="10" backingStoreName="NullBackingStore" />
</cacheManagers>
<backingStores>
<add type="Microsoft.Practices.EnterpriseLibrary.Caching.BackingStoreImplementations.NullBackingStore, Microsoft.Practices.EnterpriseLibrary.Caching"
name="NullBackingStore" />
</backingStores>
</cachingConfiguration>
</configuration>

配置文件的生成可以通过微软企业库的工具(配置方式)

修改Bootstrapper,将目录下符合条件的dll全部导入

using Prism.Mef;
using PrismSample.Infrastructure.Abstract.Presentation.Interface;
using System.ComponentModel.Composition.Hosting;
using System.Windows;
using Prism.Logging;
using PrismSample.Infrastructure.Logger;
using System.IO;
using System; namespace PrismSample
{
public class Bootstrapper : MefBootstrapper
{
private const string SEARCH_PATTERN = "PrismSample.Infrastructure.*.dll"; protected override DependencyObject CreateShell()
{
IViewModel shellViewModel = this.Container.GetExportedValue<IViewModel>("ShellViewModel");
return shellViewModel.View as DependencyObject;
} protected override void InitializeShell()
{
Application.Current.MainWindow = (Shell)this.Shell;
Application.Current.MainWindow.Show();
} protected override void ConfigureAggregateCatalog()
{
base.ConfigureAggregateCatalog(); //加载自己
this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(this.GetType().Assembly)); //加载当前目录
DirectoryInfo dirInfo = new DirectoryInfo(@".\");
foreach (FileInfo fileInfo in dirInfo.EnumerateFiles(SEARCH_PATTERN))
{
try
{
this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(fileInfo.FullName));
}
catch (Exception ex)
{
this.Logger.Log( string.Format("导入异常:{0}", ex.Message), Category.Exception, Priority.None);
}
}
} protected override ILoggerFacade CreateLogger()
{
return new Logger();
}
}
}

测试运行

修改ShellViewModel的构造函数

[ImportingConstructor]
public ShellViewModel([Import("ShellView", typeof(IView))]IView view,
[Import]ILoggerFacade logger,
[Import("PrismSampleCache", typeof(ICacheManager))] ICacheManager _cacheManager)
{
this.View = view;
this.View.DataContext = this; _cacheManager.Add("SampleValue", "CacheValue");
this._text = _cacheManager.GetData("SampleValue").ToString();
logger.Log("ShellViewModel Created", Category.Info, Priority.None);
}

运行结果:

小结

本文用微软企业库实现了一个简单的缓存系统。
源码下载

参考信息

patterns & practices – Enterprise Library
黄聪:Microsoft Enterprise Library 5.0 系列教程(一) : Caching Application Block (初级)

微软企业库的Cache的更多相关文章

  1. Prism6下的MEF:基于微软企业库的Cache

    通常,应用程序可以将那些频繁访问的数据,以及那些需要大量处理时间来创建的数据存储在内存中,从而提高性能.基于微软的企业库,我们的快速创建一个缓存的实现. 新建PrismSample.Infrastru ...

  2. 微软企业库5.0 学习之路——第八步、使用Configuration Setting模块等多种方式分类管理企业库配置信息

    在介绍完企业库几个常用模块后,我今天要对企业库的配置文件进行处理,缘由是我打开web.config想进行一些配置的时候发现web.config已经变的异常的臃肿(大量的企业库配置信息充斥其中),所以决 ...

  3. 微软企业库5.0 学习之路——第四步、使用缓存提高网站的性能(EntLib Caching)

    首先先补习下企业库的Caching Application Block的相关知识: 1.四大缓存方式,在Caching Application Block中,主要提供以下四种保存缓存数据的途径,分别是 ...

  4. 分享一个大型进销存供应链项目(多层架构、分布式WCF多服务器部署、微软企业库架构)

    项目源码下载:  WWW.DI81.COM 分享一个大型进销存供应链项目(多层架构.分布式WCF多服务器部署.微软企业库架构) 这是一个比较大型的项目,准备开源了.支持N家门店同时操作.远程WCF+企 ...

  5. 在数据库访问项目中使用微软企业库Enterprise Library,实现多种数据库的支持

    在我们开发很多项目中,数据访问都是必不可少的,有的需要访问Oracle.SQLServer.Mysql这些常规的数据库,也有可能访问SQLite.Access,或者一些我们可能不常用的PostgreS ...

  6. 微软企业库5.0学习-Security.Cryptography模块

    一.微软企业库加密应用模块提供了两种加密: 1.Hash providers :离散加密,即数据加密后无法解密 2.Symmetric Cryptography Providers:密钥(对称)加密法 ...

  7. [EntLib]微软企业库5.0 学习之路——第一步、基本入门

    话说在大学的时候帮老师做项目的时候就已经接触过企业库了但是当初一直没明白为什么要用这个,只觉得好麻烦啊,竟然有那么多的乱七八糟的配置(原来我不知道有配置工具可以进行配置,请原谅我的小白). 直到去年在 ...

  8. 使用微软企业库5.0提供的unity配置解藕系统demo(源码)

    最近公司集50多号开发人员的人力围绕一个系统做开发,框架是免不了要统一的,公司提供的架构,利于分工合作,便于维护,扩展,升级,其中使用了到微软的企业库来解藕系统,只是因为框架封装,于是在网上学习了一个 ...

  9. 微软企业库Microsoft Enterprise Library的相关文章链接

    微软企业库4.1学习笔记 http://blog.csdn.net/anyqu/article/category/1228691/3 黄聪:Enterprise Library 5.0 系列教程 ww ...

随机推荐

  1. VC中如何获取当前时间(精度达到毫秒级)

    标 题: VC中如何获取当前时间(精度达到毫秒级)作 者: 0xFFFFCCCC时 间: 2013-06-24链 接: http://www.cnblogs.com/Y4ng/p/Millisecon ...

  2. ubuntu "mkdir -p"命令

    mkdir的-p选项允许你一次性创建多层次的目录,而不是一次只创建单独的目录.例如,我们要在当前目录创建目录Projects/a/src,使用命令: mkdir -p Project/a/src 而不 ...

  3. Eclipse formater(google Java 编码规范)

    1. 谷歌Java编码规范 http://google-styleguide.googlecode.com/svn/trunk/javaguide.html 2. 下载配置文件: https://co ...

  4. 使用jquery 操作checkbox

    checkbox 的全选与全不选以及获取选择的值. 效果: <!DOCTYPE html> <html lang="en"> <head> &l ...

  5. Some General concepts in ISO C

    [ISO C11 Clause 3]对象(object):执行环境中数据存储的一块区域,它的内容可以用来表示值.-注释:对象可以具有特定的类型.--值(value):确定类型的对象的内容的确切含义.- ...

  6. C++基础回顾1(数据类型, 控制语句, 数组)

    最近两天打开本科学校的C++教材,快速回顾了一下C++方面的内容.虽然书本内容比较基础,但是还是有些知识点值得自己强化记忆.分几篇文章,加上自己的理解记录如下. 先回顾面向过程的部分. C++数据类型 ...

  7. 齐B小短裙

    女模周蕊微博引关注 火了齐B小短裙毁了干爹[图]_网易新闻中心 齐B小短裙

  8. cocos2d-x ios 设置横屏/竖屏(全)

    Cocos2d-x项目\iOS\RootViewController.mm文件中. 以下方法任选其一即可…      本人机子函数二ok! 函数一: (BOOL)shouldAutorotateToI ...

  9. 第32讲 UI组件之 时间日期控件DatePicker和TimePicker

    第32讲 UI组件之 时间日期控件DatePicker和TimePicker 在Android中,时间日期控件相对来说还是比较丰富的.其中, DatePicker用来实现日期输入设置,    Time ...

  10. mysql 获取当前时间戳

      mysql 获取当前时间为select now() 运行结果: 2012-09-05 17:24:15 mysql 获取当前时间戳为select unix_timestamp(now()) 运行结 ...