protected override object OnExecute(Services.CommandContext context)
        {
            var count = this.Redis.Count;

            context.Output.WriteLine(count.ToString());
            return(count);
        }
        protected override object OnExecute(Services.CommandContext context)
        {
            if (context.Expression.Arguments.Length < 2)
            {
                throw new Services.CommandException("Missing arguments.");
            }

            var queue = this.Redis.GetEntry <IRedisQueue>(context.Expression.Arguments[0]);

            if (queue == null)
            {
                context.Error.WriteLine($"The '{context.Expression.Arguments[0]}' queue is not existed.");
                return(0);
            }

            if (context.Expression.Arguments.Length == 2)
            {
                queue.Enqueue(context.Expression.Arguments[1]);
                return(1);
            }
            else
            {
                var items = new string[context.Expression.Arguments.Length - 1];
                Array.Copy(context.Expression.Arguments, 1, items, 0, items.Length);

                return(queue.EnqueueMany(items));
            }
        }
        protected override object OnExecute(Services.CommandContext context)
        {
            if (context.Expression.Arguments.Length < 1)
            {
                throw new Services.CommandException("Missing arguments.");
            }

            var result = new long[context.Expression.Arguments.Length];

            for (int i = 0; i < result.Length; i++)
            {
                var queue = this.Redis.GetEntry <IRedisQueue>(context.Expression.Arguments[i]);

                if (queue == null)
                {
                    context.Error.WriteLine($"The '{context.Expression.Arguments[i]}' queue is not existed.");
                    return(0);
                }

                result[i] = queue.Count;
                context.Output.WriteLine(result[i].ToString());
            }

            if (result.Length == 1)
            {
                return(result[0]);
            }
            else
            {
                return(result);
            }
        }
Beispiel #4
0
        protected override object OnExecute(Services.CommandContext context)
        {
            if (context.Expression.Arguments.Length == 0)
            {
                throw new Zongsoft.Services.CommandException("Missing arguments.");
            }

            if (context.Expression.Options.Contains("duration") && context.Expression.Options.Contains("expires"))
            {
                throw new Zongsoft.Services.CommandOptionException("duration, expires");
            }

            TimeSpan duration  = context.Expression.Options.GetValue <TimeSpan>("duration");
            var      notExists = context.Expression.Options.Contains("not") || context.Expression.Options.Contains("notExists");

            if (context.Expression.Arguments.Length == 1)
            {
                if (context.Parameter != null)
                {
                    return(this.Redis.GetCache(null).SetValue(context.Expression.Arguments[0], context.Parameter, duration, notExists));
                }

                throw new Zongsoft.Services.CommandException("Missing arguments.");
            }

            return(this.Redis.SetValue(context.Expression.Arguments[0], context.Expression.Arguments[1], duration, notExists));
        }
        protected override object OnExecute(Services.CommandContext context)
        {
            if (context.Expression.Arguments.Length < 1)
            {
                throw new Zongsoft.Services.CommandException("Missing arguments.");
            }

            int seed     = context.Expression.Options.GetValue <int>("seed");
            int interval = context.Expression.Options.GetValue <int>("interval");
            var result   = new long[context.Expression.Arguments.Length];

            for (int i = 0; i < context.Expression.Arguments.Length; i++)
            {
                result[i] = this.Redis.Increment(context.Expression.Arguments[i], interval, seed);
                context.Output.WriteLine(result[i].ToString());
            }

            if (result.Length == 1)
            {
                return(result[0]);
            }
            else
            {
                return(result);
            }
        }
        protected override object OnExecute(Services.CommandContext context)
        {
            if (context.Expression.Arguments.Length < 1)
            {
                throw new Services.CommandException("Missing arguments.");
            }

            var result = new List <long>(context.Expression.Arguments.Length);

            for (int i = 0; i < context.Expression.Arguments.Length; i++)
            {
                var hashset = this.Redis.GetEntry <IRedisHashset>(context.Expression.Arguments[i]);

                if (hashset == null)
                {
                    context.Error.WriteLine($"The '{context.Expression.Arguments[i]}' hashset is not existed.");
                    return(0);
                }

                result.Add(hashset.Count);
                context.Output.WriteLine(result[i].ToString());
            }

            if (result.Count == 1)
            {
                return(result[0]);
            }
            else
            {
                return(result);
            }
        }
        protected override object OnExecute(Services.CommandContext context)
        {
            if (context.Expression.Arguments.Length == 0)
            {
                throw new Zongsoft.Services.CommandException("Missing arguments.");
            }

            var result = new IEnumerable <string> [context.Expression.Arguments.Length];

            for (int i = 0; i < context.Expression.Arguments.Length; i++)
            {
                //查找指定模式的键名集
                result[i] = this.Redis.Find(context.Expression.Arguments[i]);

                //打印模式字符串
                context.Output.WriteLine(Services.CommandOutletColor.Magenta, context.Expression.Arguments[i]);

                //定义遍历序号
                var index = 1;

                foreach (var key in result[i])
                {
                    context.Output.Write(Services.CommandOutletColor.DarkGray, $"[{index++}] ");
                    context.Output.WriteLine(Services.CommandOutletColor.Green, key);
                }

                context.Output.WriteLine();
            }

            return(result);
        }
        protected override object OnExecute(Services.CommandContext context)
        {
            if (context.Expression.Arguments.Length < 3)
            {
                throw new Zongsoft.Services.CommandException("Missing arguments.");
            }

            var dictionary = this.Redis.GetEntry <IRedisDictionary>(context.Expression.Arguments[0]);

            if (dictionary == null)
            {
                context.Error.WriteLine($"The '{context.Expression.Arguments[0]}' dictionary is not existed.");
                return(false);
            }

            if (context.Expression.Arguments.Length == 3)
            {
                dictionary[context.Expression.Arguments[1]] = context.Expression.Arguments[2];
                return(true);
            }

            var items = new KeyValuePair <string, string> [(context.Expression.Arguments.Length - 1) / 2];

            for (int i = 0; i < items.Length; i++)
            {
                items[i] = new KeyValuePair <string, string>(context.Expression.Arguments[i * 2 + 1], context.Expression.Arguments[i * 2 + 2]);
            }

            dictionary.SetRange(items);

            return(true);
        }
