zoukankan      html  css  js  c++  java
  • Caching Application Block 转帖(原帖:http://pw.cnblogs.com/archive/2006/05/31/413581.html)

    注.NET技术

    Enterprise Library 2.0 -- Caching Application Block

           开始写Enterprise Library 2.0的学习体会,准备先把每个部分的入门部分写好,然后再继续深入的研究每一部分,希望能得到高手的指点和建议。今天写的是Enterprise Library 2.0 中的 Caching Application Block,前面的介绍部分参考了Enterprise Library 2.0 自带的帮助。

    一、为什么要使用缓存技术?
    当我们构建企业级分布式应用时,设计师和开发者都会面对很多挑战。Caching能够帮助他们克服这其中

    的一些困难,包括:
    1、性能(Performance):Caching主要是通过尽可能的保存一些有关的数据来提高应用程序的性能,避免了重复的从数据库中存取数据。
    2、可变性(Scalability): 利用缓存来存储数据可以有效的节省系统资源,并且能够随着应用程序需求的增加来增加可变性。
    3、有效性(Availability):通过将数据存放到本地缓存中,还可以使应用程序在系统发生故障时工作,包括硬件,网络的故障等。

    二、在什么情况下使用?
    1、当你需要频繁访问静态数据或访问的数据很少发生变化时;
    2、对数据的访问,创建或传递需要花费大量时间时;
    3、会被经常使用的数据需要进入缓存。

    三、缓存可以用在哪些项目中?
    1、WinForm
    2、Console Application
    3、Windows service
    4、Com+ Server
    5、Web Service
    6、ASP.NET Web Applicatio
    等等...

           当然,我们还应该了解的是每个应用程序都可以使用多个Cache,但不能在不同的应用程序中共享一个Cache。Enterprise Library中的Caching Application Block 的性能已经被高度优化了,并且是线程安全和异常安全的,我们还可以根据自己的需要对它进行扩展。

    四、对系统的要求
    1.Microsoft Windows 2000, Windows XP Professional, or Windows Server 2003 operating system
    2.Microsoft .NET Framework 2.0
    3.VS2005

    五、初步体验Caching Application Block

    下面我们来看一下如何使用Caching Application Block:
    首先要添加对Microsoft.Practices.EnterpriseLibrary.Caching的引用。

        [TestMethod]
            
    public void UseCaching()
            
    {
                
    int id = 0;
                
    string name = "SHY520";
        
            
    //创建一个缓存管理器
                CacheManager testcaching = CacheFactory.GetCacheManager();
                
    //添加要进行缓存的数据
                testcaching.Add(id.ToString(), name);
            
    //取出缓存中的数据
                string rname = (string)testcaching.GetData(id.ToString());

                Assert.AreEqual(name, rname);
            }

          相信通过上面的例子,你对Enterprise Library2.0中的Caching Application Block 已经有了一个初步的了解,下面我们来谈谈如何配置Caching Application Block。

    六、配置Caching Application Block
           首先打开我们建好的项目,我调试时候用的是一个Test Project,新建一个配置文件App.Config。这个时候我们在项目中选中App.Config右键点击,选择Open With...,如下图:

          然后在弹出的Choose Program 对话框中点 add ,并选择我们Enterprise Library2.0安装目录下的Bin目录里的EntLibConfig.exe文件,如下图:

         此时打开App.Config文件后出现下图:


        然后点击 File --> Save All,这样我们就完成了对Caching Application Block的配置,此时我们发现我们的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, Version=2.0.0.0, Culture=neutral, 

    PublicKeyToken=null"
     />
      
    </configSections>
      
    <cachingConfiguration defaultCacheManager="Cache Manager">
        
    <cacheManagers>
          
    <add expirationPollFrequencyInSeconds="60" 

    maximumElementsInCacheBeforeScavenging
    ="1000"
            numberToRemoveWhenScavenging
    ="10" backingStoreName="Null Storage"
            name
    ="Cache Manager" />
        
    </cacheManagers>
        
    <backingStores>
          
    <add encryptionProviderName="" 

    type
    ="Microsoft.Practices.EnterpriseLibrary.Caching.BackingStoreImplementations.NullBackingS

    tore, Microsoft.Practices.EnterpriseLibrary.Caching, Version=2.0.0.0, Culture=neutral, 

    PublicKeyToken=null"

            name
    ="Null Storage" />
        
    </backingStores>
      
    </cachingConfiguration>
    </configuration>


    七、Caching Application Block 的简单操作
    1.Adding Items to the Cache
           下面的代码中使用了CacheManager的Add方法,它创建了一个String类型的对象并将它存到缓存,缓存时间为10分钟,在这10分钟之类我们可以通过GetData()的方法来取得缓存里所存储的值。

    [TestMethod]
            
    public void UseCaching()
            
    {
                
    int id = 0;
                
    string name = "SHY520";
                
                
    //创建一个缓存管理器
                CacheManager testcaching = CacheFactory.GetCacheManager();
                
    //添加要进行缓存的数据,缓存时间为10分钟
                testcaching.Add(id.ToString(), name, CacheItemPriority.Normal, nullnew 

    SlidingTime(TimeSpan.FromMinutes(
    10)));
                
    //取出缓存的数据
                string rname = (string)testcaching.GetData(id.ToString());

                Assert.AreEqual(name, rname);
            }

    2.Loading the Cache
             Loading Cache 有两种情况:Proactive loading 和 Reactive loading。
            我理解为是主动缓存和被动缓存,主动缓存是将我们建设的项目中的一些贯穿整个系统都会用到的或是经常用到的数据而有很少改变的数据在程序初始化的时候就从数据库中取出,装入到缓存中,而被动缓存是因为需要响应某个页面或是某一段程序的请求时而将一些必须的数据临时装入缓存。二者各自有着自己的优点和缺点。主动缓存的优点有:1.因为这一类的缓存在应用程序的初始化的时候已经被装入Cache,所以我们在以后的使用中不需要判断它是否存在(当然为保险起见,我还是建议使用缓存前都应该判断一下是否为null);2、由于主动缓存中存放了我们经常需要用到的数据,所以它会使我们的程序的响应速度大大提高。但过多的使用这一类缓存会占用我们系统大量的资源,又会影响系统的运行速度,所以合理使用主动缓存非常重要。被动缓存的优点就是需要的时候才将数据装入缓存,很灵活,也不会占用系统太多资源,缺点正是因为它的灵活让我们搞不清楚它什么时候存在,使用的时候务必先检查它是否存在。

           举个例子说吧(也许不是很恰当),我们做MIS系统时,登陆是必不可少的,而且我们一般都是在用户登陆的时候将该用户的一些必要的信息存放到一定的地方,以便后面操作时调用,这个时候我们就可以使用主动缓存。

        [TestMethod]
            
    public void LoadingCache()
            

                
    //假设前面已经登陆成功,然后我们获取登陆用户的一些信息
                int id = 100;
                
    string name = "SHY520";
                
    string sex = "";

                Person person 
    = new Person(name, id, sex);
                
    //将登陆用户信息装入缓存
                CacheManager loginCache = CacheFactory.GetCacheManager();
                loginCache.Add(id.ToString(), person);

                
    //从缓存中取出登陆用户信息
                
    //注意强制转换类型
                Person outperson = (Person)loginCache.GetData(id.ToString());

                Assert.AreEqual(outperson.Sex,sex);
                Assert.AreEqual(outperson.Name,name);
                Assert.AreEqual(outperson.Id,id);

            }

    3.Flushing the Cache(清空缓存)
    将数据装入缓存就必然需要清空缓存,下面将介绍如何清空缓存:

        [TestMethod]
            
    public void FlushCache()
            
    {
                
    string name = "SHY520";
                CacheManager TestFlushCache 
    = CacheFactory.GetCacheManager();
                TestFlushCache.Add(
    "1",name);

                
    //清空缓存
                TestFlushCache.Flush();

                
    int i = TestFlushCache.Count;

                Assert.AreEqual(
    0,i);
            }

    4.Removing Items from the Cache
    本节中介绍如何移出缓存中的某一项,如下:

        [TestMethod]
            
    public void RemoveItemOfCache()
            
    {
                
    string name = "SHY520";
                CacheManager TestFlushCache 
    = CacheFactory.GetCacheManager();
                TestFlushCache.Add(
    "1", name);

                
    //从缓存中移出key=1的项
                TestFlushCache.Remove("1");

                
    int i = TestFlushCache.Count;
                Assert.AreEqual(
    0,i);
            }

    5.Retrieving Items from the Cache
    此节介绍如何取的缓存中的某一项的值,需要注意返回值类型的强制转换。

        [TestMethod]
            
    public void GetDataFromCache()
            
    {
                
    string name = "SHY520";
                CacheManager TestFlushCache 
    = CacheFactory.GetCacheManager();
                TestFlushCache.Add(
    "1", name);

                
    //根据key值得到缓存中的某一项,但取值的时候注意类型的强制转换
                string rname = (string) TestFlushCache.GetData("1");

                Assert.AreEqual(name,rname);
            }


           关于Caching Application Block的入门部分就说这么多,希望和正在学习Enterprise Library 2.0 的朋友们多交流,并希望过来人不吝提出建议。

  • 相关阅读:
    截图与图片合成的几种方法
    GPUImage 自定义滤镜
    How do I solve the error: An error was encountered while running (Domain = LaunchServicesError, Code = 0) ?
    tableview 重用nib cell
    开发DZ插件教程
    QBImagePickerController 用法
    ALAsset和ALAssetRepresentation
    if exists和if not exists关键字用法
    Java socket 超时
    Android 图片的压缩
  • 原文地址:https://www.cnblogs.com/xianqingzh/p/1445674.html
Copyright © 2011-2022 走看看