Example #1
0
        protected override bool ExecuteAction()
        {
            PooledSocket socket = this.Socket;

            if (socket == null)
            {
                return(false);
            }

            socket.SendCommand("get " + this.HashedKey);

            GetResponse r = GetHelper.ReadItem(this.Socket);

            if (r != null)
            {
                this.result = this.ServerPool.Transcoder.Deserialize(r.Item);
                GetHelper.FinishCurrent(this.Socket);
            }

            return(true);
        }
Example #2
0
        protected override bool ExecuteAction()
        {
            // {hashed key -> normal key}: will be used when mapping the returned items back to the original keys
            Dictionary <string, string> hashedToReal = new Dictionary <string, string>(StringComparer.Ordinal);

            // {normal key -> hashed key}: we have to hash all keys anyway, so we better cache them to improve performance instead of doing the hashing later again
            Dictionary <string, string> realToHashed = new Dictionary <string, string>(StringComparer.Ordinal);

            IMemcachedKeyTransformer transformer = this.ServerPool.KeyTransformer;

            // and store them with the originals so we can map the returned items
            // to the original keys
            foreach (string s in this.keys)
            {
                string hashed = transformer.Transform(s);

                hashedToReal[hashed] = s;
                realToHashed[s]      = hashed;
            }

            // map each key to the appropriate server in the pool
            IDictionary <MemcachedNode, IList <string> > splitKeys = this.ServerPool.SplitKeys(this.keys);

            // we'll open 1 socket for each server
            List <PooledSocket> sockets = new List <PooledSocket>();

            try
            {
                string[] command;

                // send a 'gets' to each server
                foreach (KeyValuePair <MemcachedNode, IList <string> > kp in splitKeys)
                {
                    // gets <keys>
                    //
                    // keys: key key key key
                    command    = new string[kp.Value.Count + 1];
                    command[0] = "gets";
                    kp.Value.CopyTo(command, 1);

                    for (int i = 1; i < command.Length; i++)
                    {
                        command[i] = realToHashed[command[i]];
                    }

                    PooledSocket socket = kp.Key.Acquire();
                    if (socket == null)
                    {
                        continue;
                    }

                    sockets.Add(socket);
                    socket.SendCommand(String.Join(" ", command));
                }

                Dictionary <string, object> retval = new Dictionary <string, object>(StringComparer.Ordinal);
                Dictionary <string, ulong>  cas    = new Dictionary <string, ulong>(StringComparer.Ordinal);

                // process each response and build a dictionary from the results
                foreach (PooledSocket socket in sockets)
                {
                    try
                    {
                        GetResponse r;

                        while ((r = GetHelper.ReadItem(socket)) != null)
                        {
                            string originalKey = hashedToReal[r.Key];

                            retval[originalKey] = this.ServerPool.Transcoder.Deserialize(r.Item);
                            cas[originalKey]    = r.CasValue;
                        }
                    }
                    catch (NotSupportedException)
                    {
                        throw;
                    }
                    catch (Exception e)
                    {
                        log.Error(e);
                    }
                }

                this.result    = retval;
                this.casValues = cas;
            }
            finally
            {
                if (sockets != null)
                {
                    foreach (PooledSocket socket in sockets)
                    {
                        ((IDisposable)socket).Dispose();
                    }
                }
            }

            return(true);
        }