ASP.NET緩存處理類實(shí)例
本文實(shí)例講述了ASP.NET緩存處理類。分享給大家供大家參考。具體如下:
ASP.NET 緩存處理類。
用法:
Just copy this code into a new class file (.cs) and add it to your ASP .NET website. One thing to keep in mind is that data stored in ASP .NET Cache can be accessible across all sessions. So when creating a cacheID for the object to be stored, it must be unique (or it could be overwritten). I usually store the unique cacheID in the session and then use that to referrence the cacheID. (e.g. CacheHandler.Write(Session["MyCacheData"], myData);)
具體代碼如下:
using System;
using System.Collections.Generic;
using System.Web.Caching;
using System.Web;
/// <summary>
/// This class reads/writes to ASP .NET server cache. For the sake of
/// simplicity, the class writes objects to cache with no expirateion.
/// Use the Remove() function to programmatically remove objects stored
/// from the server cache. This class was created as an alternative to
/// storing large objects in the session.
/// </summary>
public class CacheHandler
{
public static bool Write(string cacheID, object data)
{
if (HttpContext.Current == null)
return false;
if (cacheID == null || cacheID.Equals(""))
return false;
HttpRuntime.Cache.Insert(
cacheID, data, null, Cache.NoAbsoluteExpiration,
Cache.NoSlidingExpiration, CacheItemPriority.NotRemovable, null
);
return true;
}
public static object Read(string cacheID)
{
if (HttpContext.Current == null)
return null;
return HttpRuntime.Cache.Get(cacheID);
}
public static void Remove(string cacheID)
{
if (HttpContext.Current == null )
return;
if (cacheID == null || cacheID.Equals(""))
return;
HttpRuntime.Cache.Remove(cacheID);
}
}
希望本文所述對大家的asp.net程序設(shè)計(jì)有所幫助。
相關(guān)文章
.Net Core微信服務(wù)商二次進(jìn)件的開發(fā)
這篇文章主要介紹了.Net Core微信服務(wù)商二次進(jìn)件的開發(fā),包括服務(wù)商證書獲取方法及查詢進(jìn)件狀態(tài)的詳細(xì)代碼,本文給大家介紹的非常詳細(xì),需要的朋友可以參考下2021-10-10
Asp.Net 網(wǎng)站優(yōu)化系列之?dāng)?shù)據(jù)庫優(yōu)化分字訣上 分庫
當(dāng)我們的數(shù)據(jù)量很小的時候,我們會把用戶表,博客表,論壇表,閃存表等等都砸在一個庫里,我們的業(yè)務(wù)增長的很好,在不久之后我們盡力的優(yōu)化了查詢,但是效果依然不佳,這時候用分字訣的時機(jī)到了。2010-06-06
SQL為查詢的結(jié)果加上序號(ROW_NUMBER) 合并多個查詢結(jié)果
SQL為查詢的結(jié)果加上序號(ROW_NUMBER) 合并多個查詢結(jié)果2010-03-03

