C#中緩存的使用

緩存的概念及優缺點在這裏就很少作介紹,主要介紹一下使用的方法。數據庫

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

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

   Duration:緩存時間(秒爲單位),必填屬性服務器

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

新手接觸的話不建議直接使用微軟提供的類庫,由於這樣對理解不夠深入。因此在這裏我帶你們本身寫一套緩存操做方法,這樣理解得更加清晰。優化

話很少說,代碼開敲。spa

 

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

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、編寫一個緩存操做類對象

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

        //緩存容器 
        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、程序入口編寫測試方法

      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 咱們看看加入緩存以後的效率如何

4、能夠看到,瞬間完成。事已至此,緩存的使用基本是完成了。可是回過頭來咱們想一想看。一個系統成百上千個地方使用緩存的話,那豈不是要寫成百上千個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}");
            }
        }
    }
}

 

到這裏,緩存的使用基本結束了。最好值得一提的是,緩存儘可能在數據量小、重複查詢量大的狀況下使用。由於緩存也是要耗內存的,服務器內存是有限的!

相關文章
相關標籤/搜索