Пример #1
0
        public async Task Calculate(string remark = null)
        {
            LastCalculation = DateTime.UtcNow;
            Console.WriteLine($"CALCULATING... ({LastCalculation:HH:mm:ss.fff})" +
                              $" #{Thread.CurrentThread.ManagedThreadId}" +
                              $"{(remark != null ? $" [{remark}]" : string.Empty)}");
            var sw = new Stopwatch();

            sw.Start();

            IEnumerable <dynamic> results = await SqlConnection.QueryAsync(
                "SELECT Nominee, sum(Amount) as Amount FROM Votes GROUP BY Nominee");

            HashEntry FetchHash(string nominee)
            {
                return(new HashEntry(
                           nominee,
                           ((int?)results.FirstOrDefault(row => row.Nominee == nominee)?.Amount ?? 0).ToString()));
            }

            RedisDb.HashSet(
                RedisKeys.Results,
                new[]
            {
                FetchHash(Nominees.DotNet),
                FetchHash(Nominees.DotNetCore),
                FetchHash(Nominees.JavaScript)
            },
                CommandFlags.FireAndForget);

            // TODO: Fetch and store the results per Nominee in Redis.

            sw.Stop();
            Console.WriteLine($"Done.          (in {sw.Elapsed})");
        }
Пример #2
0
        private void Serialize <T>(T obj)
        {
            Type t = typeof(T);

            PropertyInfo[] properties = t.GetProperties();

            HashEntry[] hashEntries = new HashEntry[properties.Length];
            int         i           = 0;

            foreach (PropertyInfo property in properties)
            {
                hashEntries[i] = new HashEntry(property.Name, JsonConvert.SerializeObject(property.GetValue(obj)));
                i++;
            }

            RedisDb.HashSet(t.Name, hashEntries);
        }
Пример #3
0
        private void SaveChanges <T>(T obj)
        {
            Type t = typeof(T);

            PropertyInfo[] properties  = t.GetProperties();
            FieldInfo[]    fields      = t.GetFields();
            HashEntry[]    hashEntries = new HashEntry[properties.Length + fields.Length];
            int            i           = 0;

            foreach (PropertyInfo property in properties)
            {
                hashEntries[i] = new HashEntry(property.Name, JsonConvert.SerializeObject(property.GetValue(obj)));
                i++;
            }
            string guid = t.GetProperty("Identifier").GetValue(obj).ToString();

            RedisDb.SortedSetAdd(t.GetTypeInfo().Name, guid, RedisDb.SortedSetLength(t.GetTypeInfo().Name));
            RedisDb.HashSet(guid, hashEntries);
        }
Пример #4
0
 /// <summary>
 /// Adds the specified key.
 /// </summary>
 /// <param name="key">The key.</param>
 /// <param name="value">The value.</param>
 /// <param name="when">The when.</param>
 /// <returns></returns>
 public bool Add(TKey key, TValue value, When when)
 {
     return(RedisDb.HashSet(RedisKey, KeySerializer.Serialize(key), ValueSerializer.Serialize(value), when: when));
 }
Пример #5
0
 /// <summary>
 /// Adds the multiple.
 /// </summary>
 /// <param name="items">The items.</param>
 public void AddMultiple(IEnumerable <KeyValuePair <TKey, TValue> > items)
 {
     RedisDb.HashSet(RedisKey, items.Select(i => new HashEntry(KeySerializer.Serialize(i.Key), ValueSerializer.Serialize(i.Value))).ToArray());
 }