Search code examples
c#.netazureredisazure-redis-cache

StackExchange.Redis string value is converted to set


I use StackExchange.Redis to work with cache. I have action which returns values from cache (if value exists)

public ActionResult GetCalculatorSalaries()
    {
        var s = (string) _cache.StringGet("CalculatorSalaries");
        if (String.IsNullOrEmpty(s))
        {
            var salaries = _service.LoadCalculatorSalaries();
            _cache.SetAdd("CalculatorSalaries", (string)salaries);
            return Json(salaries, JsonRequestBehavior.AllowGet);
        }
        return Json(s, JsonRequestBehavior.AllowGet);
    }

_service.LoadCalculatorSalaries(); - this is service which returns data from Azure Blob and here returns data as string.

Problem: I set value for key "CalculatorSalaries" as string but when i try to get it, i get an error, as value by that key is of type set.

Where i am wrong?


P.S. When i debug code at step

var s = (string) _cache.StringGet("CalculatorSalaries");

debuging skiping when value exists for key "CalculatorSalaries" and error get only in browser console


Solution

  • You're calling SetAdd... thereby adding a set.

    Perhaps you meant StringSet:

    var salaries = _service.LoadCalculatorSalaries();
    _cache.StringSet("CalculatorSalaries", (string)salaries);
    //     ^^^^^^^^^ this