關於WnForm/Web如何使用緩衝Cach

來源:互聯網
上載者:User

參考文章   http://kb.cnblogs.com/page/69483/

              http://www.cnblogs.com/zgx/archive/2009/03/16/1413643.html

              http://www.cnblogs.com/zhangxp1129/archive/2012/09/05/2671522.html

 

 

MSDN:   http://msdn.microsoft.com/zh-cn/magazine/system.web.caching.cache(VS.85).aspx

對於每個應用程式定義域均建立該類的一個執行個體,並且只要對應的應用程式定義域保持活動,該執行個體便保持有效。

注意:

Cache 類不能在 ASP.NET 應用程式外使用。它是為在 ASP.NET 中用於為 Web 應用程式提供緩衝而設計和測試的。在其他類型的應用程式(如控制台應用程式或 Windows 表單應用程式)中,ASP.NET 緩衝可能無法正常工作。

 

System.Web.Caching是用來管理緩衝的命名空間,其父級空間是System.Web,由此可見,緩衝通常用於Web網站的開發,包括在B/S項目中的開發。

緩衝的設計主要是考慮到網路頻寬可能會延緩資料的提交與回傳,如果把資料儲存在用戶端,使用者就可以直接從用戶端讀取資料,減少用戶端與伺服器端的資料互動,提高程式的效能。

 

 

那麼System.Web.Caching可以使用到WinForm程式中嗎?

如果用的是winform,基本上不用想這個問題,因為你的程式本身就在記憶體裡運行著。winfrom 直接用記憶體用 資料字典如果是

web,緩衝就是將常用的資料放到伺服器的記憶體中,當有不同的客戶請求相同的資料時,直接從記憶體讀取,以此提高效能。

 

簡單點:WebForm是“瘦用戶端”,佔用伺服器資源。WinForm是“胖客戶單”,佔用的是本地用戶端記憶體。

推薦兩種寫法:

 

一是web項目中如何使用。

View Code /**************************************
* 作用:緩衝相關的操作
* 作者:* 日期: 2008-02-11
* 網址:
**************************************/
using System;
using System.Web;
using System.Collections.Generic;
using System.Text;

namespace RedGlove.Lib
{
    public class DataCache
    {
        /// <summary>
        /// 擷取當前應用程式指定CacheKey的Cache值
        /// </summary>
        /// <param name="CacheKey"></param>
        /// <returns></returns>
        public static object GetCache(string CacheKey)
        {
            System.Web.Caching.Cache objCache = HttpRuntime.Cache;
            return objCache[CacheKey];

        }

        /// <summary>
        /// 設定當前應用程式指定CacheKey的Cache值
        /// </summary>
        /// <param name="CacheKey"></param>
        /// <param name="objObject"></param>
        public static void SetCache(string CacheKey, object objObject)
        {
            System.Web.Caching.Cache objCache = HttpRuntime.Cache;
            objCache.Insert(CacheKey, objObject);
        }

        /// <summary>
        /// 設定當前應用程式指定CacheKey的Cache值
        /// </summary>
        /// <param name="CacheKey"></param>
        /// <param name="objObject"></param>
        public static void SetCache(string CacheKey, object objObject, DateTime absoluteExpiration, TimeSpan slidingExpiration)
        {
            System.Web.Caching.Cache objCache = HttpRuntime.Cache;
            objCache.Insert(CacheKey, objObject, null, absoluteExpiration, slidingExpiration);
        }
    }
}

 

二是winform程式中

 

View Code namespace HZ
{
    using System.Collections.Generic;

    /// <summary>
    /// 全域統一的緩衝類
    /// </summary>
    public class Cache
    {
        private SortedDictionary<string, string> dic = new SortedDictionary<string, string>();
        private static volatile Cache instance = null;
        private static object lockHelper = new object();

        private Cache()
        {

        }
        public void Add(string key, string value)
        {
            dic.Add(key, value);
        }
        public void Remove(string key)
        {
            dic.Remove(key);
        }

        public string this[string index]
        {
            get
            {
                if (dic.ContainsKey(index))
                    return dic[index];
                else
                    return null;
            }
            set { dic[index] = value; }
        }

        public static Cache Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (lockHelper)
                    {
                        if (instance == null)
                        {
                            instance = new Cache();
                        }
                    }
                }
                return instance;
            }
        }
    }
}

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.