gpt4 book ai didi

c# - 如何将Redis Value []转换为反序列化对象C#?

转载 作者:行者123 更新时间:2023-12-03 06:42:10 26 4
gpt4 key购买 nike

我正在对Webjob中的列表进行排序。
当我使用C#List时,它工作正常。但是为了提高性能,我将数据以列表的形式保存到Redis缓存中。
我的最终目标是仅取出最近5分钟的最新数据。
工作的C#代码-

public class MyObject
{
public uint InstrumentID { get; set; }
public decimal Close { get; set; }
public decimal High { get; set; }
public decimal Low { get; set; }
public decimal Open { get; set; }
public DateTime TimeStamp { get; set; }
public uint Volume { get; set; }

public DateTime Created { get; } = DateTime.Now;
public DateTime Ttl { get; } = DateTime.Now.AddMinutes(5);
public DateTime? Persisted { get; set; }

public bool IsDead => DateTime.Now > Ttl;
public bool IsPersisted => Persisted.HasValue;
public bool TimeToPersist => IsPersisted == false && DateTime.Now > Created.AddMinutes(5);

public DateTime GetStartOfPeriodByMins(int numMinutes)
{
int oldMinutes = TimeStamp.Minute;
int newMinutes = (oldMinutes / numMinutes) * numMinutes;

DateTime startOfPeriod = new DateTime(TimeStamp.Year, TimeStamp.Month, TimeStamp.Day, TimeStamp.Hour, newMinutes, 0);

return startOfPeriod;
}
}

var inputList = new SortedSet<MyObject>(new MyObjectComparer());

inputList.Add(new MyObject() { TimeStamp = DateTime.Now, Open = 9, High = 12, Low = 8, Close = 11, InstrumentID = 2526 });
Thread.Sleep(10000);
inputList.Add(new MyObject() { TimeStamp = DateTime.Now, Open = 9, High = 12, Low = 8, Close = 11, InstrumentID = 2526 });
Thread.Sleep(10000);
inputList.Add(new MyObject() { TimeStamp = DateTime.Now, Open = 9, High = 12, Low = 8, Close = 11, InstrumentID = 2526 });
Thread.Sleep(50000);
inputList.Add(new MyObject() { TimeStamp = DateTime.Now, Open = 9, High = 12, Low = 8, Close = 11, InstrumentID = 2526 });

var resultSet = inputList
.GroupBy(i => i.GetStartOfPeriodByMins(5))
.Select(gr =>
new
{
StartOfPeriod = gr.Key,
Min = gr.Min(item => item.Open),
Max = gr.Max(item => item.Open),
Open = gr.OrderBy(item => item.TimeStamp).First().Open,
Close = gr.OrderBy(item => item.TimeStamp).Last().Open
});
现在,我将相同的记录不断插入到Redis缓存中。虽然试图获取最后5分钟的数据,但我还是想使用相同的 GetStartOfPeriodByMins概念,但是它需要MyObject类的列表,并且redis返回RedisValue []。
Redis代码-使用StackExchange.Redis包
var cache = RedisConnectorHelper.Connection.GetDatabase();

//int i = 0;
for (int i = 0; i < 10000; i++)
{
var tickDataHis = new MyObject()
{
InstrumentID = 2526,
Close = 14 + i,
High = 16 + i,
Low = 11 + i,
Open = 12 + i,
TimeStamp = DateTime.Now,
Volume = 11111
};

// insert into redis
cache.ListRightPush("tickData", JsonConvert.SerializeObject(tickDataHis));

Thread.Sleep(3000);
}

var inputList = cache.ListRange("tickData");

or is there any other way to get latest 5 minutes data from rediscache?


enter image description here

最佳答案

我过去使用Redis来存储时间序列数据。为了优化数据检索,我使用了一个排序的集合(在这种情况下,有多个集合,但是概念是相同的),其中分数是记录数据的unix时间戳,并使用Newtonsoft.Json序列化了数据图书馆。
代码是这样的:

var myData = new MyObject() { SomeProperty = "my text" };
long dataTimestamp = DateTimeOffset.Now.ToUnixTimeMilliseconds();
string serializedData = Newtonsoft.Json.JsonConvert.Convert(myData);
redisDb.SortedSetAdd("mySortedSet", dataTimestamp, serializedData);
这样,如果您只想检索 过去5分钟的数据,则可以使用SortedSetRangeByScore方法直接过滤从Redis加载的数据,并传递“now-5 minutes”作为起始分数,以便可以反序列化仅需要的内容(与反序列化整个列表相比,这当然要便宜一些):
var minutesToTake = 5;
long startingTime = DateTimeOffset.Now.AddMinutes(-minutesToTake).ToUnixTimeMilliseconds();
RedisValue[] redisData = redisDb.SortedSetRangeByScore("mySortedSet", startingTime);
之后,您可以借助linq轻松地反序列化数据(考虑到RedisValue实现了运算符到字符串的转换):
MyObject[] myData = redisData.Select(d => JsonConvert.DeserializeObject<MyObject>(d)).ToArray();
编辑:我没有使用@Hasan EmrahSüngü建议的软件包。也许它更有效,我只是在解释我当时所做的事情。
编辑2 :redisDb是我的StackExchange.Redis.IDatabase实例。
编辑3 :有用的引用链接:

Redis上的
  • 数据持久性:https://redis.io/topics/data-types-intro#redis-expires-keys-with-limited-time-to-live
  • Redis排序集:https://redis.io/topics/data-types-intro#redis-sorted-sets
  • 时间序列的排序集:https://redislabs.com/redis-best-practices/time-series/sorted-set-time-series/
  • 关于c# - 如何将Redis Value []转换为反序列化对象C#?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64318932/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com