コード例 #1
0
 private BinaryResponse dispatchAndExpectResponse(uint hash, BinaryRequest req)
 {
     return(serverPool.Execute(hash, null, (socket) => {
         socket.Write(req);
         return socket.ReadBinaryResponse();
     }));
 }
コード例 #2
0
        protected override bool delete(string key, bool keyIsChecked, uint hash, int time)
        {
            if (!keyIsChecked)
            {
                checkKey(key);
            }
            BinaryRequest req = new BinaryRequest()
            {
                Opcode      = Opcodes.Delete,
                KeyAsString = KeyPrefix + key,
            };
            var response = dispatchAndExpectResponse(hash, req);

            if (response == null || response.ResponseCode != ErrorCode.None)
            {
                return(false);
            }
            return(true);
        }
コード例 #3
0
        protected override object get(string command, string key, bool keyIsChecked, uint hash, out ulong unique)
        {
            if (!keyIsChecked)
            {
                checkKey(key);
            }
            BinaryRequest req = new BinaryRequest()
            {
                Opcode      = Opcodes.Get,
                KeyAsString = KeyPrefix + key,
            };
            var response = dispatchAndExpectResponse(hash, req);

            unique = 0;
            if (response == null || response.ResponseCode != ErrorCode.None)
            {
                return(null);
            }
            return(Deserialize(response, key));
        }
コード例 #4
0
        private BinaryResponse store(string command, string key, bool keyIsChecked, object value, uint hash, int expiry,
                                     ulong unique)
        {
            if (!keyIsChecked)
            {
                checkKey(key);
            }

            return(serverPool.Execute <BinaryResponse>(hash, null, delegate(PooledSocket socket) {
                SerializedType type;
                byte[] bytes;

                //Serialize object efficiently, store the datatype marker in the flags property.
                try {
                    bytes = Serializer.Serialize(value, out type, CompressionThreshold);
                }
                catch (Exception e) {
                    //If serialization fails, return false;

                    logger.Error("Error serializing object for key '" + key + "'.", e);
                    return null;
                }
                byte[] extras = new byte[8];
                Array.Copy(BinaryMessage.NetworkOrder(BitConverter.GetBytes((int)type)), 0, extras, 0, 4);
                Array.Copy(BinaryMessage.NetworkOrder(BitConverter.GetBytes(expiry)), 0, extras, 4, 4);
                var request = new BinaryRequest()
                {
                    Extras = extras,
                    Value = bytes,
                    KeyAsString = keyPrefix + key
                };
                //Create commandline
                switch (command)
                {
                case "set":
                    request.Opcode = Opcodes.Set;
                    break;

                case "add":
                    request.Opcode = Opcodes.Add;
                    break;

                case "replace":
                    request.Opcode = Opcodes.Replace;
                    break;

                case "append":
                    extras = new byte[] {};
                    request.Opcode = Opcodes.Append;
                    break;

                case "prepend":
                    extras = new byte[] {};
                    request.Opcode = Opcodes.Prepend;
                    break;

                case "cas":
                    request.Opcode = Opcodes.Set;
                    request.CAS = unique;
                    break;
                }

                //Write commandline and serialized object.
                socket.Write(request);
                return socket.ReadBinaryResponse();
            }));
        }
コード例 #5
0
        private BinaryResponse[] executeBulk(BinaryRequest quietTemplate, BinaryRequest finalRequestTemplate,
                                             string[] keys, uint[] hashes)
        {
            EnsureAlignment(keys, hashes);

            //Group the keys/hashes by server(pool)
            Dictionary <SocketPool, Dictionary <string, List <int> > > dict =
                new Dictionary <SocketPool, Dictionary <string, List <int> > >();

            for (int i = 0; i < keys.Length; i++)
            {
                Dictionary <string, List <int> > getsForServer;
                SocketPool pool = serverPool.GetSocketPool(hashes[i]);
                if (!dict.TryGetValue(pool, out getsForServer))
                {
                    dict[pool] = getsForServer = new Dictionary <string, List <int> >();
                }

                List <int> positions;
                if (!getsForServer.TryGetValue(keys[i], out positions))
                {
                    getsForServer[keyPrefix + keys[i]] = positions = new List <int>();
                }
                positions.Add(i);
            }

            //Get the values
            BinaryResponse[] returnValues = new BinaryResponse[keys.Length];
            foreach (KeyValuePair <SocketPool, Dictionary <string, List <int> > > kv in dict)
            {
                Dictionary <UInt32, string> opaqueToKey = new Dictionary <UInt32, string>();
                serverPool.Execute(kv.Key, delegate(PooledSocket socket) {
                    int i     = 0;
                    int total = kv.Value.Count;
                    socket.ResetSequence();
                    UInt32 sequence    = socket.NextSequence;
                    UInt32 minSequence = sequence;
                    foreach (var value in kv.Value)
                    {
                        BinaryRequest req;
                        req                   = i++ + 1 < total ? quietTemplate.Clone() : finalRequestTemplate;
                        req.KeyAsString       = value.Key;
                        req.Opaque            = sequence;
                        opaqueToKey[sequence] = value.Key;
                        sequence              = socket.NextSequence;
                        socket.Write(req);
                    }
                    var responses = socket.ReadBinaryResponseBetween(minSequence, sequence - 1);

                    //Read values, one by one
                    foreach (var response in responses)
                    {
                        var gottenKey = opaqueToKey[response.Opaque];
                        foreach (int position in kv.Value[gottenKey])
                        {
                            returnValues[position] = response;
                        }
                    }
                });
            }
            return(returnValues);
        }