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));
            }
        }
示例#2
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));
        }
示例#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));
            }
        }
        public async Task <TItem> ReadAsync(IDatabaseAsync redisDatabase, TKey key, TimeSpan?resetExpiry)
        {
            var redisKey = _keygen.GetKey(key);
            var item     = await redisDatabase.StringGetAsync(redisKey).ConfigureAwait(false);

            if (resetExpiry.HasValue)
            {
                await redisDatabase.KeyExpireAsync(redisKey, resetExpiry.Value, CommandFlags.FireAndForget).ConfigureAwait(false);
            }

            return(string.IsNullOrEmpty(item) ? default(TItem) : JsonConvert.DeserializeObject <TItem>(item));
        }
示例#5
0
        public Task <bool> WriteAsync(IDatabaseAsync redisDatabase, TKey key, TItem item, TimeSpan?expiry)
        {
            var redisValue = JsonConvert.SerializeObject(item);

            return(redisDatabase.StringSetAsync(_keygen.GetKey(key), redisValue, expiry));
        }
示例#6
0
 public Task <bool> DeleteAsync(IDatabaseAsync redisDatabase, TKey key)
 {
     return(redisDatabase.KeyDeleteAsync(_keygen.GetKey(key)));
 }