zoukankan      html  css  js  c++  java
  • 微软企业库4.1学习笔记(十五)缓存模块3 使用数据库作为后端存储

    配置缓存模块使用数据库作为后端存储 

    1) CacheManager节点上点击右键,选择新建【Database Cache Storage

     

     

    2)在配置中自动加入数据访问模块的配置信息

     

    3)点击Data Cache Storage节点,右侧的DatabaseInstance属性显示的是数据库连接字符串的名称,它和数据访问配置中的一个配置关联的,

    你可以通过输入或者下来来选择一个数据访问配置。可以重新命名Name属性。

     

    4)设置PartitionName属性,这个标识在CacheManager中将会使用。

    通过在CacheManager上的右键菜单,可以选择新建其他类型的后端存储。

    注意点:缓存配置的属性值应该考虑缓存使用的模式,和系统的环境,例如,内存的容量。如果大量使用缓存,是很消耗内存的,会导致内存紧张。

    可以通过模块计数器来帮助调整缓存的配置参数值。

    缓存配置的xml结构

    下面介绍缓存配置的xml文件结构,你可以手动修改xml文件,但是企业库的配置工具可以大大减少这些配置工作。

    <configuration>

        <configSections>

            <section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />

            <section name="cachingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Caching.Configuration.CacheManagerSettings, Microsoft.Practices.EnterpriseLibrary.Caching, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />

        </configSections>

        <dataConfiguration defaultDatabase="Connection String" />

        <connectionStrings>

            <add name="Connection String" connectionString="Database=Database;Server=(local)\SQLEXPRESS;Integrated Security=SSPI"

                providerName="System.Data.SqlClient" />

        </connectionStrings>

        <cachingConfiguration defaultCacheManager="MyCacheManager1">

            <cacheManagers>

                <add expirationPollFrequencyInSeconds="60" maximumElementsInCacheBeforeScavenging="1000"

                    numberToRemoveWhenScavenging="10" backingStoreName="Null Storage"

                    type="Microsoft.Practices.EnterpriseLibrary.Caching.CacheManager, Microsoft.Practices.EnterpriseLibrary.Caching, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"

                    name="Cache Manager" />

                <add expirationPollFrequencyInSeconds="60" maximumElementsInCacheBeforeScavenging="1000"

                    numberToRemoveWhenScavenging="10" backingStoreName="Data Cache Storage"

                    type="Microsoft.Practices.EnterpriseLibrary.Caching.CacheManager, Microsoft.Practices.EnterpriseLibrary.Caching, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"

                    name="MyCacheManager1" />

            </cacheManagers>

            <backingStores>

                <add databaseInstanceName="Connection String" partitionName="CacheManager1"

                    encryptionProviderName="" type="Microsoft.Practices.EnterpriseLibrary.Caching.Database.DataBackingStore, Microsoft.Practices.EnterpriseLibrary.Caching.Database, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"

                    name="Data Cache Storage" />

                <add encryptionProviderName="" type="Microsoft.Practices.EnterpriseLibrary.Caching.BackingStoreImplementations.NullBackingStore, Microsoft.Practices.EnterpriseLibrary.Caching, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"

                    name="Null Storage" />

            </backingStores>

        </cachingConfiguration>

    </configuration>

    在应用中使用缓存模块

    准备环境:

    1、 添加对Microsoft.Practices.EnterpriseLibrary.Caching.dll程序集的引用

    2、 添加对Microsoft.Practices.EnterpriseLibrary.Common.dll程序集的引用,里面包含了企业库的核心内容,一些通用的功能

    3、 添加对Microsoft.Practices.ObjectBuilder2.dll程序集的引用

    4、 如果使用数据库作为后端存储,需要添加对Microsoft.Practices.EnterpriseLibrary.Data.dll

    Microsoft.Practices.EnterpriseLibrary.Caching.Database.dll程序集的引用

    5、 如果需要加密缓存中的数据,需要添加对Microsoft.Practices.EnterpriseLibrary.Caching.Cryptography.dllMicrosoft.Practices.EnterpriseLibrary.Security.Cryptography.dll程序集的引用

    6、 然后再代码中导入下面的命名空间

    using Microsoft.Practices.EnterpriseLibrary.Common;

    using Microsoft.Practices.EnterpriseLibrary.Caching.Expirations;

    using Microsoft.Practices.EnterpriseLibrary.Caching;

    通常,用两步就可以开始用缓存功能了。

    1) 创建CacheManager

    2) 使用CacheManager中对应的方法

    /*

     * Created by SharpDevelop.

     * User: haier

     * Date: 2010-4-21

     * Time: 22:33

     *

     * To change this template use Tools | Options | Coding | Edit Standard Headers.

     */

    using System;

    using Microsoft.Practices.EnterpriseLibrary.Common;

    using Microsoft.Practices.EnterpriseLibrary.Caching.Expirations;

    using Microsoft.Practices.EnterpriseLibrary.Caching;

    namespace BeautyCode.ConApp

    {

        public class Goods

        {

           public string ID{get;set;}

           public string Name{get;set;}

           public int Price{get;set;}

        }

        public class CachingTest

        {

           public static void Test()

           {

           ICacheManager goodsCache=CacheFactory.GetCacheManager();

           string id="001";

           string name="seed";

           int price=100;

           Goods goods=new Goods();

           goods.ID =id;

           goods .Name=name;

           goods .Price=price ;

           goodsCache .Add(goods .ID,goods ,CacheItemPriority .Normal ,

                            null,new SlidingTime (TimeSpan.FromMinutes (5)));

          

           //Retrieve the item

           goods =(Goods )goodsCache .GetData (id );

           }

        }

    }

    上面的代码演示了,用工厂方法创建CacheManager,然后调用Add方法添加数据到缓存,使用GetData方法从缓存中获取数据,

    获取之后别忘了进行类型转换。

    选择后端存储

    每个CacheManager都可以配置为只在内存中存储数据,意味着它不使用任何后端存储,也可以配置为及使用内存,也使用持久化的存储。

    持久化的形式在配置后端存储的时候指定。后端存储保证了,如果应用必须要重启的话,缓存的数据还可以存活,也就是重启之后缓存的数据不会丢失。

    默认情况下,企业库的缓存模块支持两种形式的后端存储。

    1) 独立的后端存储

    2) 数据库的后端存储

    如果你需要在多个服务器的情况下使用缓存,例如服务器场环境,后面我们会讲到。

    开发者可以扩展缓存模块,来支持额外形式的后端存储。

    一个应用可以使用多个缓存,每个缓存通过一个配置的CacheManager作为代理。企业库的缓存模块不支持多个CacheManager使用同一个后端存储。

    但是一个应用的多个CacheManager可以使用相同的partition name

    1、使用空后端存储

    空后端存储是默认的选项,它不持久化缓存的数据。意味着数据只存在于内存中,这种形式适用于你想在重启应用之后更新缓存中的数据。

    它被所有的应用支持。

    2、使用独立存储作为后端存储

    适用于下面的情况

    1) 需要持久化存储,但是用户量较少

    2) 使用数据库会超出经费预算

    3) 没有数据库设施

    3、使用数据库作为后端存储

    使用数据访问模块,可以将缓存的数据放在数据库中。默认情况,缓存模块包含一个创建缓存需要的数据表结构的脚本,支持SQL Server数据库。

    开发者可以使用其它的数据库作为后端存储,但是需要修改模块的源代码。每一个数据库类型,必须在数据访问模块中包含一个数据库provider,和一个兼容的表结构。

     

      未完待续。。。。。。。。。。。。。。。。。。。。。

  • 相关阅读:
    C. Shaass and Lights 解析(思維、組合)
    D. Binary String To Subsequences(队列)(贪心)
    CodeForces 1384B2. Koa and the Beach (Hard Version)(贪心)
    CodeForces 1384B1. Koa and the Beach (Easy Version)(搜索)
    CodeForces 1384C. String Transformation 1(贪心)(并查集)
    CodeForces 1384A. Common Prefixes
    POJ-2516 Minimum Cost(最小费用最大流)
    POJ3261-Milk Patterns(后缀数组)
    HDU-1300 Pearls(斜率DP)
    HDU-4528 小明系列故事-捉迷藏(BFS)
  • 原文地址:https://www.cnblogs.com/lmule/p/1801088.html
Copyright © 2011-2022 走看看