/// <summary> /// 执行命令 /// </summary> /// <param name="cmd">指令</param> /// <param name="args">参数</param> /// <param name="port">服务器端口</param> /// <returns></returns> private static string Exec(CmdEnum cmd, List <string> args, int port) { using (var memCached = MemCached.Create(new IPEndPoint(IPAddress.Loopback, port))) { switch (cmd) { case CmdEnum.get: if (args.Count != 1) { return(string.Empty); } try { var result = memCached.Get(args[0]); if (result.Status != OprationStatus.No_Error) { return(result.Status.ToString()); } return(result.Value.ToString()); } catch { return("The Value Is A Unknow Object"); } case CmdEnum.set: if (args.Count != 3) { return(string.Empty); } var state = memCached.Set(args[0], args[1], TimeSpan.FromSeconds(int.Parse(args[2]))); return(state.ToString()); case CmdEnum.add: if (args.Count != 3) { return(string.Empty); } state = memCached.Add(args[0], args[1], TimeSpan.FromSeconds(int.Parse(args[2]))); return(state.ToString()); case CmdEnum.replace: if (args.Count != 3) { return(string.Empty); } state = memCached.Replace(args[0], args[1], TimeSpan.FromSeconds(int.Parse(args[2]))); return(state.ToString()); case CmdEnum.delete: if (args.Count != 1) { return(string.Empty); } state = memCached.Delete(args[0]); return(state.ToString()); case CmdEnum.flush: if (args.Count != 1) { return(string.Empty); } memCached.Flush(TimeSpan.FromSeconds(int.Parse(args[0]))); return("OK"); case CmdEnum.touch: if (args.Count != 2) { return(string.Empty); } state = memCached.Touch(args[0], TimeSpan.FromSeconds(int.Parse(args[1]))); return(state.ToString()); case CmdEnum.stat: var lines = memCached.Stat().Select(item => string.Format("{0} => {1}", item.Key, item.Value)); return(string.Join(Environment.NewLine, lines)); default: return(string.Empty); } } }
/// <summary> /// 执行命令 /// </summary> /// <param name="cmd">指令</param> /// <param name="args">参数</param> /// <param name="port">服务器端口</param> /// <returns></returns> private static string Exec(CmdEnum cmd, List <string> args, int port) { var keyNotExists = "The Key Is Not Exists"; var keyHasExists = "The Key Has Exists"; var ok = "OK"; using (var memCached = MemCached.Create(new IPEndPoint(IPAddress.Loopback, port))) { switch (cmd) { case CmdEnum.get: if (args.Count != 1) { return(string.Empty); } try { var result = memCached.Get(args[0]); if (result.Status == false) { return(keyNotExists); } return(result.Value.ToString()); } catch { return("The Value Is A Unknow Object"); } case CmdEnum.set: if (args.Count != 3) { return(string.Empty); } var state = memCached.Set(args[0], args[1], TimeSpan.FromSeconds(int.Parse(args[2]))); return(state ? ok : keyNotExists); case CmdEnum.add: if (args.Count != 3) { return(string.Empty); } state = memCached.Add(args[0], args[1], TimeSpan.FromSeconds(int.Parse(args[2]))); return(state ? ok : keyHasExists); case CmdEnum.replace: if (args.Count != 3) { return(string.Empty); } state = memCached.Replace(args[0], args[1], TimeSpan.FromSeconds(int.Parse(args[2]))); return(state ? ok : keyNotExists); case CmdEnum.delete: if (args.Count != 1) { return(string.Empty); } state = memCached.Delete(args[0]); return(state ? ok : keyNotExists); case CmdEnum.flush: if (args.Count != 1) { return(string.Empty); } memCached.Flush(TimeSpan.FromSeconds(int.Parse(args[0]))); return(ok); default: return(string.Empty); } } }