当前位置:网站首页>Cache general management class + cache httpcontext Current. Cache and httpruntime Differences between caches
Cache general management class + cache httpcontext Current. Cache and httpruntime Differences between caches
2022-07-04 02:54:00 【Brother Lei talks about programming】
I used to write asp.net Time use HttpContext.Current.Cache Cache is very easy to use , I wrote one today windows Service program ,HttpContext.Current.Cache It's OK when saving cache , Always report an error when taking “ Object not referenced to instance ” Very depressed. , After looking up the information, I realized that the reference program cache should be used HttpRuntime.Cache...
We see first MSDN Explanation above :
HttpContext.Current.Cache: For the current HTTP The request for Cache object .
HttpRuntime.Cache: Gets the name of the current application Cache.
The attached write a general class of operation cache , Use... In an application , If you want to in asp.net There is , Just put HttpRuntime.Cache Change it to HttpContext.Current.Cache that will do , The code is as follows :
Code
using System;
/// < summary >
/// author:Stone_W
/// date:2010.12.1
/// desc: Cache management class
/// Be careful : To add a reference to System.Web
/// </ summary >
public class MyCacheTools : System.Web.SessionState.IRequiresSessionState
{
#region Deposit in Cache
/// < summary >
/// Deposit in Cache
/// </ summary >
/// < param name= "key" > cache key </ param >
/// < param name= "value" > The value of the cache </ param >
/// < param name= "time_HH" > save xx Hours </ param >
/// < returns > Successful execution [bool] </ returns >
public static bool SetCache(string key, object value, int time_HH)
{
bool result = false;
try
{
DateTime dt = DateTime.Now.AddHours(time_HH);
System.Web.HttpRuntime.Cache.Insert(key, value, null,
dt, System.Web.Caching.Cache.NoSlidingExpiration);
result = true;
}
catch (Exception ex) { }
return result;
}
#endregion
#region obtain Cache
/// < summary >
/// obtain Cache
/// </ summary >
/// < param name= "key" >key </ param >
/// < returns >object type </ returns >
public static object GetCache(string key)
{
return System.Web.HttpRuntime.Cache.Get(key);
}
#endregion
#region Inquire about Cache Whether there is
/// < summary >
/// Inquire about Cache Whether there is
/// </ summary >
/// < param name= "key" >key value </ param >
/// < returns >bool </ returns >
public static bool IsCacheExist(string key)
{
bool result = false;
object temp = System.Web.HttpRuntime.Cache.Get(key);
if (null != temp)
{
result = true;
}
return result;
}
#endregion
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
ps:HttpContext.Current.Cache by null This problem makes me very painful , Finally, it was solved , I hope this article is useful to you !
Follow the QR code below , Subscribe to more .
边栏推荐
- Contest3145 - the 37th game of 2021 freshman individual training match_ G: Score
- [development team follows] API specification
- Contest3145 - the 37th game of 2021 freshman individual training match_ D: Ranking
- Contest3145 - the 37th game of 2021 freshman individual training match_ J: Eat radish
- Keep an IT training diary 054- opening and closing
- Osnabrueck University | overview of specific architectures in the field of reinforcement learning
- On Valentine's day, I code a programmer's exclusive Bing Dwen Dwen (including the source code for free)
- The difference between int (1) and int (10)
- 14. Process time
- Buuctf QR code
猜你喜欢
3D game modeling is in full swing. Are you still confused about the future?
AI 助力藝術設計抄襲檢索新突破!劉芳教授團隊論文被多媒體頂級會議ACM MM錄用
A brief talk on professional modeler: the prospect and professional development of 3D game modeling industry in China
7 * 24-hour business without interruption! Practice of applying multiple live landing in rookie villages
WordPress collection WordPress hang up collection plug-in
Measurement fitting based on Halcon learning [4] measure_ arc. Hdev routine
中電資訊-信貸業務數字化轉型如何從星空到指尖?
[software implementation series] software implementation interview questions with SQL joint query diagram
Yyds dry goods inventory override and virtual of classes in C
Pagoda SSL can't be accessed? 443 port occupied? resolvent
随机推荐
13. Time conversion function
Lichuang EDA learning notes 14: PCB board canvas settings
Ai aide à la recherche de plagiat dans le design artistique! L'équipe du professeur Liu Fang a été embauchée par ACM mm, une conférence multimédia de haut niveau.
Keepalived set the master not to recapture the VIP after fault recovery (it is invalid to solve nopreempt)
[software implementation series] software implementation interview questions with SQL joint query diagram
Data collection and summary
LV1 previous life archives
Sword finger offer 14- I. cut rope
60 year old people buy medical insurance and recommend a better product
Unity controls the selection of the previous and next characters
Global and Chinese market of small batteries 2022-2028: Research Report on technology, participants, trends, market size and share
Résumé des outils communs et des points techniques de l'examen PMP
[Valentine's Day confession code] - Valentine's Day is approaching, and more than 10 romantic love effects are given to the one you love
Global and Chinese market for travel wheelchairs 2022-2028: Research Report on technology, participants, trends, market size and share
Backpropagation formula derivation [Li Hongyi deep learning version]
Setting methods, usage methods and common usage scenarios of environment variables in postman
Database concept and installation
A. Div. 7
false sharing
14. Process time