It can be used in ordinary aspx.cs code:
Cache cache = new Cache();
But in XXXX.CS, you can’t use the above method, you have to use:
Never expires
HttpContext.Current.Cache.Insert("Name", "Wang Xiang", null, DateTime.MaxValue, TimeSpan.Zero, CacheItemPriority.NotRemovable, null);
Name:Key
Wang Xiang: Value
null: Indicates no cache dependencies
DateTime.MaxValue: The maximum value of time (9999-99-99 12:59:59), indicating that the absolute time expiration policy is not used
TimeSpan.Zero: Indicates that smooth expiration is not used
CacheItemPrority.NotRemovable: Indicates that the priority is not to delete the Cache
null: If you don’t use it very much, just leave it null.
ASP.NET
Absolute time expiration (automatically expires after 10 seconds)
HttpContext.Current.Cache.Insert("Name", "Wang Xiang", null, DateTime.Now.AddSeconds(10), TimeSpan.Zero, CacheItemPriority.NotRemovable, null);
Smooth time expiration (the cache will automatically expire if it is not accessed for 10 consecutive seconds)
HttpContext.Current.Cache.Insert("Name", "Wang Xiang", null, DateTime.MaxValue, TimeSpan.FromSeconds(10));
Cache update strategy
if (HttpContext.Current.Cache["UserCacheList"] != null)
{
ht = (Hashtable)HttpContext.Current.Cache["UserCacheList"];
ht.Add(uId, HttpContext.Current.Cache["User" + uId]);
}
else
{
ht.Add(uId, HttpContext.Current.Cache["User" + uId]);
//HttpContext.Current.Cache["UserCacheList"] = ht;
HttpContext.Current.Cache.Insert("UserCacheList", ht, null, DateTime.MaxValue, TimeSpan.Zero, CacheItemPriority.NotRemovable, null);
}