Beispiel #9
0
        protected override object OnExecute(Services.CommandContext context)
        {
            if (context.Expression.Arguments.Length < 2)
            {
                throw new Services.CommandException("Missing arguments.");
            }

            var hashset = this.Redis.GetEntry <IRedisHashset>(context.Expression.Arguments[0]);

            if (hashset == null)
            {
                context.Error.WriteLine($"The '{context.Expression.Arguments[0]}' hashset is not existed.");
                return(null);
            }

            if (context.Expression.Arguments.Length == 2)
            {
                return(hashset.Remove(context.Expression.Arguments[1]) ? 1 : 0);
            }
            else
            {
                var items = new string[context.Expression.Arguments.Length - 1];
                Array.Copy(context.Expression.Arguments, 1, items, 0, items.Length);

                return(hashset.RemoveRange(items));
            }
        }
Beispiel #10
0
        protected override object OnExecute(Services.CommandContext context)
        {
            var count = RedisCommand.GetRedis(context.CommandNode).GetCount();

            context.Output.WriteLine(count.ToString());
            return(count);
        }
Beispiel #11
0
        protected override object OnExecute(Services.CommandContext context)
        {
            if (context.Expression.Arguments.Length < 1)
            {
                throw new Zongsoft.Services.CommandException("Missing arguments.");
            }

            var dictionary = this.Redis.GetEntry <IRedisDictionary>(context.Expression.Arguments[0]);

            if (dictionary == null)
            {
                context.Error.WriteLine($"The '{context.Expression.Arguments[0]}' dictionary is not existed.");
                return(null);
            }

            switch (context.Expression.Arguments.Length)
            {
            case 1:
                if (context.Expression.Options.Contains("all"))
                {
                    foreach (var entry in dictionary)
                    {
                        context.Output.WriteLine($"{entry.Key}={entry.Value}");
                    }
                }

                return(dictionary);

            case 2:
                var result = dictionary[context.Expression.Arguments[1]];
                context.Output.WriteLine(result);
                return(result);
            }

            var keys = new string[context.Expression.Arguments.Length - 1];

            Array.Copy(context.Expression.Arguments, 1, keys, 0, keys.Length);

            var values = dictionary.GetValues(keys);

            foreach (var value in values)
            {
                context.Output.WriteLine(value);
            }

            return(values);
        }
