locked
How to filter redis cache data? RRS feed

  • Question

  • User-2048214978 posted

    I want to get latest 5 minutes data from redis cache.

    I can able to achieve this using c# List operation like below but I want to do the same from Redis Cache.

    c# code which works fine - 

    inputList is the list of MyObject

    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
                   });

    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;
            }
        }

    Monday, October 12, 2020 1:47 PM

All replies

  • User475983607 posted

    I want to get latest 5 minutes data from redis cache.

    Use a standard Where to filter the records by the current DateTime.   If you are trying to get the last 5 minutes from the max time stamp then do two LINQ queries.  One to get the max timestamp and another to filter the cache.

    Monday, October 12, 2020 2:14 PM
  • User-2048214978 posted

    could you please help me with code snippet for redis cache list?

    Monday, October 12, 2020 2:31 PM
  • User475983607 posted

    could you please help me with code snippet for redis cache list?

    As far as I can tell your code filters a collection not redis cache.

    DateTime fiveMinutesAgo = DateTime.Now.AddMinutes(-5);
    inputList.Where(m => m.TimeStamp > fiveMinutesAgo);

    Can you explain the problem you are trying to solve?  It seems like you are writing a cache pattern inside a cache service rather than just using the cache service.

    Monday, October 12, 2020 3:02 PM