コード例 #1
0
        //Private common store method.
        private string store(string command, string key, bool keyIsChecked, object value, uint hash, int expiry, ulong unique)
        {
            if (!keyIsChecked)
            {
                checkKey(key);
            }

            return(serverPool.Execute <string>(hash, "", 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 "";
                }

                //Create commandline
                string commandline = "";
                switch (command)
                {
                case "set":
                case "add":
                case "replace":
                    commandline = command + " " + keyPrefix + key + " " + (ushort)type + " " + expiry + " " + bytes.Length + "\r\n";
                    break;

                case "append":
                case "prepend":
                    commandline = command + " " + keyPrefix + key + " 0 0 " + bytes.Length + "\r\n";
                    break;

                case "cas":
                    commandline = command + " " + keyPrefix + key + " " + (ushort)type + " " + expiry + " " + bytes.Length + " " + unique + "\r\n";
                    break;
                }

                //Write commandline and serialized object.
                socket.Write(commandline);
                socket.Write(bytes);
                socket.Write("\r\n");
                return socket.ReadResponse();
            }));
        }
コード例 #2
0
ファイル: RedisClient.cs プロジェクト: ithanshui/cyqdata
        private bool Set(string command, string key, bool keyIsChecked, object value, uint hash, int expirySeconds)
        {
            if (!keyIsChecked)
            {
                checkKey(key);
            }

            string result = serverPool.Execute <string>(hash, "", delegate(MSocket socket)
            {
                SerializedType type;
                byte[] bytes;
                byte[] typeBit = new byte[1];
                try
                {
                    bytes      = Serializer.Serialize(value, out type, CompressionThreshold);
                    typeBit[0] = (byte)type;
                }
                catch (Exception e)
                {
                    logger.Error("Error serializing object for key '" + key + "'.", e);
                    return("");
                }
                CheckDB(socket, hash);
                using (RedisCommand cmd = new RedisCommand(socket, 3, command))
                {
                    cmd.WriteKey(keyPrefix + key);
                    cmd.WriteValue(typeBit, bytes);
                    result = socket.ReadResponse();
                    if (result[0] != '-')
                    {
                        if (expirySeconds > 0)
                        {
                            cmd.Reset(3, "EXPIRE");
                            cmd.WriteKey(keyPrefix + key);
                            cmd.WriteValue(expirySeconds.ToString());
                            result = socket.ReadResponse();
                        }
                    }
                    return(result);
                }
            });

            return(!string.IsNullOrEmpty(result));
        }