C#中緩存的使用


緩存的概念及優缺點在這里就不多做介紹,主要介紹一下使用的方法。

1.在ASP.NET中頁面緩存的使用方法簡單,只需要在aspx頁的頂部加上一句聲明即可:

   <%@ OutputCache Duration="100" VaryByParam="none" %>

   Duration:緩存時間(秒為單位),必填屬性

2.使用微軟自帶的類庫System.Web.Caching

新手接觸的話不建議直接使用微軟提供的類庫,因為這樣對理解不夠深刻。所以在這里我帶大家自己寫一套緩存操作方法,這樣理解得更加清晰。

話不多說,代碼開敲。

 

一、首先,先模擬數據來源。新建一個類,寫一個數據操作方法(該方法耗時、耗資源)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Cache
{
    public class DataSource
    {
        /// <summary>
        /// 模擬從數據庫讀取數據
        /// 耗時、耗CPU
        /// </summary>
        /// <param name="count"></param>
        public static int GetDataByDB(int count)
        {
            Console.WriteLine("-------GetDataByDB-------");
            int result = 0;
            for (int i = count; i < 99999999; i++)
            {
                result += i;
            }
            Thread.Sleep(2000);
            return result;
        }
    }
}

 二、編寫一個緩存操作類

  2.1 構造一個字典型容器,用於存放緩存數據,權限設為private ,防止隨意訪問造成數據不安全性

        //緩存容器 
        private static Dictionary<string, object> CacheDictionary = new Dictionary<string, object>();

       2.2 構造三個方法(添加數據至緩存容器、從緩存容器獲取數據、判斷緩存是否存在)

        /// <summary>
        /// 添加緩存
        /// </summary>
        public static void Add(string key, object value)
        {
            CacheDictionary.Add(key, value);
        }

        /// <summary>
        /// 獲取緩存
        /// </summary>
        public static T Get<T>(string key)
        {
            return (T)CacheDictionary[key];
        }

        /// <summary>
        /// 判斷緩存是否存在
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static bool Exsits(string key)
        {
            return CacheDictionary.ContainsKey(key);
        }

三、程序入口編寫測試方法

      3.1 先看一下普通情況不適用緩存,它的執行效率有多慢

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Cache
{
    class Program
    {
        static void Main(string[] args)
        {
            for (int i = 1; i < 6; i++)
            {
                Console.WriteLine($"------第{i}次請求------");
                int result = DataSource.GetDataByDB(666);
                Console.WriteLine($"第{i}次請求獲得的數據為:{result}");
            }
        }
    }
}

      3.2 接下來,我們編寫緩存試用方法。概念無非就是根據key前往字典容器里查找是否有相對應緩存數據,有則直接調用,沒有則生成並存入字典容器里。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Cache
{
    class Program
    {
        static void Main(string[] args)
        {
            for (int i = 1; i < 6; i++)
            {
                Console.WriteLine($"------第{i}次請求------");
                //int result = DataSource.GetDataByDB(666);
                int result = 0;
                //key的名字一定要確保請求的准確性 DataSource GetDataByDB 666缺一不可
                string key = "DataSource_GetDataByDB_666";
                if (CacheHelper.Exsits(key))
                {
                    //緩存存在,直接獲取原數據
                    result = CacheHelper.Get<int>(key);
                }
                else
                {
                    //緩存不存在,去生成緩存,並加入容器
                    result = DataSource.GetDataByDB(666);
                    CacheHelper.Add(key, result);
                }
                Console.WriteLine($"第{i}次請求獲得的數據為:{result}");
            }
        }
    }
}

      3.3 我們看看加入緩存之后的效率如何

四、可以看到,瞬間完成。事已至此,緩存的使用基本是完成了。但是回過頭來我們想想看。一個系統成百上千個地方使用緩存的話,那豈不是要寫成百上千個if else判斷緩存是否存在,然后獲取?

       答案顯而易見,肯定不合理的。所以我們要對代碼進行優化。

       4.1 緩存操作類(CacheHelper)編寫一個通用的獲取方法

        /// <summary>
        /// 緩存獲取方法
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key">緩存字典容器對應key</param>
        /// <param name="func">委托方法 傳入操作對象</param>
        /// <returns></returns>
        public static T GetCache<T>(string key, Func<T> func)
        {
            T t = default(T);
            if (CacheHelper.Exsits(key))
            {
                //緩存存在,直接獲取原數據
                t = CacheHelper.Get<T>(key);
            }
            else
            {
                //緩存不存在,去生成緩存,並加入容器
                t = func.Invoke();
                CacheHelper.Add(key, t);
            }
            return t;
        }

 

       4.2 程序入口進行調用,傳入的委托參數為lamad表達式優化后的代碼

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Cache
{
    class Program
    {
        static void Main(string[] args)
        {
            for (int i = 1; i < 6; i++)
            {
                Console.WriteLine($"------第{i}次請求------");
                int result = 0;
                //key的名字一定要確保請求的准確性 DataSource GetDataByDB 666缺一不可
                string key = "DataSource_GetDataByDB_666";

                //將需要執行的獲取數據操作編寫成委托傳入方法(重點)
                //Func<int> func = new Func<int>(() => { return DataSource.GetDataByDB(666); });

                result = CacheHelper.GetCache(key, () => DataSource.GetDataByDB(666));
                Console.WriteLine($"第{i}次請求獲得的數據為:{result}");
            }
        }
    }
}

 

到這里,緩存的使用基本結束了。最好值得一提的是,緩存盡量在數據量小、重復查詢量大的情況下使用。因為緩存也是要耗內存的,服務器內存是有限的!

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM