Ejemplo n.º 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);
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 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));
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 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();
            }
        }
Ejemplo n.º 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();
            }
        }