Enterprise Library 缓存应用程序块快速入门

Enterprise Library 快速入门是简单的、易于理解的应用程序块关键特性的示例,使用了一个实现了常规场景的漫游集合来说明这些特性。

如果要理解一个应用程序块,快速入门将是理想的起始点,并且用试验源代码来学习新的技术也是非常舒服的。如果已对 .NET 框架比较熟悉,以及如果要查看简单的、有助于理解如何解决特定问题的代码示例的话,它们将是非常好的资源。

要使用快速入门的所有优点的话,将需要熟悉面向对象编程相关的概念和技术。

缓存快速入门

快速入门应用程序示范了缓存应用程序块的一些关键特性。它使用了一个漫游集合来示范这些特性。漫游是在“关键场景”中讨论的情况的实现,如下:

  • 漫游:添加条目到缓存中

  • 漫游:从缓存中移除条目

  • 漫游:从缓存中获取条目

  • 漫游:清除缓存

  • 漫游:加载缓存

快速入门使用了一个顶层的处理程序来捕获任何场景中的任何异常。处理程序显示了一个带有异常信息的对话框。

系统需求

要构建并运行快速入门,你将需要下列软件:

  • Microsoft Windows 2000, Windows XP Professional, 或者 Windows Server 2003 操作系统

  • Microsoft .NET Framework 2.0

  • Microsoft Visual Studio 2005 开发系统

快速入门在构建和运行应用程序之前不需要执行任何安装步骤。

注意

默认的快速入门配置不使用持久后端存储。

构建并运行快速入门

快速入门以源代码的形式发布,这意味着在运行之前必须编译它,可以使用 Visual Studio 来构建快速入门。

