• 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);
  • 相关阅读:
    vue从入门到进阶:自定义指令directive,插件的封装以及混合mixins(七)
    js模板引擎mustache介绍及实例
    vue v-cloak 的作用和用法
    vue中$event理解和框架中在包含默认值外传参
    Node.js如何设置允许跨域
    前端常见跨域解决方案(全)
    http-server使用教程 hs -o
    JMeter性能测试,完整入门篇
    Java源码初学_LinkedHashMap
    Java源码初学_HashMap
  • 原文地址:https://www.cnblogs.com/msql/p/2803566.html
Copyright © 2020-2023  润新知