0

right now I am using Ajax that trigger after every 3 minutes. But when we keep the website in the ideal mode for 4-5 hours it's cache gets null. I have verified the things are working on My Local development system, but not working on the Staging environment. I am not able to get the cause of it. here is my code:

$(document).ready(function () {
RefreshBhTokens();
function RefreshBhTokens() {
    $.ajax({
        type: "GET",
        url: "/home/refreshToken",
        complete: function (response) {
            setTimeout(function () { RefreshBhTokens(); }, 200000);
        },
        failure: function (response) {
            alert(response);
        }
    });
 }
});

Controller code:

[HttpGet]
    public IActionResult refreshToken()
    {
        CommonFunctions.CacheHandling objCache1 = new CommonFunctions.CacheHandling(_cache);
        dynamic test = objCache1.GenerateBhRestToken("");
        return Json(null);
    }

CommonFunction file:

public dynamic GenerateBhRestToken(string code)
    {
        dynamic AccessCode = AccessCodeOrRefreshCode(code);
        if (AccessCode != null)
        {
            JObject cacheEntry;
            if (!_cache.TryGetValue("BhRestToken", out cacheEntry))
            {
                // Key not in cache, so get data.
                cacheEntry = GenerateAccessTokens.BhRestToken(AccessCode["access_token"].Value);
                // Save data in cache.
                _cache.Set("BhRestToken", cacheEntry, cacheEntryOptions);
                _cache.Set("BhMemCahceExpiry", DateTime.Now.AddMinutes(5), cacheEntryOptions);
            }
            else
            {
                if (DateTime.Now > Convert.ToDateTime(GetMemoryCacheKeyValue("BhMemCahceExpiry")))
                {
                    cacheEntry = GenerateAccessTokens.BhRestToken(AccessCode["access_token"].Value);

                    // Save data in cache.
                    _cache.Set("BhRestToken", cacheEntry, cacheEntryOptions);
                    _cache.Set("BhMemCahceExpiry", DateTime.Now.AddMinutes(5), cacheEntryOptions);
                }
                else
                {
                    cacheEntry = (JObject)GetMemoryCacheKeyValue("BhRestToken");

                    if (cacheEntry == null)
                    {
                        cacheEntry = GenerateAccessTokens.BhRestToken(AccessCode["access_token"].Value);

                        // Save data in cache.
                        _cache.Set("BhRestToken", cacheEntry, cacheEntryOptions);
                        _cache.Set("BhMemCahceExpiry", DateTime.Now.AddMinutes(5), cacheEntryOptions);
                    }
                    else
                    {
                        cacheEntry = (JObject)GetMemoryCacheKeyValue("BhRestToken");
                    }

                }

            }
            return cacheEntry["BhRestToken"].Value<string>();
        }
        else { return null; }
    }

And To generate Access_code:

public dynamic AccessCodeOrRefreshCode(string code)
    {
        JObject cacheEntry;

        if (!_cache.TryGetValue("Access_Code", out cacheEntry))
        {
            // Key not in cache, so get data.
            cacheEntry = GenerateAccessTokens.GenerateAccessTokenOrRefreshToken(true, code);

            // Save data in cache.
            _cache.Set("Access_Code", cacheEntry, cacheEntryOptions);
            _cache.Set("MemCahceExpiry", DateTime.Now.AddMinutes(5), cacheEntryOptions);
        }
        else
        {

            if (DateTime.Now > Convert.ToDateTime(GetMemoryCacheKeyValue("MemCahceExpiry")))
            {
                string refresh_token = cacheEntry["refresh_token"].ToString();
                cacheEntry = GenerateAccessTokens.GenerateAccessTokenOrRefreshToken(false, refresh_token);
                _cache.Set("Access_Code", cacheEntry, cacheEntryOptions);
                _cache.Set("MemCahceExpiry", DateTime.Now.AddMinutes(5), cacheEntryOptions);
            }
            else
            {
                if (cacheEntry == null)
                {
                    cacheEntry = GenerateAccessTokens.GenerateAccessTokenOrRefreshToken(true, code);

                    // Save data in cache.
                    _cache.Set("Access_Code", cacheEntry, cacheEntryOptions);
                    _cache.Set("MemCahceExpiry", DateTime.Now.AddMinutes(5), cacheEntryOptions);
                }
                else
                {
                    cacheEntry = (JObject)GetMemoryCacheKeyValue("Access_Code");
                }
            }
        }
        return cacheEntry;
    }

I am not getting what is causing this issue. Could you please help me.

Ram Singh
  • 6,664
  • 35
  • 100
  • 166

0 Answers0