构建缓存快速入门

  1. 确认 Enterprise Library 源代码已安装

  2. 从 Windows 资源管理器中打开 Enterprise Library 源码文件夹,或者用开始菜单快捷方式打开:在任务栏中,单击 开始 ,指向 程序 ,指向 Microsoft patterns and practices ,指向 Enterprise Library 3.1 – May 2007 ,然后选择 Enterprise Library 3.1 Source Folder

  3. 打开 QuickStarts 文件夹,然后是 Cache ,然后是 CS (用于 C#)或者 VB (用于 Visual Basic .NET)。

  4. 双击 CachingQuickStart.sln 图标。

  5. Visual Studio 打开、显示解决方案文件。在菜单中,单击 生成

  6. 单击 重新生成解决方案 。默认情况下,这是一个 debug 构建。

  7. 按下 F5 运行快速入门。

快速入门配置

快速入门配置信息放置在快速入门项目的文件夹中,它具有下列属性:

  • 用于缓存操作节的缓存管理器被命名为“Default Cache Manager”。用于主动和被动加载缓存的节的缓存管理器被命名为“Loading Scenario Cache Manager”。

  • 数据仅写在内存中(而不是后端存储)。

  • 每 60 秒发生一次到期周期。

  • 在缓存中有 1000 个条目时发生清理。

  • 从缓存中清理移除 10 个条目。

快速入门中的漫游带有定义好的配置,它被包括在 App.config 文件中。这个文件放置在快速入门项目文件的文件夹中。

要修改或查看这些设置,使用 Enterprise Library 配置控制台打开包含快速入门项目文件目录中的 App.config 文件。App.config 包含了配置数据。

在每次构建代码时,Visual Studio 复制 App.config 文件到项目的输出目录中(创建的快速入门可执行文件的目录),并改名为 CachingQuickStart.exe.config 。

这意味着如果要使用配置控制台修改任何配置设置,如到期周期,并且计划重建解决方案,就必须打开快速入门源目录中的 App.config 文件来修改配置。可以用配置控制台打开 CachingQuickStart.exe.config 文件。然而,这些改变将在下次成功构建中被覆盖。

这意味着当使用配置控制台打开 App.config 文件并修改配置设置时,必须复制文件到输出目录。可以手工复制,也可以重建项目来完成此目的。记住,简单的修改配置设置没有必要重建代码。运行在成功构建之上的命令是为了方便构建。

漫游:添加条目到缓存

此漫游示范了如何添加条目到缓存中。

重建示例
  1. 配置缓存。必要的步骤,请参见输入配置信息。

  2. QuickStartForm 类中为 CacheManager 对象声明一个成员变量。

    例 7.21. C#

    private CacheManager primitivesCache;

    例 7.22. Visual Basic .NET

    Private primitivesCache As CacheManager

  3. QuickStart_Load 方法中,添加下列代码以创建 CacheManager 。对 GetCacheManager 的调用没有包含CacheManager 的名称,所以工厂创建了声明在配置文件中的默认 CacheManager 对象。

    例 7.23. C#

    this.primitivesCache = CacheFactory.GetCacheManager();

    例 7.24. Visual Basic .NET

    Me.primitivesCache = CacheFactory.GetCacheManager()

  4. 创建要添加到缓存中的条目。下列代码创建了一个 Product 类型的条目。

    例 7.25. C#

    string id="ProductOneId";
    string name = "ProductOneName";
    int price = 50;

    Product product = new Product(id, name, price);

    例 7.26. Visual Basic .NET

    Dim id As String = "ProductOneId"
    Dim name As String = "ProductOneName"
    Dim price As Integer = 50

    Dim newProduct As Product = New Product(id, name, price)

  5. 添加条目到缓存中。下列代码使用了 Add 方法的一个重载,重载包含清理优先级(在此为2),条目在到期时不刷新的指令、从条目的最后访问时间开始的5分钟的到期时间。

    例 7.27. C#

    primitivesCache.Add(product.ProductID, product, CacheItemPriority.Normal, null,
    new SlidingTime(TimeSpan.FromMinutes(5)));

    例 7.28. Visual Basic .NET

    primitivesCache.Add(newProduct.ProductID, newProduct, CacheItemPriority.Normal, Nothing, _
    New SlidingTime(TimeSpan.FromMinutes(5)))

漫游:从缓存中移除条目

此漫游示范了如何从缓存中移除条目。

重建此示例
  1. 配置缓存。必要的步骤,请参见缓存快速入门中的“快速入门配置”。

  2. QuickStartForm 类中为 CacheManager 对象声明一个成员变量。

    例 7.29. C#

    private CacheManager primitivesCache;

    例 7.30. Visual Basic .NET

    Private primitivesCache As CacheManager

  3. 在响应用户请求从缓存中移除条目的方法中,添加下列代码。

    例 7.31. C#

    // Prompt the user for the key of the item to be removed.
    if (this.selectItemForm.ShowDialog() == DialogResult.OK)
    {
    // Request that the item be removed from the cache.
    this.primitivesCache.Remove(selectItemForm.ItemKey);
    }

    例 7.32. Visual Basic .NET

    ' Prompt the user for the key of the item to be removed.
    If (Me.selectItemForm.ShowDialog() = DialogResult.OK) Then
    ' Request that the item be removed from the cache.
    Me.primitivesCache.Remove(selectItemForm.ItemKey)
    End If

漫游:从缓存中获取条目

此漫游示范了如何从缓存中获取条目。

重建示例
  1. 配置缓存。必要步骤,请参见缓存快速入门中的“快速入门配置”。

  2. QuickStartForm 类中为 CacheManager 对象声明一个成员变量。

    例 7.33. C#

    private CacheManager primitivesCache;

    例 7.34. Visual Basic .NET

    Private primitivesCache As CacheManager

  3. 在响应用户请求从缓存中读取条目的方法中,添加下列代码。

    例 7.35. C#

    // Prompt the user for the key of the item to be read.
    if (this.selectItemForm.ShowDialog() == DialogResult.OK)
    {
    // Read the item from the cache. If the item is not found in the cache, the
    // return value will be null.
    Product product = (Product) this.primitivesCache.GetData(selectItemForm.ItemKey);
    }

    例 7.36. Visual Basic .NET

    ' Prompt the user for the key of the item to be read.
    If (Me.selectItemForm.ShowDialog() = DialogResult.OK) Then
    ' Read the item from the cache. If the item is not found in the cache, the
    ' return value will be null.
    Dim requestedProduct As Product = DirectCast(Me.primitivesCache.GetData(selectItemForm.ItemKey), Product)
    End If

漫游:清除缓存

此漫游示范了如何清除缓存,清空缓存中的所有数据。

重建此示例
  1. 配置缓存。必要步骤,请参见缓存快速入门中的“快速入门配置”。

  2. QuickStartForm 类中为 CacheManager 对象声明一个成员变量。

    例 7.37. C#

    private CacheManager primitivesCache;

    例 7.38. Visual Basic .NET

    Private primitivesCache As CacheManager

  3. 在响应用户请求清空缓存的方法中,添加下列代码。

    例 7.39. C#

    this.primitivesCache.Flush();

    例 7.40. Visual Basic .NET

    Me.primitivesCache.Flush()
posted @ 2007-11-01 00:39  Dorian Deng  阅读(1209)  评论(0)    收藏  举报