Enterprise Library Cache(缓存程序块)笔记

1、引入命名空间

    using System.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Common;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Caching;
using Microsoft.Practices.EnterpriseLibrary.Caching.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Caching.Expirations;

2、实例化

    public static ICacheManager cacheManager = CacheFactory.GetCacheManager("Cache Manager");

    这里的Cache Manager是配置文件中的,配置文件如下:

    <configSections>

     <section name="cachingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Caching.Configuration.CacheManagerSettings, Microsoft.Practices.EnterpriseLibrary.Caching, Version=5.0.414.0, Culture=neutral, PublicKeyToken=null" requirePermission="true" />

    </configSections>

    <cachingConfiguration defaultCacheManager="Cache Manager">
    <cacheManagers>
      <add name="Cache Manager" type="Microsoft.Practices.EnterpriseLibrary.Caching.CacheManager, Microsoft.Practices.EnterpriseLibrary.Caching, Version=5.0.414.0, Culture=neutral, PublicKeyToken=null"
          expirationPollFrequencyInSeconds="60" maximumElementsInCacheBeforeScavenging="1000"
          numberToRemoveWhenScavenging="10" backingStoreName="NullBackingStore" />
      
    </cacheManagers>
    <backingStores>
      <add type="Microsoft.Practices.EnterpriseLibrary.Caching.BackingStoreImplementations.NullBackingStore, Microsoft.Practices.EnterpriseLibrary.Caching, Version=5.0.414.0, Culture=neutral, PublicKeyToken=null"
          name="NullBackingStore" />
    </backingStores>
  </cachingConfiguration>

3、实例,将一个20000行的DataTable缓存到内存中,每隔60秒更新一次。

   

protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string cacheKey = "CustomersDataTable";
                if (cacheManager.GetData(cacheKey) == null)
                {
                    DataTable customerData = returnDataTable();
                    cacheManager.Add(cacheKey, customerData,CacheItemPriority.Normal,null,new AbsoluteTime(TimeSpan.FromSeconds(60)));
                    gvTest.DataSource = customerData;
                    gvTest.DataBind();
                }
                else
                {
                    gvTest.DataSource = cacheManager.GetData("CustomersDataTable") as DataTable;
                    gvTest.DataBind();
                }

            }
        }
private DataTable returnDataTable()
        {
            Database db = DatabaseFactory.CreateDatabase("QuickStarts Instance");
            DataTable dt = db.ExecuteDataSet(CommandType.Text, "select  top 20000 * from Customers;").Tables[0];
            return dt;
        }


像这样缓存起来带来的的性能提升很明显。这里只是我自己一个小小的测试,至于Enterprise Library缓冲层的更多应用,可以查看博客:

http://www.cnblogs.com/Terrylee/archive/2005/11/13/275233.html

这里我贴出以上博客的几段精典,当是笔记吧,感激作者的技术分享,呵呵!

 Database db = DatabaseFactory.CreateDatabase("Database Instance");
           DataSet ds = db.ExecuteDataSet(CommandType.Text,"Select * from Products");
            
           ///创建CacheManager
            IsolatedCacheManager = CacheFactory.GetCacheManager("Isolated Cache Manager");
             
            ///创建一个单一的时间
            DateTime refreshTime = new DateTime(2005, 11, 12, 12, 51, 30);

            ///指定为绝对过期时间
            AbsoluteTime expireTime = new AbsoluteTime(refreshTime);
            
            ///添加缓冲项,优先级为Normal
           IsolatedCacheManager.Add("MyDataSet",ds, CacheItemPriority.Normal, null,expireTime);

用表达式来设置:

表达式的格式:<Minute> <Hour> <Day of month> <Month> <Day of week>

例子:

“* * * * *”       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 9:15 PM on 4 July

Database db = DatabaseFactory.CreateDatabase("Database Instance");
             DataSet ds = db.ExecuteDataSet(CommandType.Text,"Select * from Products");
          
            ///创建CacheManager
           IsolatedCacheManager = CacheFactory.GetCacheManager("Isolated Cache Manager");
            
           ///创建基于表达式
           ExtendedFormatTime expireTime = new ExtendedFormatTime("0 0 * * 6");
                      ///添加缓冲项,优先级为Normal,过期时间
           IsolatedCacheManager.Add("Key1", "Cache Item1", CacheItemPriority.Normal,null, expireTime);

 

 

 

 

你可能感兴趣的:(library)