Exemple #1
0
        /// <summary>
        /// Reads the BLOB, updates the Redis Cache in bulk manner based on batch size.
        /// </summary>
        /// <param name="myBlob">BLOB Stream reference</param>
        /// <param name="redisCacheHelper">Redis Cache loader helper</param>
        /// <param name="batchSize">batch size for bulk load</param>
        /// <returns></returns>
        private static async Task <int> ReadBlobAndUpdateRedisCache(Stream myBlob, RedisCacheHelper redisCacheHelper, int batchSize)
        {
            int keysWrittenToCache = 0;

            /* Read contents of the updated BLOB */
            using (var reader = new StreamReader(myBlob))
            {
                var counter = 0;
                var batch   = new Dictionary <string, string>();

                while (!reader.EndOfStream)
                {
                    // assuming data format of xxxx,yyyyy
                    var line = reader.ReadLine()?.Split(new char[] { ',' });

                    /* Ignore lines which could not be read or do not have at least one delimiter. */
                    if (line != null && line.Length > 1)
                    {
                        batch.Add(line[0], line[1]);
                        counter++;
                    }

                    /* if the read count has reached batch size, write the key-value pair to Redis Cache
                     * clear the batch and reset the read counter.
                     */
                    if (counter == batchSize)
                    {
                        await redisCacheHelper.BatchWriteKeyValuePairAsync(batch);

                        keysWrittenToCache += counter;
                        counter             = 0;
                        batch.Clear();
                    }
                }

                /*
                 * if items are still left in the batch, as count could not meet
                 * the batch size, write remaining key-value pair to Redis Cache.
                 */
                if (batch.Count > 0)
                {
                    await redisCacheHelper.BatchWriteKeyValuePairAsync(batch);

                    keysWrittenToCache += batch.Count;
                    batch.Clear();
                }
            }
            return(keysWrittenToCache);
        }