Exemple #1
0
        public async Task Set(string key, byte[] value, TimeSpan?expireIn)
        {
            await _semaphore.WaitAsync();

            var keyBytes = Encoding.UTF8.GetBytes(key);
            var request  = new MemcachedRequest(Enums.Opcode.Set, keyBytes, NextOpaque, value, TypeCode.Object, expireIn);

            await CommandWithNoResult(request);
        }
Exemple #2
0
        public async Task Delete(string key)
        {
            await _semaphore.WaitAsync();

            var keyBytes = Encoding.UTF8.GetBytes(key);
            var request  = new MemcachedRequest(Enums.Opcode.Delete, keyBytes, NextOpaque);

            await CommandWithNoResult(request);
        }
Exemple #3
0
        public async Task <byte[]> Get(string key)
        {
            await _semaphore.WaitAsync();

            var keyBytes = Encoding.UTF8.GetBytes(key);
            var request  = new MemcachedRequest(Enums.Opcode.Get, keyBytes, NextOpaque);

            return(await CommandWithResult(request));
        }
Exemple #4
0
        private async Task <MemcachedResponse> ExecuteCommand(MemcachedRequest request)
        {
            await _protocolWriter.WriteAsync(_memcachedMessageWriter, request);

            var result = await _protocolReader.ReadAsync(_memcachedMessageReader);

            _protocolReader.Advance();
            return(result.Message);
        }
Exemple #5
0
        private async Task CommandWithNoResult(MemcachedRequest request)
        {
            try
            {
                var result = await ExecuteCommand(request);

                if (result.Header.ResponseStatus != ResponseStatus.NoError)
                {
                    throw new Exception(result.Header.ResponseStatus.ToString());
                }
            }
            finally
            {
                _semaphore.Release();
            }
        }
Exemple #6
0
        private async Task <byte[]> CommandWithResult(MemcachedRequest request)
        {
            try
            {
                var result = await ExecuteCommand(request);

                if (result.Header.ResponseStatus == ResponseStatus.NoError)
                {
                    return(result.Data.ToArray());
                }
                else
                {
                    throw new Exception(result.Header.ResponseStatus.ToString());
                }
            }
            finally
            {
                _semaphore.Release();
            }
        }