企业库系列讲座:缓存应用程序块.ppt

上传人:牧羊曲112 文档编号:6240704 上传时间:2023-10-09 格式:PPT 页数:49 大小:882KB
返回 下载 相关 举报
企业库系列讲座:缓存应用程序块.ppt_第1页
第1页 / 共49页
企业库系列讲座:缓存应用程序块.ppt_第2页
第2页 / 共49页
企业库系列讲座:缓存应用程序块.ppt_第3页
第3页 / 共49页
企业库系列讲座:缓存应用程序块.ppt_第4页
第4页 / 共49页
企业库系列讲座:缓存应用程序块.ppt_第5页
第5页 / 共49页
点击查看更多>>
资源描述

《企业库系列讲座:缓存应用程序块.ppt》由会员分享,可在线阅读,更多相关《企业库系列讲座:缓存应用程序块.ppt(49页珍藏版)》请在三一办公上搜索。

1、November 2004,Enterprise LibraryCaching Application Block,莫淘Architect EvangelistDevelop&Platform EvangelismMicrosoft(China),November 2004,温故知新,什么是Patterns&practicesApplication Block的发展,November 2004,Patterns&practicesArchitecture Guidance for the Enterprise,Reference Architectures,Application Blocks

2、,Guides Guidance for broad horizontal topics such as security,performance,deployment and operations,Patterns,D,A,D,I,A,D,I,Atomic solutions to recurring problems,Sub-system-level guidance for common services,System-level guidance for common customer scenarios,D,A,D,I,A,D,I,Available online:Books ava

3、ilable:,ProvenBased on field experienceAuthoritative Offer the best advice available AccurateTechnically validated and tested ActionableProvide the steps to success RelevantAddress real-world problems based on customer scenarios,FactoryPlugin,Enterprise library,Indigo,November 2004,App Block History

4、&Feedback,发展历史(History)应用程序块(Application blocks)已经诞生多年在Microsoft内部,程序块产生在不同的设计组中,并演化了多年程序块涉及的范围very simple helpers powerful frameworks like UIP几点共识降低项目成本增加解决方案的可用性业界反馈(Feedback)提供了程序块的一致性 提供了模块之间更少的依赖性On other blocks On infrastructure更容易的配置 易用性,November 2004,Security,Crypto,Configuration,Data Access

5、,Logging,Caching,Exceptions,Enterprise Library v1总体架构,Legend,Dependency,Plug-in,ConfigTool,November 2004,Enterprise Library Vision,Partner blocks,Customer blocks,Community blocks,p&p blocks,Block Specification,November 2004,Enterprise Library 的定义,Enterprise Library 是是面对一些企业级应用开发中常见的问题的解决方案库(library

6、of application blocks which solve common challenges)是一系列可以工作在任何架构下的帮助类(A set of helper classes which work in any architectural style)是体系架构上的指导性文件(Architectural guidance embodied in code which ships with full source allowing you to modify and extend)是免费的(Available as a free download),Enterprise Libra

7、ry 不是不是.Net架构中的一部分(A part of the.NET Framework)不对任何体系架构后依赖或影响关系(An application framework that imposes an architectural style)不是一种官方支持的产品(A Microsoft product with support,compatibility and localization)不用于销售(For sale),November 2004,Enterprise的未来之路,微软将一如既往的在改领域进行投入(We will continue to invest in blocks

8、 and block libraries)将不断的扩展、升级该方案库(Additional capabilities and scenarios)New blocks,such as ValidationUpdating existing application blocks,such as UIPScenarios such as Smart Client融入其他的产品(Enablement)Visual Studio tooling and wizards Deeper community penetration会出现在新的.NET 2.0/Whidbey 上的方案Blocks revve

9、d to adhere to best practices of new platformIntegration with new VS2005 experiences,November 2004,今天的交流题目,Patterns&practices 中关于缓冲的指导意见关于Caching Application Block应用场景和设计指南的描述应用示例问题解答,November 2004,有同感?,用户抱怨系统的性能您已经将明年的预算花在如何扩展现有的数据库服务器的吞吐能力上了您的应用程序在没有和后端的数据源连接时,程序不能够执行或执行的很不好,November 2004,为什么需要Cac

