3

I'm using .Net core with StackExchangeRedis:

public void ConfigureServices(IServiceCollection services)
    {
        services.AddStackExchangeRedisCache(options =>
           {
               options.Configuration = "127.0.0.1:6379";
           });
        services.AddControllers();

    }

Later I inject the service and testing it via set/get :

private readonly IDistributedCache _cache;

        public MyRedisController(IDistributedCache cache)
        {
            _cache = cache;
        }

        [HttpGet]
        public async Task<string> Get()
        { 
             _cache.SetString("dd", "5");
            var a = await _cache.GetStringAsync("dd"); //5
            return a;


        }

The problem is that when I try to get the value in redis-cli, I see :

127.0.0.1:6379> get "dd"
(error) WRONGTYPE Operation against a key holding the wrong kind of value

After investigating, I see that it's stored as a hash:

enter image description here

Question:

How can I use StackExchangeRedis to store simple string types without the hash type ? I want a simple string. (get/set)

ps
I know I can get the value via: hget "dd" data. But I'm after storing simple string type.

1 Answer 1

2

Just to be clear: none of the code shown in the question is (directly) touching StackExchange.Redis; AddStackExchangeRedisCache is an ASP.NET layer wrapper by Microsoft, and is not part of StackExchange.Redis - it looks like it does other things to encapsulate additional features such as absolute/sliding expiration, although frankly (as observations):

  1. if -1 means "not set", they could have just not stored a hash field
  2. absolute expiration is supported directly in redis via EXPIRE

I'm happy to add an answer, but it will only be in terms of StackExchange.Redis:

// note: the multiplexer is disposable, but you usually keep this around
//  and reuse it between lots of operations - don't Connect each time
var muxer = ConnectionMultiplexer.Connect("127.0.0.1:6379");

// actually do things
var db = muxer.GetDatabase();
db.StringSet("dd", 5);
var value = db.StringGet("dd");
Console.WriteLine((int)value);

If you want to see what AddStackExchangeRedisCache is doing behind the scenes, you can use redis-cli MONITOR to see what commands it issues in your original code.

Sign up to request clarification or add additional context in comments.

2 Comments

Marc , how come MS called their package AddStackExchangeRedisCache (while not using Stackexchange.REdis)? isn't stackexchange from Stackoverflow ?
@Royi it uses it internally, as the layer between their code and the redis server, but: the MS API doesn't claim to be a 1:1 map to the redis API (that's the job of SE-Redis). So: when you say "store this string" on the MS API, it doesn't mean "using the string concept in redis", it just means "store it - how is up to you", and in this case, the MS API chooses to use a redis hash. Make sense? And yes, StackExchange.Redis is maintained by (and for) Stack Exchange / Stack Overflow

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.