Exemplo n.º 1
0
        public static ICachedumpOperationResult Cachedump(this MemcachedClient client, int slab = 1, int limit = 0)
        {
            #region 反射获取 IServerPool 属性值
            Type         t                   = client.GetType();
            PropertyInfo prop_Pool           = t.GetProperty("Pool", BindingFlags.Instance | BindingFlags.NonPublic);
            PropertyInfo prop_KeyTransformer = t.GetProperty("KeyTransformer", BindingFlags.Instance | BindingFlags.NonPublic);

            IServerPool pool = prop_Pool.GetValue(client) as IServerPool;
            IMemcachedKeyTransformer keyTransformer = prop_KeyTransformer.GetValue(client) as IMemcachedKeyTransformer;
            #endregion

            string key = string.Format("{0} {1}", slab, limit);

            var hashedKey = keyTransformer.Transform(key.Replace(" ", "_"));
            var node      = pool.Locate(hashedKey);

            ICachedumpOperationResultFactory CachedumpOperationResultFactory = new DefaultCachedumpOperationResultFactory();
            var result = CachedumpOperationResultFactory.Create();

            if (node != null)
            {
                var command       = new CachedumpOperation(key);
                var commandResult = node.Execute(command);

                if (commandResult.Success)
                {
                    result.Value = new Dictionary <string, string>();

                    command.Result.ForEach(item =>
                    {
                        //item is:
                        //ITEM testKey2 [3 b; 1600335168 s]
                        //ITEM key_name [value_length b; expire_time | access_time s]
                        // 0     1      2             3  4                         5
                        //1.2.2- 访问时间(timestamp)
                        //1.2.4+ 过期时间(timestamp)
                        //如果是永不过期的key,expire_time会显示为服务器启动的时间

                        string[] parts = item.Split(' ');
                        result.Value.Add(parts[1], string.Join(" ", parts, 2, parts.Length - 2));
                    });
                    result.Pass();
                    return(result);
                }
                else
                {
                    commandResult.Combine(result);
                    return(result);
                }
            }
            result.Fail("Unable to locate node");
            return(result);
        }