csredis icon indicating copy to clipboard operation
csredis copied to clipboard

如何从redis里取BitArray

Open 1995liuwei opened this issue 3 years ago • 5 comments

如下代码所示,我将一个BitArray存入redis中,成功了;但是在get的时候报错了。 var key = "love"; var bit = new BitArray(10000); RedisHelper.Instance.Set(key, bit); var cacehBit = RedisHelper.Instance.Get<BitArray>(key); 错误信息 Newtonsoft.Json.JsonSerializationException:“Cannot create and populate list type System.Collections.BitArray. Path '', line 1, position 1.”

存在redis的BitArray image image 期望的帮助 如何从redis中正确取出BitArray

1995liuwei avatar Jun 20 '22 10:06 1995liuwei

本身有 setbit getbit 方法 http://redis.cn/commands/setbit.html

FreeRedis 是重新开发的 RedisClient :

https://github.com/2881099/FreeRedis

2881099 avatar Jun 20 '22 10:06 2881099

本身有 setbit getbit 方法 http://redis.cn/commands/setbit.html

FreeRedis 是重新开发的 RedisClient :

https://github.com/2881099/FreeRedis

不能直接使用setbit或getbit,我们是需要一口气从redis读出,要用setbit或getbit我们需要循环一个一个调用,效率不高。

1995liuwei avatar Jun 20 '22 11:06 1995liuwei

byte[] value = cli.Get<byte[]>(key);

这样可以得到原型

2881099 avatar Jun 20 '22 11:06 2881099

byte[] 是转换的时候有问题吗?前后两次BitArray大小不一致了 image

1995liuwei avatar Jun 20 '22 11:06 1995liuwei

这个byte[]不是BitArray,取决于存储时。

byte[] value = BitArray.ToByte(..) 假设有这个方法 cli.Set(key, value)

之后就可以Get<byte[]> 得到 value

2881099 avatar Jun 20 '22 11:06 2881099