Beispiel #12
0
        protected override object OnExecute(Services.CommandContext context)
        {
            if (context.Expression.Arguments.Length < 1)
            {
                throw new Zongsoft.Services.CommandException("Invalid arguments of command.");
            }

            if (context.Expression.Arguments.Length == 1)
            {
                this.Redis.Remove(context.Expression.Arguments[0]);
            }
            else
            {
                this.Redis.RemoveMany(context.Expression.Arguments);
            }

            return(null);
        }
        protected override object OnExecute(Services.CommandContext context)
        {
            if (context.Expression.Arguments.Length < 1)
            {
                throw new Services.CommandException("Missing arguments.");
            }

            var result = new IRedisHashset[context.Expression.Arguments.Length];

            for (var i = 0; i < context.Expression.Arguments.Length; i++)
            {
                result[i] = this.Redis.GetEntry <IRedisHashset>(context.Expression.Arguments[i]);

                if (result[i] == null)
                {
                    context.Error.WriteLine($"The '{context.Expression.Arguments[i]}' hashset is not existed.");
                    return(null);
                }

                context.Output.WriteLine(Services.CommandOutletColor.Magenta, $"The '{context.Expression.Arguments[i]}' hashset have {result[i].Count} entries:");

                foreach (var item in result[i])
                {
                    context.Output.WriteLine(item.ToString());
                }

                context.Output.WriteLine();
            }

            if (result.Length == 1)
            {
                return(result[0]);
            }
            else
            {
                return(result);
            }
        }
        protected override object OnExecute(Services.CommandContext context)
        {
            if (context.Expression.Arguments.Length < 2)
            {
                throw new Zongsoft.Services.CommandException("The arguments is not enough.");
            }

            if (context.Expression.Arguments.Length % 2 != 0)
            {
                throw new Zongsoft.Services.CommandException("The count arguments must be an even number.");
            }

            var interval = context.Expression.Options.GetValue <int>("interval");
            var result   = new List <long>(context.Expression.Arguments.Length / 2);

            for (int i = 0; i < context.Expression.Arguments.Length / 2; i++)
            {
                var dictionary = this.Redis.GetEntry <IRedisDictionary>(context.Expression.Arguments[i * 2]);

                if (dictionary == null)
                {
                    context.Error.WriteLine($"The '{context.Expression.Arguments[i * 2]}' dictionary is not existed.");
                    return(0);
                }

                result.Add(dictionary.Increment(context.Expression.Arguments[i * 2 + 1], interval));
                context.Output.WriteLine(result.ToString());
            }

            if (result.Count == 1)
            {
                return(result[0]);
            }
            else
            {
                return(result);
            }
        }
Beispiel #15
0
        protected override object OnExecute(Services.CommandContext context)
        {
            if (context.Expression.Arguments.Length < 1)
            {
                throw new Services.CommandException("Missing arguments.");
            }

            foreach (var arg in context.Expression.Arguments)
            {
                var queue = this.Redis.GetEntry <IRedisQueue>(arg);

                if (queue == null)
                {
                    context.Error.WriteLine($"The '{arg}' queue is not existed.");
                }
                else
                {
                    queue.Clear();
                }
            }

            return(null);
        }
Beispiel #16
0
        protected override object OnExecute(Services.CommandContext context)
        {
            if (context.Expression.Arguments.Length < 1)
            {
                throw new Zongsoft.Services.CommandException("Missing arguments.");
            }

            var count  = context.Expression.Options.GetValue <int>("count");
            var index  = context.Expression.Options.GetValue <int>("index");
            var result = new List <string>(context.Expression.Arguments.Length * count);

            for (int i = 0; i < context.Expression.Arguments.Length; i++)
            {
                var queue = this.Redis.GetEntry <IRedisQueue>(context.Expression.Arguments[0]);

                if (queue == null)
                {
                    context.Error.WriteLine($"The '{context.Expression.Arguments[i]}' queue is not existed.");
                    return(null);
                }

                //打印当前队列名
                context.Output.WriteLine(Services.CommandOutletColor.Magenta, $"Dequeued entries from 'context.Expression.Arguments[i]' queue:");

                var items = queue.Take(index, count);

                foreach (var item in items)
                {
                    result.Add((string)item);
                    context.Output.WriteLine(item);
                }

                context.Output.WriteLine();
            }

            return(result);
        }
Beispiel #17
0
        protected override object OnExecute(Services.CommandContext context)
        {
            if (context.Expression.Arguments.Length == 0)
            {
                throw new Zongsoft.Services.CommandException("Missing arguments.");
            }

            //查找指定模式的键名集
            var result = RedisCommand.GetRedis(context.CommandNode)
                         .Find(
                context.Expression.Arguments[0],
                context.Expression.Options.GetValue <int>(COUNT_OPTION));

            //定义遍历序号
            var index = 1;

            foreach (var key in result)
            {
                context.Output.Write(Services.CommandOutletColor.DarkGray, $"[{index++}] ");
                context.Output.WriteLine(key);
            }

            return(result);
        }
        protected override object OnExecute(Services.CommandContext context)
        {
            if (context.Expression.Arguments.Length < 2)
            {
                throw new Zongsoft.Services.CommandException("Missing arguments.");
            }

            var dictionary = this.Redis.GetEntry <IRedisDictionary>(context.Expression.Arguments[0]);

            if (dictionary == null)
            {
                context.Error.WriteLine($"The '{context.Expression.Arguments[0]}' dictionary is not existed.");
                return(0);
            }

            int count = 0;

            for (int i = 1; i < context.Expression.Arguments.Length; i++)
            {
                count += dictionary.Remove(context.Expression.Arguments[i]) ? 1 : 0;
            }

            return(count);
        }
