使用Microsoft Application Block之Cache Application

來源:互聯網
上載者:User

關於caching application block可以從http://www.microsoft.com/resources/practices/default.mspx下載

這裡我們介紹一下簡單的使用

1.建立一個windows form的測試程式

textbox用來顯示cache的內容,button用來擷取cache項.程式不能在簡單了哦.

2.添加Cache的引用(在block的安裝目錄,直接copy過來):

Microsoft.ApplicationBlocks.Cache.dll
Microsoft.ApplicationBlocks.Data.dll
Microsoft.ApplicationBlocks.ExceptionManagement.dll
Microsoft.ApplicationBlocks.ExceptionManagement.Interfaces.dll
MemoryMappedFileStream.dll

3.配置config檔案

因為是win form,所以添加一個app.config,如果是web引用,則對應web.config檔案.

預設的檔案樣本:C:\Program Files\Microsoft Application Blocks for .NET\Caching Framework\Code\CS\Solution Items

這裡我們直接copy app.config的內容吧.

好了,所有部署就完成了,下面來看看code

 

using System.Data;
using Microsoft.ApplicationBlocks.Cache;

namespace WinApp_Block_Cache
{
    /**//// <summary>
    /// Form1 的摘要說明。
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.TextBox textBox1;

        private void button1_Click(object sender, System.EventArgs e)
        {
            this.textBox1.Text = (string)GetCachedItem("hello");    
        }
        private object GetCachedItem(string itemName)
        {
            //擷取Cache管理的singlton
            CacheManager cacheMgr = CacheManager.GetCacheManager();
            object obj = cacheMgr.GetData(itemName);
            if(obj == null)
            {
                //下面是ExtendedFormatTime設定釋義
                /**//// <remarks>
                /// Extended format sintax : <br/><br/>
                /// 
                /// Minute       - 0-59 <br/>
                /// Hour         - 0-23 <br/>
                /// Day of month - 1-31 <br/>
                /// Month        - 1-12 <br/>
                /// Day of week  - 0-7 (Sunday is 0 or 7) <br/>
                /// Wildcards    - * means run every <br/>
                /// Examples: <br/>
                /// * * * * *    - expire every minute of every day <br/>
                /// 5 * * * *    - expire 5th minute of every hour or every minute
                ///                   after 1 hour from the last used time, whichever
                ///                   is earlier <br/>
                /// * 21 * * *   - expire every minute of the 21st hour of every day 
                ///                   or one hour after every day<br/>
                /// 31 15 * * *  - expire 3:31 PM every day <br/>
                /// 7 4 * * 6    - expire Saturday 4:07 AM <br/>
                /// 15 21 4 7 *  - expire 9:15 PM on 4 July <br/>
                /// </remarks>
                Microsoft.ApplicationBlocks.Cache.ICacheItemExpiration[] exps = 
                    {
                        //設定cache到期依賴,這裡設定1分鐘後到期.
                        //你可以設定多個到期依賴項,該block提供如下四種到期依賴:
                        //AbsoluteTime
                        //ExtendedFormatTime
                        //FileDependency
                        //SlidingTime

                        new Microsoft.ApplicationBlocks.Cache.Expirations.ExtendedFormatTime("* * * * *")
                    };
                obj = "hello world!!" + DateTime.Now.Millisecond.ToString();

                //如果不想對cache到期事件處理,可把callback設為null
                //這裡我們用CacheExpiredProcess來處理cache的到期
                cacheMgr.Add(itemName,
                    obj,
                    exps,
                    CacheItemPriority.High,
                    new CacheItemRemovedCallback(CacheExpiredProcess));
            }
            return obj;
        }
        //設定了一個處理到期事件函數
        private void CacheExpiredProcess(string cacheKey,CacheItemRemoveCause cause)
        {
            MessageBox.Show(cacheKey + " is expired!");
        }

    }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.