当前位置:网站首页>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 .

边栏推荐
- Create real-time video chat in unity3d
- Bugku Zhi, you have to stop him
- CSCI 2134
- Buuctf QR code
- Dans la recherche de l'intelligence humaine ai, Meta a misé sur l'apprentissage auto - supervisé
- 2022 registration examination for safety production management personnel of fireworks and firecracker production units and examination skills for safety production management personnel of fireworks an
- Solve the problem that the tabbar navigation at the bottom of vantui does not correspond to the page (window.loading.hash)
- Dare to climb here, you're not far from prison, reptile reverse actual combat case
- I stepped on a foundation pit today
- Unity writes a character controller. The mouse controls the screen to shake and the mouse controls the shooting
猜你喜欢

WordPress collection WordPress hang up collection plug-in

What is cloud primordial?

C language black Technology: Archimedes spiral! Novel, interesting, advanced~

I stepped on a foundation pit today

17. File i/o buffer

Setting methods, usage methods and common usage scenarios of environment variables in postman

Mysql-15 aggregate function

LV1 tire pressure monitoring

Record a problem that soft deletion fails due to warehouse level error

Résumé: entropie, énergie libre, symétrie et dynamique dans le cerveau
随机推荐
Global and Chinese market of thin film deposition systems 2022-2028: Research Report on technology, participants, trends, market size and share
150 ppt! The most complete "fair perception machine learning and data mining" tutorial, Dr. AIST Toshihiro kamishima, Japan
false sharing
Unspeakable Prometheus monitoring practice
There is no need to authorize the automatic dream weaving collection plug-in for dream weaving collection
12. Gettimeofday() and time()
Solve the problem that the tabbar navigation at the bottom of vantui does not correspond to the page (window.loading.hash)
Question C: Huffman tree
PTA tiantisai l1-079 tiantisai's kindness (20 points) detailed explanation
Unity controls the selection of the previous and next characters
C language black Technology: Archimedes spiral! Novel, interesting, advanced~
Keepalived set the master not to recapture the VIP after fault recovery (it is invalid to solve nopreempt)
Take you to master the formatter of visual studio code
15. System limitations and options
Global and Chinese market of contour projectors 2022-2028: Research Report on technology, participants, trends, market size and share
The difference between int (1) and int (10)
Format character%* s
中電資訊-信貸業務數字化轉型如何從星空到指尖?
Database concept and installation
STM32 key content