Exemple #1
0
        public static void test()
        {
            MemcachedClientConfiguration config = new MemcachedClientConfiguration();

            config.Servers.Add(new IPEndPoint(IPAddress.Loopback, 11211));
            config.Protocol = MemcachedProtocol.Binary;
            //config.Authentication.Type = typeof(PlainTextAuthenticator);
            //config.Authentication.Parameters["userName"] = "******";
            //config.Authentication.Parameters["password"] = "******";

            var mc = new MemcachedClient(config);

            for (var i = 0; i < 100; i++)
            {
                mc.Store(StoreMode.Set, "Hello", "World");
            }


            // simple multiget; please note that only 1.2.4 supports it (windows version is at 1.2.1)
            List <string> keys = new List <string>();

            for (int i = 1; i < 100; i++)
            {
                string k = "aaaa" + i + "--" + (i * 2);
                keys.Add(k);

                mc.Store(StoreMode.Set, k, i);
            }

            IDictionary <string, ulong>  cas;
            IDictionary <string, object> retvals = mc.Get(keys);

            List <string> keys2 = new List <string>(keys);

            keys2.RemoveRange(0, 50);

            IDictionary <string, object> retvals2 = mc.Get(keys2);

            retvals2 = mc.Get(keys2);

            ServerStats ms = mc.Stats();

            // store a string in the cache
            mc.Store(StoreMode.Set, "MyKey", "Hello World");

            // retrieve the item from the cache
            Console.WriteLine(mc.Get("MyKey"));

            Console.WriteLine(mc.Increment("num1", 1, 10));
            Console.WriteLine(mc.Increment("num1", 1, 10));
            Console.WriteLine(mc.Decrement("num1", 1, 14));

            // store some other items
            mc.Store(StoreMode.Set, "D1", 1234L);
            mc.Store(StoreMode.Set, "D2", DateTime.Now);
            mc.Store(StoreMode.Set, "D3", true);
            mc.Store(StoreMode.Set, "D4", new Person());

            mc.Store(StoreMode.Set, "D5", new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 });


            //mc2.Store(StoreMode.Set, "D1", 1234L);
            //mc2.Store(StoreMode.Set, "D2", DateTime.Now);
            //mc2.Store(StoreMode.Set, "D3", true);
            //mc2.Store(StoreMode.Set, "D4", new Product());

            Console.WriteLine("D1: {0}", mc.Get("D1"));
            Console.WriteLine("D2: {0}", mc.Get("D2"));
            Console.WriteLine("D3: {0}", mc.Get("D3"));
            Console.WriteLine("D4: {0}", mc.Get("D4"));

            byte[] tmp = mc.Get <byte[]>("D5");

            // delete them from the cache
            mc.Remove("D1");
            mc.Remove("D2");
            mc.Remove("D3");
            mc.Remove("D4");

            ServerStats stats = mc.Stats();

            Console.WriteLine(stats.GetValue(ServerStats.All, StatItem.ConnectionCount));
            Console.WriteLine(stats.GetValue(ServerStats.All, StatItem.GetCount));

            // add an item which is valid for 10 mins
            mc.Store(StoreMode.Set, "D4", new Person(), new TimeSpan(0, 10, 0));
        }
        public InstanceStatistics(System.Net.IPEndPoint serverEndPoint, Server server, Instance instance, ServerStats serverStats, State serviceState)
        {
            string memcachedFileVersion = null;

            if (File.Exists(Configuration.Default.MemcachedBinarySource) == true)
            {
                memcachedFileVersion = FileVersionInfo.GetVersionInfo(Configuration.Default.MemcachedBinarySource).FileVersion;
            }

            _serverName   = server.ServerName;
            _instanceName = instance.DisplayName;
            _serviceState = serviceState;

            Instance = instance;

            _statusIconIndex = StatusIconIndex.CommunicationError;
            _statusTooltip   = Constants.TooltipCommunicationError;

            if (serverStats != null && serverStats.GetRaw(serverEndPoint, StatItem.Version) != null)
            {
                foreach (StatItem statItem in Enum.GetValues(typeof(Enyim.Caching.Memcached.StatItem)))
                {
                    switch (statItem)
                    {
                    case StatItem.BytesRead:
                        _bytesRead = serverStats.GetValue(serverEndPoint, statItem);
                        break;

                    case StatItem.BytesWritten:
                        _bytesWritten = serverStats.GetValue(serverEndPoint, statItem);
                        break;

                    case StatItem.ConnectionCount:
                        _connectionCount = serverStats.GetValue(serverEndPoint, statItem);
                        break;

                    case StatItem.ConnectionStructures:
                        _connectionStructures = serverStats.GetValue(serverEndPoint, statItem);
                        break;

                    case StatItem.GetCount:
                        _getCount = serverStats.GetValue(serverEndPoint, statItem);
                        break;

                    case StatItem.GetHits:
                        _getHits = serverStats.GetValue(serverEndPoint, statItem);
                        break;

                    case StatItem.GetMisses:
                        _getMisses = serverStats.GetValue(serverEndPoint, statItem);
                        break;

                    case StatItem.ItemCount:
                        _itemCount = serverStats.GetValue(serverEndPoint, statItem);
                        break;

                    case StatItem.MaxBytes:
                        _maxBytes = serverStats.GetValue(serverEndPoint, statItem);
                        break;

                    case StatItem.ServerTime:
                        string serverTimeRaw = serverStats.GetRaw(serverEndPoint, statItem);
                        int    serverTimeSeconds;
                        if (serverTimeRaw != null && Int32.TryParse(serverTimeRaw, out serverTimeSeconds) == true)
                        {
                            _serverTime = new DateTime(1970, 1, 1).AddSeconds(serverTimeSeconds).ToLocalTime().ToString();
                        }
                        else
                        {
                            _serverTime = "<unknown>";
                        }
                        break;

                    case StatItem.SetCount:
                        _setCount = serverStats.GetValue(serverEndPoint, statItem);
                        break;

                    case StatItem.TotalConnections:
                        _totalConnections = serverStats.GetValue(serverEndPoint, statItem);
                        break;

                    case StatItem.TotalItems:
                        _totalItems = serverStats.GetValue(serverEndPoint, statItem);
                        break;

                    case StatItem.Uptime:
                        string uptimeRaw = serverStats.GetRaw(serverEndPoint, statItem);
                        int    uptimeSeconds;
                        if (uptimeRaw != null && Int32.TryParse(uptimeRaw, out uptimeSeconds) == true)
                        {
                            _uptime = TimeSpan.FromSeconds(uptimeSeconds).ToString();
                        }
                        else
                        {
                            _uptime = "<unknown>";
                        }
                        break;

                    case StatItem.UsedBytes:
                        _usedBytes = serverStats.GetValue(serverEndPoint, statItem);
                        break;

                    case StatItem.Version:
                        _version = serverStats.GetRaw(serverEndPoint, statItem);
                        break;
                    }
                }
            }

            if (_serviceState != State.Running && _serviceState != State.Unknown)
            {
                _statusIconIndex = StatusIconIndex.ServiceDown;
                _statusTooltip   = Constants.TooltipServiceDown;
            }
            else if (_serviceState == State.Unknown)
            {
                _statusIconIndex = StatusIconIndex.ServiceNonControllable;
                _statusTooltip   = Constants.TooltipServiceNonControllable;
            }
            else if (_version == null || _version == String.Empty)
            {
                _statusIconIndex = StatusIconIndex.CommunicationError;
                _statusTooltip   = Constants.TooltipCommunicationError;
            }
            else if (memcachedFileVersion != null && memcachedFileVersion != _version)
            {
                _statusIconIndex = StatusIconIndex.NeedsUpdate;
                _statusTooltip   = Constants.TooltipNeedsUpdate;
            }
            else
            {
                _statusIconIndex = StatusIconIndex.Up;
                _statusTooltip   = Constants.TooltipUp;
            }
        }