10、he?,性能采用一种更为密切的方式,为数据的消费者存储相关的数据;以避免重复性的数据创建、处理和传输。可扩展性避免在相同的数据处理和业务逻辑上浪费资源。减小数据库的请求,以便允许更多的客户访问。可用性在另外的地方存储数据。您的应用能够在网络的的延时、WEB服务和硬件的问题中减小损失,November 2004,典型的应用场景,提问:以前不是有缓冲吗?回答:全面性+易用性+一致性+您正在使用本地的缓冲创建智能客户端应用使用缓冲功能,支持离线应用您正在创建WINDOWS服务或者控制台应用,并且需要使用缓冲改善性能,November 2004,什么是缓冲应用程序块?,提供了一些方便易用的、可扩展的缓

11、冲机制。这些缓冲机制可以使用在整体应用中的各个层面。支持后期存储,支持的存储方式包括数据库方式和独立存储方式(Isolated storage),便于应用方便的重新启动便于使用便于配置支持使用配置工具线程安全可以确保在内存中的缓冲和后端存储保持同步,November 2004,深入了解缓冲模块,如何创建缓冲配置文件选择后端存储创建缓冲向缓冲中添加一项设置过期策略从缓冲中获得一项内容缓冲装载策略从缓冲中移除一项刷新缓冲关键特性,November 2004,Security,Crypto,Configuration,Data Access,Logging,Caching,Exceptions,Ca

12、che AB 所处的地位,Legend,Dependency,Plug-in,ConfigTool,November 2004,在您的应用配置文件中增加一个缓冲应用为要创建的数据项创建 Cache manager每一个子项需要有独立的名字确定哪一个是默认的Cache manager,创建配置文件,November 2004,关于缓冲存储的几点建议,内存驻留型缓冲的典型应用:应用程序经常使用同样的数据一个应用程序经常需要重新获得数据磁盘驻留型缓冲的典型应用:数据量比较大同时,从应用服务提供商(例如数据库)重新获取数据,开销比较大在缓冲的生命周期中,必须经历系统的重新启动,Caching Arch

13、itecture Guide for.NET Framework Applications,November 2004,缓冲通常在内存中还经常需要有后端存储 没有后端存储,是不容易实现持久化的因此持久化存储是非常必要的 持久化的后端存储当需要被缓冲的数据的生命周期中不可避免的需要经历进程或系统的重启时支持使用隔离式存储和数据库方式(Data Access Application Block)内容需要和缓冲一致(Rollback)内容是从缓冲的初始化时装载到后端的,关于缓冲存储的几点建议(2),November 2004,关于缓冲存储的几点建议(3),隔离式存储支持基于用户和程序集的隔离机制不需

14、要Unique Path了避免Hard CodeCaching Application Block 提供了分区机制,在一个应用程序中,并存多个cache managers不支持,也不应该支持多个由多个Cache manager共享存储或分区,November 2004,关于缓冲存储配置,缓冲还要加密?内存溢出;木马程序Data Access Application Block利用数据库实例进行分区的命名和数据的分离需要数据库访问模块(DAAB)的支持,November 2004,如何加密?需要 Cryptography Application Block 模块的支持应该使用对称算法,关于缓冲存

15、储配置(2),November 2004,创建默认 cache manager,典型用法,CacheManager myCache=CacheManager.GetCacheManager();,创建命名的cache manager,CacheManager productsCache=CacheManager.GetCacheManager(“Products”);,November 2004,增加条目,默认的增加一个条目,productsCache.Add(“ProductID123”,productObject);,默认值清除优先级:Normal过期设置:No expiration注意增

16、加第二个同样KEY值的条目,会替换掉第一个当希望使用后端存储时,对象应该支持序列化,November 2004,定义过期策略,基于时间的过期设置过期时间支持两种:相对时间绝对时间对于使用那些变化比较大的数据,例如经常被更新的数据或只在一段时间内存在的数据时,建议存在在缓冲中基于提醒的过期,且支持策略对该资源属性的更改,对应的提醒(Such as a file,a folder,or any other type of data source)只是Remove,November 2004,基于时间的过期,绝对时间(Absolute):允许您定义缓冲项的生命周期简单的方法指定一个特定的时间作为过期

