Use Cache.Add() and set the cache priority extremely high(CacheItemPriority.NotRemovable), this will help it stay in the cache.
HttpContext.Current.Cache.Add("key", "your object", null, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.NotRemovable, null);
System.Web.Caching.CacheItemPriority.NotRemovable
This still seems to not guarantee it being in the cache, but it also sets it so high its highly unlikely that it will be... but you will still need to check if its there and re-cache if not...
(looks like some people on the intertubes don't like using this high a cache setting... and some do... depends on your exact scenario i guess...)
public static string GetSpecialObject
{
get
{
object obj = HttpContext.Current.Cache["key"];
if (obj == null)
{
obj = HttpContext.Current.Cache.Add("key",
"I stay in cache as best i can object", // Build Object Function Here
null,
Cache.NoAbsoluteExpiration,
Cache.NoSlidingExpiration,
CacheItemPriority.NotRemovable,
null);
}
return (string)obj;
}
}
good luck