Exemple #1
0
        /// <summary>
        /// Retrieves stats for passed in servers (or all servers).
        ///
        /// Returns a map keyed on the servername.
        /// The value is another map which contains stats
        /// with stat name as key and value as value.
        /// </summary>
        /// <param name="servers">string array of servers to retrieve stats from, or all if this is null</param>
        /// <returns>Stats map</returns>
        public Hashtable Stats(ArrayList servers)
        {
            // get SockIOPool instance
            SockIOPool pool = SockIOPool.GetInstance(_poolName);

            // return false if unable to get SockIO obj
            if (pool == null)
            {
                return(null);
            }

            // get all servers and iterate over them
            if (servers == null)
            {
                servers = pool.Servers;
            }

            // if no servers, then return early
            if (servers == null || servers.Count <= 0)
            {
                return(null);
            }

            // array of stats Hashtables
            Hashtable statsMaps = new Hashtable();

            for (int i = 0; i < servers.Count; i++)
            {
                SockIO sock = pool.GetConnection((string)servers[i]);
                if (sock == null)
                {
                    continue;
                }

                // build command
                string command = "stats\r\n";

                try
                {
                    sock.Write(UTF8Encoding.UTF8.GetBytes(command));
                    sock.Flush();

                    // map to hold key value pairs
                    Hashtable stats = new Hashtable();

                    // loop over results
                    while (true)
                    {
                        string line = sock.ReadLine();

                        if (line.StartsWith(STATS))
                        {
                            string[] info = line.Split(' ');
                            string   key  = info[1];
                            string   val  = info[2];

                            stats[key] = val;
                        }
                        else if (END == line)
                        {
                            break;
                        }

                        statsMaps[servers[i]] = stats;
                    }
                }
                catch (IOException e)
                {
                    try
                    {
                        sock.TrueClose();
                    }
                    catch (IOException)
                    {
                    }

                    sock = null;
                }

                if (sock != null)
                {
                    sock.Close();
                }
            }

            return(statsMaps);
        }
Exemple #2
0
        /// <summary>
        /// Invalidates the entire cache.
        ///
        /// Will return true only if succeeds in clearing all servers.
        /// If pass in null, then will try to flush all servers.
        /// </summary>
        /// <param name="servers">optional array of host(s) to flush (host:port)</param>
        /// <returns>success true/false</returns>
        public bool FlushAll(ArrayList servers)
        {
            // get SockIOPool instance
            SockIOPool pool = SockIOPool.GetInstance(_poolName);

            // return false if unable to get SockIO obj
            if (pool == null)
            {
                return(false);
            }

            // get all servers and iterate over them
            if (servers == null)
            {
                servers = pool.Servers;
            }

            // if no servers, then return early
            if (servers == null || servers.Count <= 0)
            {
                return(false);
            }

            bool success = true;

            for (int i = 0; i < servers.Count; i++)
            {
                SockIO sock = pool.GetConnection((string)servers[i]);
                if (sock == null)
                {
                    success = false;
                    continue;
                }

                // build command
                string command = "flush_all\r\n";

                try
                {
                    sock.Write(UTF8Encoding.UTF8.GetBytes(command));
                    sock.Flush();

                    // if we get appropriate response back, then we return true
                    string line = sock.ReadLine();
                    success = (OK == line)
                        ? success && true
                        : false;
                }
                catch (IOException e)
                {
                    try
                    {
                        sock.TrueClose();
                    }
                    catch (IOException ioe)
                    {
                    }

                    success = false;
                    sock    = null;
                }

                if (sock != null)
                {
                    sock.Close();
                }
            }

            return(success);
        }