0

I was trying to retrieve cached data (redis) from redis using node js which was set using a .Net core 3.1 application

The code snippet which I was used to set cache is as below

nuget package : Microsoft.Extensions.Caching.StackExchangeRedis v 5.0.0 (Eventhough this is deprecated this is the version which is compatible with .Net core 3.1)

 public async Task SetCache(dynamic data, string cacheKey)
        {
            try
            {
                if (_redisConfig.EnableCache == true)
                {
                    string cachedDataString = JsonSerializer.Serialize(data);
                    var dataToCache = Encoding.UTF8.GetBytes(cachedDataString);
                    // Add the data into the cache
                    cacheKey = _redisConfig.Env + "_" + cacheKey;
                    await _cache.SetAsync(cacheKey, dataToCache, _options);
                }
            }
            catch (Exception)
            {
                return;
            }
        }

Below is the code I used to retrieve cache using node js

npm package: redis: "^4.5.1",


exports.getRedisData = async (key) => {
    try {
        return await redisClient.get(key);
    } catch (error) {
        console.log(error);
        return null
    }
        
 }

after executing the above solution the result I got is,

message: 'WRONGTYPE Operation against a key holding the wrong kind of value' Snap shot of the above error

Can anyone give me a clue to fix this ?

  • 1
    [This answer](https://stackoverflow.com/a/44444966/7687666) should inspire you. – Jason Pan Dec 23 '22 at 14:25
  • @JasonPan Thank you for the hint this is super helpful and you saved the day. – Ashan Maduranga Dec 24 '22 at 16:04
  • 1
    Does this answer your question? [WRONGTYPE Operation against a key holding the wrong kind of value php](https://stackoverflow.com/questions/37953019/wrongtype-operation-against-a-key-holding-the-wrong-kind-of-value-php) – Ian Kemp Dec 24 '22 at 20:59

1 Answers1

1

With the help of @JsonPan's answer, I was able to retrieve the data from Redis with a very simple change in my code. Here I have added my solution for anyone who may get the same issue as me.

By using hGet instead of get I was able to retrieve the Hashed data. (hGet is used to get Hashed data while get is used to retrieve string data)

exports.getRedisData = async (key) => {
    try {
        return await redisClient.hGet(key,"data");
    } catch (error) {
        console.log(error);
        return null
    }
        
 }