Подробности о проблеме:
Локальная среда: кэш правильно сохраняет и извлекает элементы.
Среда виртуальной машины: после сохранения элементов в кэше, сразу после входа в систему. он показывает данные в кеше, но при следующих попытках извлечения возвращает ноль, как если бы элементы никогда не кэшировались.
Фрагмент реализации:
Вот упрощенная версия того, как я управляю кешем:< /p>
Код: Выделить всё
public bool TryGetFromCache(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(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))
{
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;
}
Подробнее здесь: https://stackoverflow.com/questions/792 ... sp-net-4-7
Мобильная версия