แคช ASP.NET Webforms ถูกนำมาใช้อย่างสะดวกสบายมากสำหรับการรักษาข้อมูลการปฏิบัติงานชั่วคราว ในช่วงปีแรก ๆ ของแพลตฟอร์ม. NET นักพัฒนามักใช้ในการทำงานกับระบบเนมสเปซ System.Web แม้ในแอปพลิเคชัน Winforms
เทมเพลตรหัสที่นำเสนออย่างต่อเนื่องในบทความเกี่ยวกับการแคช ASP.NET นั้นง่ายมากและใช้งานได้จริง:
// try to get an instance of object from cache
DataSet ds = HttpRuntime . Cache [ "KeyName" ] as DataSet ;
// check the result and recreate it, if it is null
if ( ds == null )
{
ds = QueryDataFromDatabase ( ) ;
HttpRuntime . Cache . Insert ( "KeyName" , ds ) ;
}
// using the instance of object that has been populated from cache or from storage
DataRow dr = ds . Tables [ 0 ] . Rows [ 0 ] ;อย่างไรก็ตามการใช้งานแคช ASP.NET ไม่รวมคุณสมบัติที่ต้องการ
แม้ว่าเมธอด cache.insert (String, Object) ที่เพิ่มค่าลงในแคช แต่ก็เหมาะสำหรับกรณีส่วนใหญ่ แต่มักจะเป็นที่พึงปรารถนาที่จะใช้การตั้งค่าที่แตกต่างกันสำหรับการแคชข้อมูลตามการตั้งค่าของแอปพลิเคชันเว็บที่เก็บไว้ในไฟล์ web.config และบางครั้งก็ปิดการใช้งานแคชสำหรับแอปพลิเคชันทั้งหมด ในกรณีนี้ซอร์สโค้ดไม่ควรเปลี่ยนแปลงและแอปพลิเคชันควรทำงานโดยไม่ต้องคอมไพล์ใหม่
แคช ASP.NET จะส่งคืนอินสแตนซ์ของคลาส object เสมอไม่ว่าประเภทของอินสแตนซ์จะเป็นอย่างไร ในกรณีส่วนใหญ่นี่ไม่ใช่ปัญหาเนื่องจากสามารถใช้ Nullable types แทน Value types บางทีคุณอาจต้องการใช้วิธีการทั่วไปในการดึงข้อมูลจากแคช:
// this is a dafault extraction data from cache
myClassName item = HttpRuntime . Cache [ "Key1" ] as myClassName ;
// this is a desired extraction data with generic methods
myClassName item = DataCache . Get < myClassName > ( "Key1" ) ; หากวิธีการทั่วไปไม่พอใจสำหรับคุณก็เป็นไปได้ที่จะใช้เวอร์ชันเริ่มต้น ในความเป็นจริงตัวเลือกทั้งสองนี้เหมือนกัน แต่วิธีการทั่วไปมีคุณสมบัติเพิ่มเติม ตัวอย่างเช่นค่าเริ่มต้นสามารถกำหนดได้หากแคชไม่มีค่าที่จะเรียกคืน:
myClassName defaultValue = new myClassName ( /* init properties */ ) ;
myClassName item = DataCache . Get < myClassName > ( "Key1" , defaultValue ) ;
/* if there is nothing in the cache, then the item will be defaultValue */แคช ASP.NET จะส่งคืนวัตถุที่เก็บไว้ในแคชเสมอ ซึ่งหมายความว่าการเปลี่ยนแปลงคุณสมบัติใด ๆ ของวัตถุที่แยกออกมาจะเปลี่ยนวัตถุที่เก็บไว้ในแคชเนื่องจากสิ่งเหล่านี้เป็นวัตถุเดียวกัน ในบางกรณีคุณอาจต้องการแก้ไขวัตถุที่ดึงมา แต่ทิ้งวัตถุไว้ในแคชไม่เปลี่ยนแปลง หนึ่งในฟังก์ชั่นที่สะดวกคือการดึงสำเนาของวัตถุจากแคชซึ่งสามารถเปลี่ยนแปลงได้โดยไม่ต้องกังวลเกี่ยวกับวัตถุในแคช
แคช ASP.NET คล้ายกับ NameValueCollection มันเป็นโซลูชันที่เรียบง่ายและสง่างามสำหรับเว็บแอปพลิเคชันขนาดเล็ก แต่มันยากที่จะสร้างคีย์เมื่อแอปพลิเคชันเติบโตขึ้น ตัวอย่างเช่นพิจารณาการแคชสองตารางที่เกี่ยวข้องเช่นผู้ขายและรุ่น ในกรณีนี้เว็บแอปพลิเคชันจะเก็บข้อมูลทั้งหมดจากตารางผู้ขายในวัตถุแคชเดียวและวัตถุแคชจำนวนมากสำหรับบันทึกที่เกี่ยวข้องของตารางโมเดล กุญแจสำคัญสำหรับผู้ขายควรเป็น Vendors และกุญแจสำหรับรุ่นที่ควรจะเป็น Models.[VendorID] เมื่อตารางผู้ขายมีการเปลี่ยนแปลงอินสแตนซ์แคชสำหรับผู้ขายและอินสแตนซ์ที่เกี่ยวข้องเฉพาะสำหรับโมเดลจะต้องถูกลบออกจากแคช ซึ่งหมายความว่าควรแคชข้อมูลและลบออกจากแคชตามภูมิภาค
ฟังก์ชั่นที่อธิบายไว้เป็นทางเลือกและสามารถนำไปใช้ได้อย่างง่ายดายในเว็บแอปพลิเคชันใด ๆ เทมเพลตพร็อกซีเป็นตัวเลือกที่ดีสำหรับการใช้คุณสมบัติทั้งหมดที่อธิบายไว้ ไลบรารีที่เกิดขึ้นสามารถใช้กับ. NET Frameworks ทั้งหมด, เริ่มต้นเวอร์ชัน 2.0 . NET Framework 4.0 แนะนำ System.Runtime.Caching Namespace และหลายคลาสที่มีรูปแบบการแคชใหม่ ดังนั้นการใช้แคชมาตรฐาน ASP.NET และการปรับปรุงจึงไม่จำเป็น
เศร้า แต่จริง
/// <summary>
/// Is cache used or not
/// </summary>
public static bool IsCacheEnable ;
/// <summary>
/// How to store objects in the cache
/// </summary>
public static CacheExpirationType ExpirationType ;
/// <summary>
/// How long objects should be stored in the cache
/// </summary>
public static TimeSpan ExpirationTime ; enum CacheExpirationType กำหนดวิธีการจัดเก็บวัตถุในแคช
/// <summary>
/// How to store objects in the cache
/// </summary>
public enum CacheExpirationType
{
/// <summary>
/// Without time limit, the value of the <seealso cref = "DataCache.ExpirationTime" /> property will be ignored
/// </summary>
NoExpiration ,
/// <summary>
/// The <seealso cref="DataCache.ExpirationTime"/> at which the inserted object expires and is removed from the cache
/// </summary>
AbsoluteExpiration ,
/// <summary>
/// The interval <seealso cref="DataCache.ExpirationTime"/> between the time the inserted object is last accessed and the time at which that object expires
/// </summary>
SlidingExpiration
} /// <summary>
/// Retrieves the specified item from the Cache object
/// </summary>
/// <typeparam name="T">The type for the cache item to retrieve</typeparam>
/// <param name="key">The identifier for the cache item to retrieve</param>
/// <param name="defaultValue">The default value if the object is not in the cache</param>
/// <returns>The retrieved cache item, or <paramref name="defaultValue"/> if the key is not found</returns>
public static T GetData < T > ( string key , T defaultValue = default ( T ) )
/// <summary>
/// Retrieves the deep copied of specified item from the Cache object
/// </summary>
/// <typeparam name="T">The type for the cache item to retrieve</typeparam>
/// <param name="key">The identifier for the cache item to retrieve</param>
/// <param name="defaultValue">The default value if the object is not in the cache</param>
/// <returns>The retrieved cache item, or <paramref name="defaultValue"/> if the key is not found</returns>
public static T GetDeepCopiedData < T > ( string key , T defaultValue = default ( T ) ) /// <summary>
/// Inserts an item into the cache with a cache key to reference its location, using default values provided by the settings
/// </summary>
/// <param name="key">The cache key used to reference the item</param>
/// <param name="value">The object to be inserted into the cache</param>
public static void InsertData ( string key , object value )
/// <summary>
/// Inserts an item into the cache with a cache key to reference its location, using the absolute expiration time
/// </summary>
/// <param name="key">The cache key used to reference the item</param>
/// <param name="value">The object to be inserted into the cache</param>
/// <param name="expirationTime">How long the object should be stored in the cache</param>
public static void InsertAbsoluteExpirationData ( string key , object value , TimeSpan expirationTime )
/// <summary>
/// Inserts an item into the cache with a cache key to reference its location, using the sliding expiration time
/// </summary>
/// <param name="key">The cache key used to reference the item</param>
/// <param name="value">The object to be inserted into the cache</param>
/// <param name="expirationTime">How long the object should be stored in the cache</param>
public static void InsertSlidingExpirationData ( string key , object value , TimeSpan expirationTime )
/// <summary>
/// Inserts an item into the cache with a cache key to reference its location, using the type of expiration and default value for expiration time
/// </summary>
/// <param name="key">The cache key used to reference the item</param>
/// <param name="value">The object to be inserted into the cache</param>
/// <param name="expirationType">How to store objects in the cache</param>
public static void InsertExpirationData ( string key , object value , CacheExpirationType expirationType )
/// <summary>
/// Inserts an item into the cache with a cache key to reference its location, using the type of expiration and expiration time
/// </summary>
/// <param name="key">The cache key used to reference the item</param>
/// <param name="value">The object to be inserted into the cache</param>
/// <param name="expirationType">How to store objects in the cache</param>
/// <param name="expirationTime">How long the object should be stored in the cache</param>
public static void InsertExpirationData ( string key , object value , CacheExpirationType expirationType , TimeSpan expirationTime ) /// <summary>
/// Removes the specified item from the application's cache
/// </summary>
/// <param name="key">An identifier for the cache item to remove</param>
public static void RemoveDataByKey ( string key )
/// <summary>
/// Removes all items from the application's cache that starts with key
/// </summary>
/// <param name="keyStartsWith">An starts with identifier for the cache item to remove</param>
public static void RemoveAllDataByKey ( string keyStartsWith )
/// <summary>
/// Removes all items from the application's cache
/// </summary>
public static void RemoveAllData ( ) public class Global : System . Web . HttpApplication
{
protected void Application_Start ( object sender , EventArgs e )
{
Settings settings = Settings . Default ;
DataCache . IsCacheEnable = settings . IsCacheEnable ;
DataCache . ExpirationType = settings . ExpirationType ;
DataCache . ExpirationTime = settings . ExpirationTime ;
}
} public partial class DefaultPage : System . Web . UI . Page
{
public const string STR_CACHENAME = "something" ;
protected void Page_Load ( object sender , EventArgs e )
{
if ( ! this . IsPostBack )
{
SomethingDataModel val = DataCache . GetData < SomethingDataModel > ( STR_CACHENAME ) ;
if ( val == null )
{
SomethingDataModel val = new SomethingDataModel ( ) ; // get data from ...
DataCache . InsertData ( STR_CACHENAME , val ) ;
}
DataBind ( val ) ; // use data on the page
}
}
} หากคุณต้องการกำหนดการตั้งค่าเวลาที่แตกต่างจากที่ระบุโดยค่าเริ่มต้นในแคชให้ใช้หนึ่งใน Insert{Which}Data
public partial class DefaultPage : System . Web . UI . Page
{
public const string STR_CACHENAME = "something" ;
protected void Page_Load ( object sender , EventArgs e )
{
if ( this . IsPostBack )
{
DAL . Update ( ) ; // update the database ...
DataCache . RemoveAllDataByKey ( STR_CACHENAME ) ; // clear cache
Response . Redirect ( "Default.aspx" ) ; // proccessing the request
}
}
} มีคำถาม? ติดต่อฉันและฉันจะช่วยคุณจัดเรียง
<style> .inner {Min-Width: 800px! สำคัญ; ความกว้างสูงสุด: 60%! สำคัญ;} </style>