Exemple #1
0
        public async Task <TItem> ReadAsync(IDatabaseAsync redisDatabase, TKey key, TimeSpan?resetExpiry)
        {
            var headerKey = _keygen.GetKey(key);
            var chunkGets = new List <Task <RedisValue> >();

            using (var redisLock = _lockFactory.GetLockInstance(headerKey))
            {
                await redisLock.AcquireAsync().ConfigureAwait(false);


                var header = await GetAndUpdateExpiry(redisDatabase, headerKey, resetExpiry).ConfigureAwait(false);

                if (header.IsNullOrEmpty)
                {
                    return(default(TItem));
                }


                var totalChunks = (int)header;

                for (var chunkIndex = 0; chunkIndex < totalChunks; chunkIndex++)
                {
                    var chunkKey = _keygen.GetKey(key, chunkIndex.ToString());

                    chunkGets.Add(GetAndUpdateExpiry(redisDatabase, chunkKey, resetExpiry));
                }

                await Task.WhenAll(chunkGets).ConfigureAwait(false);
            }

            var jsonData = string.Join("", chunkGets.Select(cg => cg.Result));

            return(string.IsNullOrEmpty(jsonData) ? default(TItem) : JsonConvert.DeserializeObject <TItem>(jsonData));
        }
        public async Task <bool> DeleteAsync(IDatabaseAsync redisDatabase, TKey key)
        {
            var headerKey    = _keygen.GetKey(key);
            var chunkDeletes = new List <Task <bool> >();

            using (var redisLock = _lockFactory.GetLockInstance(headerKey))
            {
                await redisLock.AcquireAsync().ConfigureAwait(false);

                var header = await redisDatabase.StringGetAsync(headerKey).ConfigureAwait(false);

                if (header.IsNullOrEmpty)
                {
                    return(false);
                }

                var chunkCount = (int)header;
                for (var chunkIndex = 0; chunkIndex < chunkCount; chunkIndex++)
                {
                    var chunkKey = _keygen.GetKey(key, chunkIndex.ToString());

                    chunkDeletes.Add(redisDatabase.KeyDeleteAsync(chunkKey));
                }

                await Task.WhenAll(chunkDeletes).ConfigureAwait(false);

                return(await redisDatabase.KeyDeleteAsync(headerKey).ConfigureAwait(false));
            }
        }
Exemple #3
0
        public async Task <bool> WriteAsync(IDatabaseAsync redisDatabase, TKey key, TItem item, TimeSpan?expiry)
        {
            var         headerKey         = _keygen.GetKey(key);
            var         chunkIndex        = 0;
            Task <bool> previousWriteTask = null;

            using (var redisLock = _lockFactory.GetLockInstance(headerKey))
            {
                await redisLock.AcquireAsync().ConfigureAwait(false);

                using (var bufferedWriter = new BufferedTextWriter(_chunkSize))
                {
                    bufferedWriter.BufferFull += async(s, e) =>
                    {
                        var bufferContent = e.Buffer.ToString();
                        var myChunkIndex  = chunkIndex++;

                        e.Buffer.Clear();

                        if (previousWriteTask != null)
                        {
                            await previousWriteTask.ConfigureAwait(false);
                        }

                        previousWriteTask = WriteToRedisAsync(redisDatabase, key, expiry, bufferContent, myChunkIndex);
                    };

                    JsonSerializer.CreateDefault().Serialize(bufferedWriter, item);

                    if (bufferedWriter.Buffer.Length > 0)
                    {
                        if (previousWriteTask != null)
                        {
                            await previousWriteTask.ConfigureAwait(false);

                            previousWriteTask = null;
                        }

                        await WriteToRedisAsync(redisDatabase, key, expiry, bufferedWriter.Buffer.ToString(), chunkIndex ++).ConfigureAwait(false);
                    }
                }

                return(await redisDatabase.StringSetAsync(headerKey, chunkIndex, expiry).ConfigureAwait(false));
            }
        }