admin管理员组文章数量:1336380
I am encountering an issue with HttpRuntime.Cache in an ASP.NET 4.7 application, where the caching mechanism works as expected locally (including on local IIS), but fails to retrieve cached items when the application is deployed on a Virtual Machine.
Issue Details: Local Environment: The cache correctly stores and retrieves items. VM Environment: After storing items in the cache,on the spot when I Logged it show data in the cache but in next attempts to retrieve ,it return null, as if the items were never cached. Implementation Snippet: Here's a simplified version of how I'm managing the cache:
public bool TryGetFromCache<T>(string cacheKey, out T cachedData)
{
cachedData = default(T);
var data = HttpRuntime.Cache.Get(cacheKey);
if (data != null && data is T)
{
cachedData = (T)data;
return true;
}
return false;
}
public void AddToCache<T>(string cacheKey, T data, double cacheDurationMinutes = 120)
{
try
{
HttpRuntime.Cache.Insert(
cacheKey,
data,
null,
DateTime.Now.AddMinutes(cacheDurationMinutes),
System.Web.Caching.Cache.NoSlidingExpiration);
logger.Publish("Cache Operation", $"Successfully added to cache: {cacheKey}", null);
}
catch (Exception ex)
{
logger.Publish("Cache Operation Error", $"Failed to add to cache: {cacheKey}. Error: {ex.Message}", ex);
}
}
public APIResponse LoadBanners()
{
try
{
string log = string.Empty;
string cacheKey = "BannerList";
log = "Checking cache for key: " + cacheKey;
if (!TryGetFromCache(cacheKey, out List<Banners> banners))
{
log += " - Cache not found. Fetching from CRM.";
banners = _Service_Account.LoadBanners();
AddToCache(cacheKey, banners, 120);
var cachedValue = HttpRuntime.Cache.Get("BannerList");
if (cachedValue == null)
{
// Log that the cache insertion failed
log += " - cache insertion failed.";
}
else
{
log += " - CCache found. Using cached banners.";
}
}
else
{
log += " - Cache found. Using cached banners.";
}
var Response_msg = banners;
if (Response_msg != null)
{
response.Code = Codes.SUCCESS;
response.Message = Messages.SUCCESS;
response.Data = new { Response_msg = Response_msg };
return response;
}
}
catch (Exception)
{
throw;
}
response.Code = Codes.FAILURE;
response.Message = Messages.FAILURE;
response.Data = new { };
return response;
}
I appreciate any insights or suggestions on how to resolve this caching issue on a VM.
本文标签: cHttpRuntimeCache Working Locally But Not on VM Deployment in ASPNET 47Stack Overflow
版权声明:本文标题:c# - HttpRuntime.Cache Working Locally But Not on VM Deployment in ASP.NET 4.7 - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742405573a2468751.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论