17、 扩展方法您可以通过表达式定义Every minuteEvery Sunday15th of Every month 变化的时间允许您定义针对条目的被调用的两次之间的间隔,定义条目的生命周期,November 2004,基于时间的实例,DateTime refreshTime=new DateTime(2005,3,21,2,0,0);AbsoluteTime expireTime=new AbsoluteTime(refreshTime);primitivesCache.Add(Key1,Cache Item1,CacheItemPriority.Normal,null,expireTime

18、);,需要使用ADD方法Caching Application Block 支持基于时间的表达式AbsoluteTimeSlidingTimeExtendedFormatTime例如:expire at 2:00 AM on 3/21/05,November 2004,变化时间的过期,TimeSpan refreshTime=new TimeSpan(0,5,0);SlidingTime expireTime=new SlidingTime(refreshTime);primitivesCache.Add(Key1,Cache Item1,CacheItemPriority.Normal,nu

19、ll,expireTime);,例如:被访问5分钟后,November 2004,支持的时间表达方法和示例,Extended time format“”*means run every periodExamples“*”expires every minute“5*”expire 5th minute of every hour“*21*”expire every minute of the 21st hour of every day“31 15*”expire 3:31 PM every day“7 4*6”expire Saturday 4:07 AM“15 21 4 7*”expire

20、 9:15 PM on 4 July,November 2004,扩展时间应用范例,ExtendedFormatTime expireTime=new ExtendedFormatTime(0 0*6);primitivesCache.Add(Key1,Cache Item1,CacheItemPriority.Normal,null,expireTime);,扩展时间应用范例:expire at midnight every Saturday,November 2004,基于提醒机制的过期,FileDependencyexpireNotice=new FileDependency(“Trig

21、ger.txt”);productsCache.Add(Key1,Cache Item1,CacheItemPriority.Normal,null,expireNotice);,文件依赖的例子:expire if the file Trigger.txt is changed,您可以创建自己的过期类,只需要实现 ICacheItemExpiration接口,November 2004,配置过期表决的频率,通过后台线程(BackgroundScheduler),移除过期事项您可以对次线程进行配置过高:CPU浪费,且CACHE没有起到作用过低:内存消耗太大建议使用性能计数器监视一下,Novemb

22、er 2004,条目移除提示,productsCache.Add(Key1,Cache Item1,CacheItemPriority.Normal,new ProductCacheRefreshAction(),expireNotice);,Caching Application Block 提供了项目移除的提醒,并在一下情况下被激活条目过期了条目被显式的移除了条目被策略的清楚了需要实现 ICacheItemRefreshAction接口,November 2004,条目移除提示(2),Serializablepublic class ProductCacheRefreshAction:IC

23、acheItemRefreshAction public void Refresh(string key,object expiredValue,CacheItemRemovedReason removalReason)/Item has been removed from cache./Perform desired actions here,/based upon the removal reason(e.g.refresh the cache with the/item).,一个类实现了 ICacheItemRefreshAction 接口,同时如果需要后端存储时,还必须被标识为 Ser

24、ializable(Especially for persistent backing store),November 2004,类型要正确一定要检查空值(item not found in cache),从Cache manager中获取,public Product ReadProductByID(string productID)Product product=(Product)cache.GetData(productID);if(product=null)/Item not in cache,类型转换,检查空值,November 2004,装载缓冲,缓冲的前期装载(Proactive

25、 loading)应用启动时装载优点全部装载后,应用运行性能提升明显缺点启动时间长可能带来不必要的资源浪费 为了提升启动性能而进行的基于不同线程的装载,有造成了应用结构的复杂性缓冲的被动装载(Reactive loading)按需装载优点只有在需要的时候才装载,对资源的需求小缺点但是在首次装载的时候,速度慢,November 2004,1:Create cache manager,主动装载的实例,CacheManager productsCache=CacheManager.GetCacheManager();,/Retrieve the data from the sourceArrayLi

