当前位置:网站首页>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 .
边栏推荐
- No clue about the data analysis report? After reading this introduction of smartbi, you will understand!
- Node write API
- CSCI 2134
- Properties of binary trees (numerical aspects)
- C language black Technology: Archimedes spiral! Novel, interesting, advanced~
- Problems and solutions of several concurrent scenarios of redis
- Remember another interview trip to Ali, which ends on three sides
- Kiss number + close contact problem
- Setting methods, usage methods and common usage scenarios of environment variables in postman
- 16. System and process information
猜你喜欢
1day vulnerability pushback skills practice (3)
Kiss number + close contact problem
The difference between MCU serial communication and parallel communication and the understanding of UART
在尋求人類智能AI的過程中,Meta將賭注押向了自監督學習
LV1 previous life archives
長文綜述:大腦中的熵、自由能、對稱性和動力學
Talking about custom conditions and handling errors in MySQL Foundation
1day vulnerability pushback skills practice (3)
Mysql-15 aggregate function
中電資訊-信貸業務數字化轉型如何從星空到指尖?
随机推荐
2022 Guangxi provincial safety officer a certificate examination materials and Guangxi provincial safety officer a certificate simulation test questions
Global and Chinese market of handheld melanoma scanners 2022-2028: Research Report on technology, participants, trends, market size and share
Key knowledge of C language
1day vulnerability pushback skills practice (3)
[untitled] the relationship between the metauniverse and digital collections
16. System and process information
Network communication basic kit -- IPv4 socket structure
Sword finger offer 14- I. cut rope
Global and Chinese market of box seals 2022-2028: Research Report on technology, participants, trends, market size and share
Keepalived set the master not to recapture the VIP after fault recovery (it is invalid to solve nopreempt)
Command Execution Vulnerability - command execution - vulnerability sites - code injection - vulnerability exploitation - joint execution - bypass (spaces, keyword filtering, variable bypass) - two ex
Keep an IT training diary 054- opening and closing
Practical multifunctional toolbox wechat applet source code / support traffic master
96% of the collected traffic is prevented by bubble mart of cloud hosting
Libcblas appears when installing opencv import CV2 so. 3:cannot open shared object file:NO such file or directory
Career development direction
Imperial cms7.5 imitation "D9 download station" software application download website source code
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.
Hospital network planning and design document based on GLBP protocol + application form + task statement + opening report + interim examination + literature review + PPT + weekly progress + network to
Data collection and summary