程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> Enterprise Library for .NET Framework 2.0緩存應用實例

Enterprise Library for .NET Framework 2.0緩存應用實例

編輯:C#入門知識

Enterprise Library for .NET Framework 2.0緩存應用實例。本站提示廣大學習愛好者:(Enterprise Library for .NET Framework 2.0緩存應用實例)文章只能為提供參考,不一定能成為您想要的結果。以下是Enterprise Library for .NET Framework 2.0緩存應用實例正文


Enterprise Library for .NET Framework 2.0 是微軟宣布的企業庫,它支撐.NET Framework 2.0。而且由一系列的企業運用法式塊來組成。本文即以實例展現了Enterprise Library for .NET Framework 2.0緩存的應用辦法,供年夜家參考。

症結代碼以下:

using Microsoft.Practices.EnterpriseLibrary.Caching;
using Microsoft.Practices.EnterpriseLibrary.Caching.Expirations;
using System;

namespace ETLUtilHelpV2
{
  /// <summary>
  /// Enterprise Library for .NET Framework 2.0 緩存對象類
  /// </summary>
  public class ETLCacheToolV2
  {
    /*
     *在Caching Application Block中,重要供給以下四種保留緩存數據的門路,
     *分離是:內存存儲(默許)、自力存儲(Isolated Storage)、
     *數據庫存儲(DataBase Cache Storage)和自界說存儲(Custom Cache Storage)。
     *In-Memory:保留在內存中。  
     *Isolated Storage Cache Store:體系將緩存的信息保留在自力文件中(C:\Users\<<user name>>\AppData\Local\IsolatedStorage)。
     *Data Cache Storage:將緩存數據保留在數據庫中。(須要運轉CreateCachingDatabase.sql劇本)
     *Custom Cache Storage:本身擴大的處置器。我們可以將數據保留在注冊表中或文本文件中。
     *
     * 緩存品級,在企業庫的緩存模塊中曾經供給了4個緩存品級:Low,Normal,High和NotRemovable,在超越最年夜緩存數目後會主動依據緩存品級來移除對象。
     * 過時方法,企業庫默許供給4種過時方法
     * AbsoluteTime:相對是時光過時,傳遞一個時光對象指定到時過時
     * SlidingTime:緩存在最初一次拜訪以後若干時光後過時,默許為2分鐘,有2個結構函數可以指定一個過時時光或指定一個過時時光和一個最初應用時
     * ExtendedFormatTime :指定過時格局,以特定的格局來過時,經由過程ExtendedFormat.cs類來包裝過時方法,詳細可參照ExtendedFormat.cs,源代碼中曾經給出了許多方法
     * FileDependency:依附於文件過時,當所依附的文件被修正則過時,這個我認為很有效,由於在很多網站,如服裝論壇t.vhao.net、消息體系等都須要年夜量的設置裝備擺設,可以將設置裝備擺設文件信息停止緩存,將依附項設為設置裝備擺設文件,如許當用戶更改了設置裝備擺設文件後經由過程ICacheItemRefreshAction.Refresh可以主動從新緩存。
     */

    ///// <summary>
    ///// 自界說緩存刷新操作
    ///// </summary>
    //[Serializable]
    //public class CacheItemRefreshAction : ICacheItemRefreshAction
    //{
    //  #region ICacheItemRefreshAction 成員
    //  /// <summary>
    //  /// 自界說刷新操作
    //  /// </summary>
    //  /// <param name="removedKey">移除的鍵</param>
    //  /// <param name="expiredValue">過時的值</param>
    //  /// <param name="removalReason">移除來由</param>
    //  void ICacheItemRefreshAction.Refresh(string removedKey, object expiredValue, CacheItemRemovedReason removalReason)
    //  {
    //    if (removalReason == CacheItemRemovedReason.Expired)
    //    {
    //      CacheManager cache = CacheFactory.GetCacheManager();
    //      cache.Add(removedKey, expiredValue);
    //    }
    //  }
    //  #endregion
    //}

