当前位置:网站首页>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 .
边栏推荐
- [Yugong series] February 2022 attack and defense world advanced question misc-84 (MySQL)
- Advanced learning of MySQL -- Application -- storage engine
- Global and Chinese market of small batteries 2022-2028: Research Report on technology, participants, trends, market size and share
- A brief talk on professional modeler: the prospect and professional development of 3D game modeling industry in China
- Unity writes a character controller. The mouse controls the screen to shake and the mouse controls the shooting
- There is no need to authorize the automatic dream weaving collection plug-in for dream weaving collection
- Design and implementation of redis 7.0 multi part AOF
- Kiss number + close contact problem
- 96% of the collected traffic is prevented by bubble mart of cloud hosting
- Global and Chinese market of handheld melanoma scanners 2022-2028: Research Report on technology, participants, trends, market size and share
猜你喜欢
The 37 year old programmer was laid off, and he didn't find a job for 120 days. He had no choice but to go to a small company. As a result, he was confused
LV1 previous life archives
Setting methods, usage methods and common usage scenarios of environment variables in postman
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.
16. System and process information
長文綜述:大腦中的熵、自由能、對稱性和動力學
Save Private Ryan - map building + voltage dp+deque+ shortest circuit
中電資訊-信貸業務數字化轉型如何從星空到指尖?
There is no need to authorize the automatic dream weaving collection plug-in for dream weaving collection
[Valentine's Day confession code] - Valentine's Day is approaching, and more than 10 romantic love effects are given to the one you love
随机推荐
Summarize the past to motivate yourself to move on
Contest3145 - the 37th game of 2021 freshman individual training match_ D: Ranking
Basé sur... Netcore Development blog Project Starblog - (14) Implementation of theme switching function
The "message withdrawal" of a push message push, one click traceless message withdrawal makes the operation no longer difficult
High level application of SQL statements in MySQL database (I)
MySQL advanced (Advanced) SQL statement (I)
Latex tips slash \backslash
2022 attached lifting scaffold worker (special type of construction work) free test questions and attached lifting scaffold worker (special type of construction work) examination papers 2022 attached
Global and Chinese markets for electroencephalogram (EEG) devices 2022-2028: Research Report on technology, participants, trends, market size and share
FRP intranet penetration
On Valentine's day, I code a programmer's exclusive Bing Dwen Dwen (including the source code for free)
[Valentine's Day confession code] - Valentine's Day is approaching, and more than 10 romantic love effects are given to the one you love
Osnabrueck University | overview of specific architectures in the field of reinforcement learning
MySQL workbench use
Imperial cms7.5 imitation "D9 download station" software application download website source code
Tsinghua University product: penalty gradient norm improves generalization of deep learning model
Contest3145 - the 37th game of 2021 freshman individual training match_ F: Smallest ball
Résumé: entropie, énergie libre, symétrie et dynamique dans le cerveau
Final consistency of MESI cache in CPU -- why does CPU need cache
Li Chuang EDA learning notes IX: layers