C#中緩存的基本使用方法
前言
緩存主要是為了提高數(shù)據(jù)的讀取速度。因?yàn)榉?wù)器和應(yīng)用客戶端之間存在著流量的瓶頸,所以讀取大容量數(shù)據(jù)時(shí),使用緩存來直接為客戶端服務(wù),可以減少客戶端與服務(wù)器端的數(shù)據(jù)交互,從而大大提高程序的性能。
緩存這個(gè)東西可大可小,小到一個(gè)靜態(tài)的字段,大到將整個(gè)數(shù)據(jù)庫Cache起來。項(xiàng)目開發(fā)過程中緩存的應(yīng)用到處可見,本文主要介紹一下使用的方法,下面話不多說了,來一起看看詳細(xì)的介紹吧
1.在ASP.NET中頁面緩存的使用方法簡單,只需要在aspx頁的頂部加上一句聲明即可:
<%@ OutputCache Duration="100" VaryByParam="none" %>
Duration:緩存時(shí)間(秒為單位),必填屬性
2.使用微軟自帶的類庫System.Web.Caching
新手接觸的話不建議直接使用微軟提供的類庫,因?yàn)檫@樣對(duì)理解不夠深刻。所以在這里我?guī)Т蠹易约簩懸惶拙彺娌僮鞣椒ǎ@樣理解得更加清晰。
話不多說,代碼開敲。
一、首先,先模擬數(shù)據(jù)來源。新建一個(gè)類,寫一個(gè)數(shù)據(jù)操作方法(該方法耗時(shí)、耗資源)
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>
/// 模擬從數(shù)據(jù)庫讀取數(shù)據(jù)
/// 耗時(shí)、耗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;
}
}
}
二、編寫一個(gè)緩存操作類
2.1 構(gòu)造一個(gè)字典型容器,用于存放緩存數(shù)據(jù),權(quán)限設(shè)為private ,防止隨意訪問造成數(shù)據(jù)不安全性
//緩存容器 private static Dictionary<string, object> CacheDictionary = new Dictionary<string, object>();
2.2 構(gòu)造三個(gè)方法(添加數(shù)據(jù)至緩存容器、從緩存容器獲取數(shù)據(jù)、判斷緩存是否存在)
/// <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);
}
三、程序入口編寫測(cè)試方法
3.1 先看一下普通情況不適用緩存,它的執(zhí)行效率有多慢
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}次請(qǐng)求------");
int result = DataSource.GetDataByDB(666);
Console.WriteLine($"第{i}次請(qǐng)求獲得的數(shù)據(jù)為:{result}");
}
}
}
}

3.2 接下來,我們編寫緩存試用方法。概念無非就是根據(jù)key前往字典容器里查找是否有相對(duì)應(yīng)緩存數(shù)據(jù),有則直接調(diào)用,沒有則生成并存入字典容器里。
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}次請(qǐng)求------");
//int result = DataSource.GetDataByDB(666);
int result = 0;
//key的名字一定要確保請(qǐng)求的準(zhǔn)確性 DataSource GetDataByDB 666缺一不可
string key = "DataSource_GetDataByDB_666";
if (CacheHelper.Exsits(key))
{
//緩存存在,直接獲取原數(shù)據(jù)
result = CacheHelper.Get<int>(key);
}
else
{
//緩存不存在,去生成緩存,并加入容器
result = DataSource.GetDataByDB(666);
CacheHelper.Add(key, result);
}
Console.WriteLine($"第{i}次請(qǐng)求獲得的數(shù)據(jù)為:{result}");
}
}
}
}
3.3 我們看看加入緩存之后的效率如何

四、可以看到,瞬間完成。事已至此,緩存的使用基本是完成了。但是回過頭來我們想想看。一個(gè)系統(tǒng)成百上千個(gè)地方使用緩存的話,那豈不是要寫成百上千個(gè)if else判斷緩存是否存在,然后獲?。?/strong>
答案顯而易見,肯定不合理的。所以我們要對(duì)代碼進(jìn)行優(yōu)化。
4.1 緩存操作類(CacheHelper)編寫一個(gè)通用的獲取方法
/// <summary>
/// 緩存獲取方法
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key">緩存字典容器對(duì)應(yīng)key</param>
/// <param name="func">委托方法 傳入操作對(duì)象</param>
/// <returns></returns>
public static T GetCache<T>(string key, Func<T> func)
{
T t = default(T);
if (CacheHelper.Exsits(key))
{
//緩存存在,直接獲取原數(shù)據(jù)
t = CacheHelper.Get<T>(key);
}
else
{
//緩存不存在,去生成緩存,并加入容器
t = func.Invoke();
CacheHelper.Add(key, t);
}
return t;
}
4.2 程序入口進(jìn)行調(diào)用,傳入的委托參數(shù)為lamad表達(dá)式優(yōu)化后的代碼
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}次請(qǐng)求------");
int result = 0;
//key的名字一定要確保請(qǐng)求的準(zhǔn)確性 DataSource GetDataByDB 666缺一不可
string key = "DataSource_GetDataByDB_666";
//將需要執(zhí)行的獲取數(shù)據(jù)操作編寫成委托傳入方法(重點(diǎn))
//Func<int> func = new Func<int>(() => { return DataSource.GetDataByDB(666); });
result = CacheHelper.GetCache(key, () => DataSource.GetDataByDB(666));
Console.WriteLine($"第{i}次請(qǐng)求獲得的數(shù)據(jù)為:{result}");
}
}
}
}
到這里,緩存的使用基本結(jié)束了。最好值得一提的是,緩存盡量在數(shù)據(jù)量小、重復(fù)查詢量大的情況下使用。因?yàn)榫彺嬉彩且膬?nèi)存的,服務(wù)器內(nèi)存是有限的!
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
Windows Form 分頁 具體實(shí)現(xiàn)
其實(shí)功能實(shí)現(xiàn)很簡單。我做的是一個(gè)通用的分頁控件。項(xiàng)目時(shí)間很緊,可能有點(diǎn)粗糙。歡迎大家斧正。不說了直接貼代碼吧2013-12-12
C# web.config之<customErrors>節(jié)點(diǎn)說明案例詳解
這篇文章主要介紹了C# web.config之<customErrors>節(jié)點(diǎn)說明案例詳解,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08
Unity3D Shader實(shí)現(xiàn)貼圖切換效果
這篇文章主要為大家詳細(xì)介紹了Unity3D Shader實(shí)現(xiàn)貼圖切換效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-03-03
C#中的只讀結(jié)構(gòu)體(readonly struct)詳解
這篇文章主要給大家介紹了關(guān)于C#中只讀結(jié)構(gòu)體(readonly struct)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
詳解如何在ASP.NET Core配置請(qǐng)求超時(shí)中間件
本文參考官方文檔,為大家詳細(xì)介紹如何使用Asp.net core 8.0 的最小API 模板項(xiàng)目,配置超時(shí)中間件,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解下2024-01-01

