微软企业库的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. 【object-c基础】Object-c基础之三:面对对象开发@interface,@implementation

    1.@interface 在java等语言编程中,创建类都是用class,但在object-c中,用@interface. 例子: @interface circle :NSObject    //定 ...

  2. Codeforces 4D Mysterious Present

    http://codeforces.com/contest/4/problem/D 题目大意: 给出n个信封,这n个信封有长和宽,给出卡片的尺寸,求取能够装入卡片的最长的序列,序列满足后一个的长和宽一 ...

  3. MVC3路由设置访问后缀 html jsp

     C# Code  12345678910111213141516171819202122232425262728293031323334353637383940414243444546   publ ...

  4. Why Memory Barrier?

    引言:xchg做了什么? 首先,xchg eax, ecx并不会比mov edx, eax + mov eax, ecx + mov ecx, edx这三条指令加一起快,原因是xchg有副作用. Mi ...

  5. 如何做Gibbs采样(how to do gibbs-sampling)

    原文地址:<如何做Gibbs采样(how to do gibbs-sampling)> 随机模拟 随机模拟(或者统计模拟)方法最早有数学家乌拉姆提出,又称做蒙特卡洛方法.蒙特卡洛是一个著名 ...

  6. 推荐2个小工具 .NET reflector resharper

  7. paip.hadoop的应用研究总结

    paip.hadoop的应用研究总结 作者Attilax ,  EMAIL:1466519819@qq.com  来源:attilax的专栏 地址:http://blog.csdn.net/attil ...

  8. Hibernate的查询 HQL查询 查询某几列

    HQL 是Hibernate Query Language的简写,即 hibernate 查询语言:HQL采用面向对象的查询方式.HQL查询提供了更加丰富的和灵活的查询特性,因此Hibernate将H ...

  9. [Cycle.js] Fine-grained control over the DOM Source

    Currently in our main() function,  we get click$ event. function main(sources) { const click$ = sour ...

  10. word-wrap 和 word-break

    一.word-wrap 1.浏览器支持 所有主流浏览器都支持 word-wrap属性 2.定义和用法 word-wrap 属性允许长单词或 URL 地址换行到下一行. 语法 word-wrap: no ...