最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - HttpRuntime.Cache Working Locally But Not on VM Deployment in ASP.NET 4.7 - Stack Overflow

programmeradmin0浏览0评论

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.

发布评论

评论列表(0)

  1. 暂无评论