    static CacheManager CacheMgr = null;
    static ETLCacheToolV2()
    {
      CacheMgr = CacheFactory.GetCacheManager();
    }
    /// <summary>
    /// 獲得CacheManager實例
    /// </summary>
    /// <returns>CacheManager</returns>
    public static CacheManager Instance()
    {
      return CacheMgr;
    }
    /// <summary>
    /// 添加緩存
    /// </summary>
    /// <param name="key">鍵</param>
    /// <param name="value">值</param>
    public static void Add(string key, object value)
    {
      CacheMgr.Add(key, value);
    }
    /// <summary>
    /// 添加緩存_滑動過時_小時
    /// </summary>
    /// <param name="key">鍵</param>
    /// <param name="value">值</param>
    /// <param name="hour">小時</param>
    public static void AddWithHour(string key, object value, int hour)
    {
      CacheMgr.Add(key, value, CacheItemPriority.Normal, null, new SlidingTime(TimeSpan.FromHours(hour)));
    }
    /// <summary>
    /// 添加緩存_滑動過時_天
    /// </summary>
    /// <param name="key">鍵</param>
    /// <param name="value">值</param>
    /// <param name="days">天</param>
    public static void AddWithDay(string key, object value, int days)
    {
      CacheMgr.Add(key, value, CacheItemPriority.Normal, null, new SlidingTime(TimeSpan.FromDays(days)));
    }
    /// <summary>
    /// 添加緩存_滑動過時_毫秒
    /// </summary>
    /// <param name="key">鍵</param>
    /// <param name="value">值</param>
    /// <param name="millisecond">毫秒</param>
    public static void AddWithMillisecond(string key, object value, int millisecond)
    {
      CacheMgr.Add(key, value, CacheItemPriority.Normal, null, new SlidingTime(TimeSpan.FromMilliseconds(millisecond)));
    }
    /// <summary>
    ///添加緩存_滑動過時_分鐘
    /// </summary>
    /// <param name="key">鍵</param>
    /// <param name="value">值</param>
    /// <param name="minutes">分鐘</param>
    public static void AddWithMinutes(string key, object value, int minutes)
    {
      CacheMgr.Add(key, value, CacheItemPriority.Normal, null, new SlidingTime(TimeSpan.FromMinutes(minutes)));
    }
    /// <summary>
    ///添加緩存_滑動過時_秒
    /// </summary>
    /// <param name="key">鍵</param>
    /// <param name="value">值</param>
    /// <param name="seconds">秒</param>
    public static void AddWithSeconds(string key, object value, int seconds)
    {
      CacheMgr.Add(key, value, CacheItemPriority.Normal, null, new SlidingTime(TimeSpan.FromSeconds(seconds)));
    }
    /// <summary>
    /// 添加緩存_滑動過時_文件依附
    /// </summary>
    /// <param name="key">鍵</param>
    /// <param name="value">值</param>
    /// <param name="filePath">文件途徑</param>
    public static void AddFileDependency(string key, object value, string filePath)
    {
      FileDependency _fileDependency = new FileDependency(filePath);
      CacheMgr.Add(key, value, CacheItemPriority.Normal, null, _fileDependency);
    }
    /// <summary>
    /// 添加緩存_滑動過時_小時
    /// </summary>
    /// <param name="key">鍵</param>
    /// <param name="value">值</param>
    /// <param name="hour">小時</param>
    /// <param name="refreshAction">ICacheItemRefreshAction</param>
    public static void AddWithHour(string key, object value, int hour, ICacheItemRefreshAction refreshAction)
    {
      CacheMgr.Add(key, value, CacheItemPriority.Normal, refreshAction, new SlidingTime(TimeSpan.FromHours(hour)));
    }
    /// <summary>
    /// 添加緩存_滑動過時_天
    /// </summary>
    /// <param name="key">鍵</param>
    /// <param name="value">值</param>
    /// <param name="days">天</param>
    /// <param name="refreshAction">ICacheItemRefreshAction</param>
    public static void AddWithDay(string key, object value, int days, ICacheItemRefreshAction refreshAction)
    {
      CacheMgr.Add(key, value, CacheItemPriority.Normal, refreshAction, new SlidingTime(TimeSpan.FromDays(days)));
    }
    /// <summary>
    /// 添加緩存_滑動過時_毫秒
    /// </summary>
    /// <param name="key">鍵</param>
    /// <param name="value">值</param>
    /// <param name="millisecond">毫秒</param>
    /// <param name="refreshAction">ICacheItemRefreshAction</param>
    public static void AddWithMillisecond(string key, object value, int millisecond, ICacheItemRefreshAction refreshAction)
    {
      CacheMgr.Add(key, value, CacheItemPriority.Normal, refreshAction, new SlidingTime(TimeSpan.FromMilliseconds(millisecond)));
    }
    /// <summary>
    /// 添加緩存_滑動過時_分鐘
    /// </summary>
    /// <param name="key">鍵</param>
    /// <param name="value">值</param>
    /// <param name="minutes">分鐘</param>
    /// <param name="refreshAction">ICacheItemRefreshAction</param>
    public static void AddWithMinutes(string key, object value, int minutes, ICacheItemRefreshAction refreshAction)
    {
      CacheMgr.Add(key, value, CacheItemPriority.Normal, refreshAction, new SlidingTime(TimeSpan.FromMinutes(minutes)));
    }
    /// <summary>
    /// 添加緩存_滑動過時_秒
    /// </summary>
    /// <param name="key">鍵</param>
    /// <param name="value">值</param>
    /// <param name="seconds">秒</param>
    /// <param name="refreshAction">ICacheItemRefreshAction</param>
    public static void AddWithSeconds(string key, object value, int seconds, ICacheItemRefreshAction refreshAction)
    {
      CacheMgr.Add(key, value, CacheItemPriority.Normal, refreshAction, new SlidingTime(TimeSpan.FromSeconds(seconds)));
    }
    /// <summary>
    /// 添加緩存_滑動過時_文件依附
    /// </summary>
    /// <param name="key">鍵</param>
    /// <param name="value">值</param>
    /// <param name="filePath">文件途徑</param>
    /// <param name="refreshAction">ICacheItemRefreshAction</param>
    public static void AddFileDependency(string key, object value, string filePath, ICacheItemRefreshAction refreshAction)
    {
      FileDependency _fileDependency = new FileDependency(filePath);
      CacheMgr.Add(key, value, CacheItemPriority.Normal, refreshAction, _fileDependency);
    }
    /// <summary>
    /// 清空緩存
    /// </summary>
    public static void Flush()
    {
      CacheMgr.Flush();
    }
    /// <summary>
    /// 移出緩存
    /// </summary>
    /// <param name="key"></param>
    public static void Remove(string key)
    {
      if (CacheMgr.Contains(key))
        CacheMgr.Remove(key);
    }
    /// <summary>
    /// 獲得緩存
    /// </summary>
    /// <param name="key">鍵</param>
    /// <returns>值</returns>
    public static object GetData(string key)
    {
      if (CacheMgr.Contains(key))
        return CacheMgr.GetData(key);
      return null;
    }
    /// <summary>
    /// 獲得緩存
    /// </summary>
    /// <typeparam name="T">泛型</typeparam>
    /// <param name="key">鍵</param>
    /// <returns>值</returns>
    public static T GetData<T>(string key)
    {
      if (CacheMgr.Contains(key))
        return (T)CacheMgr.GetData(key);
      return default(T);
    }
  }
}

讀者可在本身項目中對上述代碼加以測試,信任會對年夜家的C#法式設計起到必定的贊助感化。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved