欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來到入門教程網!

C#教程

當前位置:主頁 > 軟件編程 > C#教程 >

C#中緩存的基本使用方法

來源:本站原創(chuàng)|時間:2020-01-10|欄目:C#教程|點擊: 次

前言

緩存主要是為了提高數據的讀取速度。因為服務器和應用客戶端之間存在著流量的瓶頸,所以讀取大容量數據時,使用緩存來直接為客戶端服務,可以減少客戶端與服務器端的數據交互,從而大大提高程序的性能。

緩存這個東西可大可小,小到一個靜態(tài)的字段,大到將整個數據庫Cache起來。項目開發(fā)過程中緩存的應用到處可見,本文主要介紹一下使用的方法,下面話不多說了,來一起看看詳細的介紹吧

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

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

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

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

新手接觸的話不建議直接使用微軟提供的類庫,因為這樣對理解不夠深刻。所以在這里我?guī)Т蠹易约簩懸惶拙彺娌僮鞣椒ǎ@樣理解得更加清晰。

話不多說,代碼開敲。

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

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 先看一下普通情況不適用緩存,它的執(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}次請求------");
  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 我們看看加入緩存之后的效率如何

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

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

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表達式優(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}次請求------");
    int result = 0;
    //key的名字一定要確保請求的準確性 DataSource GetDataByDB 666缺一不可
    string key = "DataSource_GetDataByDB_666";

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

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

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

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對我們的支持。

上一篇:C#如何生成唯一訂單號

欄    目:C#教程

下一篇:C# 添加PDF頁眉/頁腳的示例代碼

本文標題:C#中緩存的基本使用方法

本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/5097.html

網頁制作CMS教程網絡編程軟件編程腳本語言數據庫服務器

如果侵犯了您的權利,請與我們聯系,我們將在24小時內進行處理、任何非本站因素導致的法律后果,本站均不負任何責任。

聯系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網 版權所有