Beispiel #19
0
        protected override object OnExecute(Services.CommandContext context)
        {
            if (context.Expression.Arguments.Length == 0)
            {
                throw new Zongsoft.Services.CommandException("Missing arguments.");
            }

            var expiry    = context.Expression.Options.GetValue <TimeSpan?>(EXPIRY_OPTION) ?? TimeSpan.Zero;
            var requisite = context.Expression.Options.GetValue <Caching.CacheRequisite>(REQUISITE_OPTION);

            var redis = RedisCommand.GetRedis(context.CommandNode);

            if (context.Expression.Arguments.Length == 1)
            {
                if (context.Parameter != null)
                {
                    return(redis.SetValue(context.Expression.Arguments[0], context.Parameter, expiry, requisite));
                }

                throw new Zongsoft.Services.CommandException("Missing arguments.");
            }

            return(redis.SetValue(context.Expression.Arguments[0], context.Expression.Arguments[1], expiry, requisite));
        }
Beispiel #20
0
 public override bool CanExecute(Services.CommandContext parameter)
 {
     return(_redis != null && base.CanExecute(parameter));
 }
        protected override object OnExecute(Services.CommandContext context)
        {
            if (context.Expression.Arguments.Length < 1)
            {
                throw new Zongsoft.Services.CommandException("Missing arguments.");
            }

            int index  = 0;
            var result = new List <object>(context.Expression.Arguments.Length);

            for (int i = 0; i < context.Expression.Arguments.Length; i++)
            {
                var entry = this.Redis.GetEntry(context.Expression.Arguments[i]);

                if (entry == null)
                {
                    context.Output.WriteLine(Services.CommandOutletColor.Red, $"The '{context.Expression.Arguments[i]}' entry is not existed.");
                }
                else
                {
                    result.Add(entry);

                    var entryType = this.Redis.GetEntryType(context.Expression.Arguments[i]);
                    context.Output.Write(Services.CommandOutletColor.DarkGray, $"[{entryType}] ");

                    var expiry = this.Redis.GetEntryExpiry(context.Expression.Arguments[i]);
                    if (expiry.HasValue)
                    {
                        context.Output.Write(Services.CommandOutletColor.DarkCyan, expiry.Value.ToString() + " ");
                    }

                    switch (entryType)
                    {
                    case RedisEntryType.String:
                        context.Output.WriteLine(entry);
                        break;

                    case RedisEntryType.Dictionary:
                        context.Output.WriteLine(Services.CommandOutletColor.DarkYellow, $"The '{context.Expression.Arguments[i]}' dictionary have {((IRedisDictionary)entry).Count} entries.");

                        foreach (DictionaryEntry item in (IDictionary)entry)
                        {
                            context.Output.Write(Services.CommandOutletColor.Gray, $"[{(++index).ToString()}] ");
                            context.Output.Write(Services.CommandOutletColor.DarkGreen, item.Key.ToString());
                            context.Output.Write(Services.CommandOutletColor.Cyan, " : ");
                            context.Output.WriteLine(Services.CommandOutletColor.DarkGreen, item.Value.ToString());
                        }

                        break;

                    case RedisEntryType.List:
                        context.Output.WriteLine(Services.CommandOutletColor.DarkYellow, $"The '{context.Expression.Arguments[i]}' list(queue) have {((IRedisQueue)entry).Count} entries.");

                        foreach (object item in (IEnumerable)entry)
                        {
                            context.Output.Write(Services.CommandOutletColor.Gray, $"[{(++index).ToString()}] ");

                            if (item == null)
                            {
                                context.Output.WriteLine(Services.CommandOutletColor.DarkGray, "NULL");
                            }
                            else
                            {
                                context.Output.WriteLine(Services.CommandOutletColor.DarkGreen, item.ToString());
                            }
                        }

                        break;

                    case RedisEntryType.Set:
                    case RedisEntryType.SortedSet:
                        context.Output.WriteLine(Services.CommandOutletColor.DarkYellow, $"The '{context.Expression.Arguments[i]}' hashset have {((IRedisHashset)entry).Count} entries.");

                        foreach (object item in (IEnumerable)entry)
                        {
                            context.Output.Write(Services.CommandOutletColor.Gray, $"[{(++index).ToString()}] ");

                            if (item == null)
                            {
                                context.Output.WriteLine(Services.CommandOutletColor.DarkGray, "NULL");
                            }
                            else
                            {
                                context.Output.WriteLine(Services.CommandOutletColor.DarkGreen, item.ToString());
                            }
                        }

                        break;

                    default:
                        context.Output.WriteLine();
                        break;
                    }
                }
            }

            if (result.Count == 1)
            {
                return(result[0]);
            }
            else
            {
                return(result);
            }
        }