26、st list=dataProvider.GetProductList();/Add all the items to the cachefor(int i=0;i list.Count;i+)Product product=(Product)listi;productsCache.Add(product.ProductID,product);,2:Add items during component initialization,November 2004,后期装载的实例,Product product=(Product)productsCache.GetData(productID);if

27、(product=null)/Retrieve it from the data provider/and cache it for more requests.product=dataProvider.GetProductByID(productID);if(product!=null)productsCache.Add(productID,product);,1:Create cache manager,CacheManager productsCache=CacheManager.GetCacheManager();,2:Add items when retrieved from dat

28、a source,November 2004,何时使用主动装载(Proactive caching)在一些情况下中,他们自己有更新周期。当装载到缓冲将导致状态过期的出现You are using static or semistaticstate that has known update periods.If you use it in other scenarios,the state might expire before it is used.此时,您需要清楚的知道被缓冲的对象的生命周期You are using state with a known lifetime您还需要提前知道占

29、用资源的程度You are using state of a known size.If you use proactive cache data loading when you do not know the size of the data,you might exhaust system resources.You must try to not use resources that you do not have.使用不稳定的资源时,尽量多使用主动装载缓冲You have problematic resources,such as a slow database,a slow net

30、work,or unreliable Web services.You can use this technique to retrieve all the state proactively,cache it,and work against the cache as much as it can.,最佳实践Cache主动装载,November 2004,何时使用被动装载:需要缓冲的对象状态过多或系统资源不足的情况You are using lots of state and you do not have sufficient resources to cache all state fo

31、r the entire application.资源的可靠性和性能良好,此时被动装载的又是更明显You are using reliable and responsive resources,such as a database,network,or Web service that will not impede application stability and performance.希望利用缓冲机制,但是在应用程序的初始化时,希望不使用缓冲,而是根据用户输入等条件,进行缓冲You are interested in caching data that is not available

32、 during the initialization of an application.For example,this data might be affected by user input such as common search queries or user-specific data such as a users profile.,最佳实践Cache被动装载,November 2004,1:Remove item with specified key,从缓冲中移除,productsCache.Remove(“Product101Key”),2:No error occurs

33、if key is not found因此通过这种方法,不能知道ITEM是否移除了如果对象实现了ICacheRefreshItemAction,可以有信息,November 2004,刷新缓冲,为了更有效的利用资源,您需要刷新缓冲Use to manage storage,memory and other resources efficiently精确刷新Explicit flushing使用代码Initiated by application code全部移除Removes all items from the cache自我清除Scavenging使用应用程序块的功能Initiated b

34、y application block基于优先级和最后访问的时间Based upon priority and last access time控制移除的精确度Configuration settings control size of cache and number removed,productsCache.Flush(),November 2004,自我清除(1),自我清除的配置缓冲中的最大元素数量Maximum elements in cache一次移除的数量Number of items removed when scavenging occurs,November 2004,自我

35、清除(2),为缓冲建立优先级,productsCache.Add(Key1,Cache Item1,CacheItemPriority.High,new ProductCacheRefreshAction(),expireNotice),优先级的值LowNormalHighNotRemovable自我清除的算法,不需要扩展,View/Application Share:Demonstration,Live Meeting View/Application Share.Use Live Meeting Edit Slide Properties.to edit.,November 2004,再次

36、重复几个关键点(1),支持客户化后端存储(Backing store provider)Implement IBackingStore(derive from BaseBackingStore)Use Configuration Console to select custom provider支持客户化过期策略 Expiration policyImplement ICacheItemExpirationSpecify object on call to Add method of CacheManager object另外Anything and everything you have t

37、he source code!Please post extensions and suggestions to the community,November 2004,Security,Crypto,Configuration,Data Access,Logging,Caching,Exceptions,再次重复几个关键点(2),Legend,Dependency,Plug-in,ConfigTool,Announcing:Enterprise Library 1.0,Download it Today!,November 2004,patterns&practices Live!,2005.67 Enterprise Library Logging&Instrumentation Application Block 2005.67 Enterprise Library Exception Handling Application Block2005.67 Enterprise Library Cryptography Application Block2005.67 Build your own Enterprise Library Application Block,Enterprise Library Communityhttp:/=39209&clcid=0 x09,

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 生活休闲 > 在线阅读


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号