当前位置:网站首页>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 .
边栏推荐
- The boss said: whoever wants to use double to define the amount of goods, just pack up and go
- 15. System limitations and options
- [development team follows] API specification
- Keep an IT training diary 055- moral bitch
- Johnson–Lindenstrauss Lemma
- ZABBIX API pulls the values of all hosts of a monitoring item and saves them in Excel
- 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.
- A. ABC
- 12. Gettimeofday() and time()
- AI 助力藝術設計抄襲檢索新突破!劉芳教授團隊論文被多媒體頂級會議ACM MM錄用
猜你喜欢
Problems and solutions of several concurrent scenarios of redis
[software implementation series] software implementation interview questions with SQL joint query diagram
Network byte order
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
What are the conditions for the opening of Tiktok live broadcast preview?
150 ppt! The most complete "fair perception machine learning and data mining" tutorial, Dr. AIST Toshihiro kamishima, Japan
Imperial cms7.5 imitation "D9 download station" software application download website source code
High level application of SQL statements in MySQL database (I)
false sharing
Dare to climb here, you're not far from prison, reptile reverse actual combat case
随机推荐
Unspeakable Prometheus monitoring practice
Global and Chinese market of thin film deposition systems 2022-2028: Research Report on technology, participants, trends, market size and share
Mysql-15 aggregate function
15. System limitations and options
Www 2022 | taxoenrich: self supervised taxonomy complemented by Structural Semantics
What are the conditions for the opening of Tiktok live broadcast preview?
ZABBIX API batch delete a template of the host
The first spring of the new year | a full set of property management application templates are presented, and Bi construction is "out of the box"
ZABBIX API pulls the values of all hosts of a monitoring item and saves them in Excel
Global and Chinese markets of advanced X-ray inspection system (Axi) in PCB 2022-2028: Research Report on technology, participants, trends, market size and share
On Valentine's day, I code a programmer's exclusive Bing Dwen Dwen (including the source code for free)
16. System and process information
Leetcode 110 balanced binary tree
Mysql to PostgreSQL real-time data synchronization practice sharing
Zblog collection plug-in does not need authorization to stay away from the cracked version of zblog
Redis transaction
Unity controls the selection of the previous and next characters
Yyds dry goods inventory override and virtual of classes in C
Global and Chinese market of small batteries 2022-2028: Research Report on technology, participants, trends, market size and share
Short math guide for latex by Michael downs