示例#1
0
        public static string Process(MiniRedisStorage storage, string command)
        {
            var parameters = new Regex(@"\s\s+")
                             .Replace(command, " ")
                             .Split(" ");

            if (parameters.Length == 0)
            {
                throw new ArgumentException("Command is not specified");
            }

            const string nil = "(nil)";
            var          cmd = parameters[0];

            return(cmd.ToUpperInvariant() switch
            {
                "SET" => (storage.Set(parameters) ?? nil),
                "GET" => (storage.Get(parameters) ?? nil),
                "DEL" => storage.Del(parameters),
                "DBSIZE" => storage.DbSize(parameters),
                "INCR" => storage.Incr(parameters),
                "ZADD" => storage.ZAdd(parameters),
                "ZCARD" => storage.ZCard(parameters),
                "ZRANK" => (storage.ZRank(parameters) ?? nil),
                "ZRANGE" => storage.ZRange(parameters) ?? nil,
                _ => throw new ArgumentException($"Could not recognize command '{cmd}'")
            });
示例#2
0
        public static string Set(this MiniRedisStorage storage, string[] parameters)
        {
            AssertLengthIsExactly(parameters, 3, 4);

            if (parameters.Length == 3)
            {
                return(storage.Set(parameters[1], parameters[2], null));
            }

            var secondsParam = parameters[3];

            if (!int.TryParse(secondsParam, out var seconds))
            {
                throw new ArgumentException($"Unable to parse {secondsParam}, number of seconds was expected");
            }

            var key   = parameters[1];
            var value = parameters[2];

            return(storage.Set(key, value, seconds));
        }