简体   繁体   中英

How to get Unicode string from redis cache using StackExchange.Redis in c#?

I am using StackExchange.Redis library to work with redis. I have set Unicode string value as below.

db.StringSet(key,"धन्यवाद");

And I am trying to get Unicode string using

db.StringGet(key);

But getting ????? instead of Unicode string. Redis stores Unicode string value in below format.

\xe0\xa4\xa7\xe0\xa4\xa8\xe0\xa5\x8d\xe0\xa4\xaf\xe0\xa4\xb5\xe0\xa4\xbe\xe0\xa4\xa6

RedisValue has an implicit cast operator of type string. It encodes it as an UTF-8 byte array before sending it. If you have your string in Unicode you should obtain the bytes by yourself.

byte[] data = Encoding.Unicode.GetBytes(value);
db.StringSet(key, data);
...
data = db.StringGet(key);
value = Encoding.Unicode.GetString(data);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM