Ejemplo n.º 1
0
        public async Task DictionaryIncr()
        {
            var dict = new RedisDictionary<string, long>(GlobalSettings.Default, "dict");
            await dict.Delete();

            (await dict.Increment("hogehoge", 100)).Is(100);
            (await dict.Increment("hogehoge", 100)).Is(200);
            (await dict.Increment("hogehoge", -100)).Is(100);
            (await dict.Get("hogehoge")).Value.Is(100);

            (await dict.IncrementLimitByMax("hogehoge", 40, 150)).Is(140);
            (await dict.IncrementLimitByMax("hogehoge", 40, 150)).Is(150);
            (await dict.IncrementLimitByMin("hogehoge", -40, 100)).Is(110);
            (await dict.IncrementLimitByMin("hogehoge", -40, 100)).Is(100);

            var dict2 = new RedisDictionary<string, double>(GlobalSettings.Default, "dict2");
            await dict2.Delete();
            (await dict2.Increment("hogehoge", 100.5)).Is(100.5);
            (await dict2.Increment("hogehoge", 100.0)).Is(200.5);
            (await dict2.Increment("hogehoge", -100.0)).Is(100.5);
            (await dict2.Get("hogehoge")).Value.Is(100.5);

            (await dict2.IncrementLimitByMax("hogehoge", 40.1, 150.9)).Is(140.6);
            (await dict2.IncrementLimitByMax("hogehoge", 40.3, 150.9)).Is(150.9);
            (await dict2.IncrementLimitByMin("hogehoge", -40.1, 100.9)).Is(110.8);
            (await dict2.IncrementLimitByMin("hogehoge", -40.3, 100.9)).Is(100.9);
        }
Ejemplo n.º 2
0
        public async Task DictionaryIncr()
        {
            var dict = new RedisDictionary <string, long>(GlobalSettings.Default, "dict");
            await dict.Delete();

            (await dict.Increment("hogehoge", 100)).Is(100);
            (await dict.Increment("hogehoge", 100)).Is(200);
            (await dict.Increment("hogehoge", -100)).Is(100);
            (await dict.Get("hogehoge")).Value.Is(100);

            (await dict.IncrementLimitByMax("hogehoge", 40, 150)).Is(140);
            (await dict.IncrementLimitByMax("hogehoge", 40, 150)).Is(150);
            (await dict.IncrementLimitByMin("hogehoge", -40, 100)).Is(110);
            (await dict.IncrementLimitByMin("hogehoge", -40, 100)).Is(100);

            var dict2 = new RedisDictionary <string, double>(GlobalSettings.Default, "dict2");
            await dict2.Delete();

            (await dict2.Increment("hogehoge", 100.5)).Is(100.5);
            (await dict2.Increment("hogehoge", 100.0)).Is(200.5);
            (await dict2.Increment("hogehoge", -100.0)).Is(100.5);
            (await dict2.Get("hogehoge")).Value.Is(100.5);

            (await dict2.IncrementLimitByMax("hogehoge", 40.1, 150.9)).Is(140.6);
            (await dict2.IncrementLimitByMax("hogehoge", 40.3, 150.9)).Is(150.9);
            (await dict2.IncrementLimitByMin("hogehoge", -40.1, 100.9)).Is(110.8);
            (await dict2.IncrementLimitByMin("hogehoge", -40.3, 100.9)).Is(100.9);
        }
Ejemplo n.º 3
0
        public async Task DictIncrMax()
        {
            var v = new RedisDictionary <int>(settings, "test-hash");

            await v.Set("a", 0);

            (await v.IncrementLimitByMax("a", 10, 100)).Is(10);
            (await v.IncrementLimitByMax("a", 20, 100)).Is(30);
            (await v.IncrementLimitByMax("a", 30, 100)).Is(60);
            (await v.IncrementLimitByMax("a", 40, 100)).Is(100);
            (await v.IncrementLimitByMax("a", 50, 100)).Is(100);

            (await v.Get("a")).Is(100);

            var v2 = new RedisDictionary <double>(settings, "test-hash");
            await v2.Set("a", 0);

            (await v2.IncrementLimitByMax("a", 10.5, 100)).Is(10.5);
            (await v2.IncrementLimitByMax("a", 20.5, 100)).Is(31);
            (await v2.IncrementLimitByMax("a", 40.5, 100)).Is(71.5);
            (await v2.IncrementLimitByMax("a", 40.5, 100.1)).Is(100.1);
            (await v2.IncrementLimitByMax("a", 50.0, 100)).Is(100);

            (await v2.Get("a")).Is(100);
        }
Ejemplo n.º 4
0
 public static void ListAllFromDictionary(RedisDictionary redisDictionary)
 {
     foreach (var item in redisDictionary)
     {
         Console.WriteLine("{0}=>{1}", item.Key, item.Value);
     }
 }
Ejemplo n.º 5
0
        /// <summary>
        /// 同时设置哈希表 <paramref name="key"/> 中一个或多个域值。
        /// </summary>
        /// <param name="client">Redis 客户端。</param>
        /// <param name="key">键名</param>
        /// <param name="fieldValues">域值的匿名对象。</param>
        /// <returns>结果。</returns>
        public static Result HMSet(this IRedisClient client, string key, object fieldValues)
        {
            if (client == null)
            {
                throw new ArgumentNullException(nameof(client));
            }
            if (fieldValues == null)
            {
                throw new ArgumentNullException(nameof(fieldValues));
            }
            RedisDictionary redisDict;

            if (fieldValues is System.Collections.IDictionary)
            {
                redisDict = new RedisDictionary(fieldValues as System.Collections.IDictionary);
            }
            else
            {
                var typeMapper = TypeMapper.Create(fieldValues.GetType());
                redisDict = new RedisDictionary(typeMapper.Count);
                foreach (var propertyMapper in typeMapper.Properties)
                {
                    redisDict.Add(propertyMapper.Name, BinaryValue.Create(propertyMapper.GetValue(fieldValues, false), propertyMapper.Property));
                }
            }
            return(HMSet(client, key, redisDict));
        }
Ejemplo n.º 6
0
        public static Task SetAsync <TValue>(RedisDictionary <string, Crap> _dictionary, string key, TValue value)
        {
            _dictionary.Add(key, new Crap {
                Value = value
            });

            return(Task.CompletedTask);
        }
Ejemplo n.º 7
0
 public GatewayServer(string ID, string name)
     : base(ID, name)
 {
     JsConfig.AssumeUtc    = true;
     activeTrips           = new ActiveTrips(redisClient, ID + ":" + MemberInfoGetting.GetMemberName(() => activeTrips));
     partnerAccounts       = new RedisDictionary <string, PartnerAccount>(redisClient, ID + ":" + MemberInfoGetting.GetMemberName(() => partnerAccounts));
     clientIdByAccessToken = new RedisDictionary <string, string>(redisClient, ID + ":" + MemberInfoGetting.GetMemberName(() => clientIdByAccessToken));
 }
Ejemplo n.º 8
0
        public static string FindFromDictionary(string command, RedisDictionary redisDictionary)
        {
            int wordStartIndex = command.IndexOf(' ') + 1;

            string word = command.Substring(wordStartIndex, command.Length - wordStartIndex).Trim();

            return word + "=>" + redisDictionary[word];
        }
Ejemplo n.º 9
0
        public static void RemoveFromDictionary(string command, RedisDictionary redisDictionary)
        {
            int wordStartIndex = command.IndexOf(' ') + 1;

            string word = command.Substring(wordStartIndex, command.Length - wordStartIndex).Trim();

            redisDictionary.Remove(word);
        }
Ejemplo n.º 10
0
        /// <summary>
        /// 根据给定的 sha1 校验码,对缓存在服务器中的脚本进行求值。
        /// </summary>
        /// <param name="client">Redis 客户端。</param>
        /// <param name="sha1">sha1 校验码。</param>
        /// <param name="keyArgs">键名和参数值的字典。</param>
        /// <returns>执行脚本后的值。</returns>
        public static object EvalSHA(this IRedisClient client, string sha1, RedisDictionary keyArgs)
        {
            if(client == null) throw new ArgumentNullException(nameof(client));
            if(string.IsNullOrWhiteSpace(sha1)) throw new ArgumentNullException(nameof(sha1));
            if(keyArgs == null) throw new ArgumentNullException(nameof(keyArgs));

            var args = RedisArgs.ConcatAll(new object[] { sha1, keyArgs.Keys.Count }, keyArgs.Keys, keyArgs.Values);
            return client.Execute(new RedisObject("EVALSHA", args.ToArray()));
        }
Ejemplo n.º 11
0
        public static void AddToDictionary(string command, RedisDictionary redisDictionary)
        {
            int wordStartIndex = command.IndexOf(' ') + 1;
            int wordEndIndex = command.IndexOf('-');

            string word = command.Substring(wordStartIndex, wordEndIndex - wordStartIndex).Trim();
            string explination = command.Substring(wordEndIndex + 1, (command.Length - 1) - wordEndIndex);

            redisDictionary.Add(word, explination);
        }
Ejemplo n.º 12
0
        private void InitializePersistantDataObjects()
        {
            partners = new Dictionary <string, Gateway>();
            var redisClient = (RedisClient)redis.GetClient();

            originatingPartnerByTrip = new RedisDictionary <string, string>(redisClient, ID + ":" + MemberInfoGetting.GetMemberName(() => originatingPartnerByTrip));
            originatingPartnerByTrip.Clear();
            servicingPartnerByTrip = new RedisDictionary <string, string>(redisClient, ID + "." + MemberInfoGetting.GetMemberName(() => servicingPartnerByTrip));
            servicingPartnerByTrip.Clear();
            partnerCoverage = new RedisDictionary <string, List <Zone> >(redisClient, ID + "." + MemberInfoGetting.GetMemberName(() => partnerCoverage));
            partnerCoverage.Clear();
            partnerAccounts.Clear();
            clientIdByAccessToken.Clear();
        }
Ejemplo n.º 13
0
        public static Task <TValue> GetAsyncX <TValue>(RedisDictionary <string, object> _dictionary, string key, Func <string, TValue> onAdd = null)
        {
            var result = _dictionary.GetOrAdd(key, (keyname) =>
            {
                var value = default(TValue);
                if (onAdd != null)
                {
                    value = onAdd.Invoke(keyname);
                }

                return(value);
            });

            var xxx = _dictionary.ValueSerializer.Deserialize <TValue>(result);

            return(Task.FromResult(xxx));
        }
Ejemplo n.º 14
0
        static void Main(string[] args)
        {
            using (RedisClient client = new RedisClient())
            {
                UI.DrawMenu();

                Console.Write("Please enter a dictionary name: ");
                string dictionaryName = Console.ReadLine();

                RedisDictionary redisDictionary = new RedisDictionary(client, dictionaryName);

                while (true)
                {
                    Console.Write("Please enter a command: ");
                    string command = Console.ReadLine().Trim();

                    if (command == "Exit")
                    {
                        break;
                    }

                    Engine.ParseCommand(command, redisDictionary);
                }

                //redisDictionary.Add("gosho", "ribata");
                //redisDictionary.Add("pesho", "akademiqta");
                //redisDictionary.Add("kiki", "razvrata");

                //Console.WriteLine(redisDictionary["kiki"]);

                //Console.WriteLine(redisDictionary.ContainsKey("pesho"));
                //redisDictionary.Remove("pesho");
                //Console.WriteLine(redisDictionary.ContainsKey("pesho"));

                //foreach (var item in redisDictionary)
                //{
                //    Console.WriteLine("{0} -> {1}", item.Key, item.Value);
                //}

                //redisDictionary["kiki"] = "obshtata";

                //Console.WriteLine(redisDictionary["kiki"]);
            }
        }
Ejemplo n.º 15
0
        /// <summary>
        /// 仅当所有给定键都不存在,同时设置一个或多个键值。即使只有一个给定键已存在,MSETNX 也会拒绝执行所有给定键的设置操作。
        /// </summary>
        /// <param name="client">Redis 客户端。</param>
        /// <param name="keyValues">键值的字典。</param>
        /// <returns>当所有键都成功设置返回 true,否则返回 false。</returns>
        public static bool MSetNx(this IRedisClient client, RedisDictionary keyValues)
        {
            if (client == null)
            {
                throw new ArgumentNullException("client");
            }
            if (keyValues == null)
            {
                throw new ArgumentNullException("keyValues");
            }
            if (keyValues.Count == 0)
            {
                return(false);
            }

            var args = RedisArgs.Parse(keyValues).ToArray();

            return(client.Execute(new RedisBoolean("MSETNX", args)));
        }
Ejemplo n.º 16
0
        /// <summary>
        /// 同时设置哈希表 <paramref name="key"/> 中一个或多个域值。
        /// </summary>
        /// <param name="client">Redis 客户端。</param>
        /// <param name="key">键名</param>
        /// <param name="fieldValues">域值的字典。</param>
        /// <returns>结果。</returns>
        public static Result HMSet(this IRedisClient client, string key, RedisDictionary fieldValues)
        {
            if (client == null)
            {
                throw new ArgumentNullException(nameof(client));
            }
            if (fieldValues == null)
            {
                throw new ArgumentNullException(nameof(fieldValues));
            }
            if (fieldValues.Count == 0)
            {
                return(Result.Successfully);
            }

            var args = RedisArgs.Parse(fieldValues, key).ToArray();

            return(client.Execute(new RedisStatus("HMSET", args)));
        }
Ejemplo n.º 17
0
        /// <summary>
        /// 根据给定的 sha1 校验码,对缓存在服务器中的脚本进行求值。
        /// </summary>
        /// <param name="client">Redis 客户端。</param>
        /// <param name="sha1">sha1 校验码。</param>
        /// <param name="keyArgs">键名和参数值的字典。</param>
        /// <returns>返回执行脚本后的值。</returns>
        public static object EvalSHA(this IRedisClient client, string sha1, RedisDictionary keyArgs)
        {
            if (client == null)
            {
                throw new ArgumentNullException("client");
            }
            if (string.IsNullOrEmpty(sha1))
            {
                throw new ArgumentNullException("sha1");
            }
            if (keyArgs == null)
            {
                throw new ArgumentNullException("keyArgs");
            }

            var args = RedisArgs.ConcatAll(new object[] { sha1, keyArgs.Keys.Count }, keyArgs.Keys, keyArgs.Values);

            return(client.Execute(new RedisObject("EVALSHA", args.ToArray())));
        }
Ejemplo n.º 18
0
        /// <summary>
        /// 同时设置一个或多个键值。
        /// </summary>
        /// <param name="client">Redis 客户端。</param>
        /// <param name="keyValues">键值的字典。</param>
        /// <returns>返回一个结果。</returns>
        public static Result MSet(this IRedisClient client, RedisDictionary keyValues)
        {
            if (client == null)
            {
                throw new ArgumentNullException("client");
            }
            if (keyValues == null)
            {
                throw new ArgumentNullException("keyValues");
            }
            if (keyValues.Count == 0)
            {
                return(Result.Successfully);
            }

            var args = RedisArgs.Parse(keyValues).ToArray();

            return(client.Execute(new RedisStatus("MSET", args)));
        }
Ejemplo n.º 19
0
        /// <summary>
        /// 执行 Lua 脚本进行求值。
        /// </summary>
        /// <param name="client">Redis 客户端。</param>
        /// <param name="script">Lua 脚本代码。</param>
        /// <param name="keyArgs">键名和参数值的字典。</param>
        /// <returns>执行脚本后的值。</returns>
        public static object Eval(this IRedisClient client, string script, RedisDictionary keyArgs)
        {
            if (client == null)
            {
                throw new ArgumentNullException(nameof(client));
            }
            if (string.IsNullOrWhiteSpace(script))
            {
                throw new ArgumentNullException(nameof(script));
            }
            if (keyArgs == null)
            {
                throw new ArgumentNullException(nameof(keyArgs));
            }

            var args = RedisArgs.ConcatAll(new object[] { script, keyArgs.Keys.Count }, keyArgs.Keys, keyArgs.Values);

            return(client.Execute(new RedisObject("EVAL", args.ToArray())));
        }
Ejemplo n.º 20
0
        public async Task DictionaryAdd()
        {
            var dict = new RedisDictionary <string, int>(GlobalSettings.Default, "dict");
            await dict.Delete();

            (await dict.Set("a", -1)).IsTrue();
            (await dict.Set("a", 0)).IsFalse(); // already exists
            (await dict.Set("b", 1)).IsTrue();
            (await dict.Set("b", -1, when: When.NotExists)).IsFalse();
            (await dict.Set("c", 2)).IsTrue();
            (await dict.Set("d", 3)).IsTrue();
            await dict.Set(new Dictionary <string, int> {
                { "e", 4 }, { "f", 5 }
            });

            var xs = (await dict.GetAll()).OrderBy(x => x.Key).ToArray();

            xs.Select(x => x.Key).Is("a", "b", "c", "d", "e", "f");
            xs.Select(x => x.Value).Is(0, 1, 2, 3, 4, 5);
            (await dict.Keys()).OrderBy(x => x).Is("a", "b", "c", "d", "e", "f");
            (await dict.Values()).OrderBy(x => x).Is(0, 1, 2, 3, 4, 5);
            (await dict.Length()).Is(6);

            (await dict.Exists("a")).IsTrue();
            (await dict.Exists("z")).IsFalse();

            (await dict.Get("a")).Value.Is(0);
            (await dict.Get("c")).Value.Is(2);
            (await dict.Get("z")).HasValue.IsFalse();

            var mget = (await dict.Get(new[] { "a", "b", "u", "d", "z" })).OrderBy(x => x.Key).ToArray();

            mget.Select(x => x.Key).Is("a", "b", "d");
            mget.Select(x => x.Value).Is(0, 1, 3);

            (await dict.Delete("c")).IsTrue();
            (await dict.Delete("c")).IsFalse();
            (await dict.Keys()).OrderBy(x => x).Is("a", "b", "d", "e", "f");

            (await dict.Delete(new[] { "a", "c", "d", "z" })).Is(2);
            (await dict.Keys()).OrderBy(x => x).Is("b", "e", "f");
        }
Ejemplo n.º 21
0
        public async Task DictionaryAdd()
        {
            var dict = new RedisDictionary<string, int>(GlobalSettings.Default, "dict");
            await dict.Delete();

            (await dict.Set("a", -1)).IsTrue();
            (await dict.Set("a", 0)).IsFalse(); // already exists
            (await dict.Set("b", 1)).IsTrue();
            (await dict.Set("b", -1, when: When.NotExists)).IsFalse();
            (await dict.Set("c", 2)).IsTrue();
            (await dict.Set("d", 3)).IsTrue();
            await dict.Set(new Dictionary<string, int> { { "e", 4 }, { "f", 5 } });

            var xs = (await dict.GetAll()).OrderBy(x => x.Key).ToArray();
            xs.Select(x => x.Key).Is("a", "b", "c", "d", "e", "f");
            xs.Select(x => x.Value).Is(0, 1, 2, 3, 4, 5);
            (await dict.Keys()).OrderBy(x => x).Is("a", "b", "c", "d", "e", "f");
            (await dict.Values()).OrderBy(x => x).Is(0, 1, 2, 3, 4, 5);
            (await dict.Length()).Is(6);

            (await dict.Exists("a")).IsTrue();
            (await dict.Exists("z")).IsFalse();

            (await dict.Get("a")).Value.Is(0);
            (await dict.Get("c")).Value.Is(2);
            (await dict.Get("z")).HasValue.IsFalse();

            var mget = (await dict.Get(new[] { "a", "b", "u", "d", "z" })).OrderBy(x => x.Key).ToArray();
            mget.Select(x => x.Key).Is("a", "b", "d");
            mget.Select(x => x.Value).Is(0, 1, 3);

            (await dict.Delete("c")).IsTrue();
            (await dict.Delete("c")).IsFalse();
            (await dict.Keys()).OrderBy(x => x).Is("a", "b", "d", "e", "f");

            (await dict.Delete(new[] { "a", "c", "d", "z" })).Is(2);
            (await dict.Keys()).OrderBy(x => x).Is("b", "e", "f");
        }
Ejemplo n.º 22
0
        public static void ParseCommand(string command, RedisDictionary redisDictionary)
        {
            int commandEndIndex = command.IndexOf(' ');

            string parsedCommand = command.Substring(0, commandEndIndex).Trim();

            switch (parsedCommand)
            {
                case "Add": AddToDictionary(command, redisDictionary);
                    Console.WriteLine("Word is added successfully.");
                    break;
                case "Remove": RemoveFromDictionary(command, redisDictionary);
                    Console.WriteLine("Word is removed successfully.");
                    break;
                case "Find":
                    string result = FindFromDictionary(command, redisDictionary);
                    Console.WriteLine(result);
                    break;
                case "List": ListAllFromDictionary(redisDictionary);
                    break;
                default: Console.WriteLine("Wrong command. Try again.");
                    break;
            }
        }
Ejemplo n.º 23
0
        public async Task DictDecrMin()
        {
            var v = new RedisDictionary <int>(settings, "test-hash");

            await v.Set("a", 100);

            (await v.IncrementLimitByMin("a", -10, 0)).Is(90);
            (await v.IncrementLimitByMin("a", -20, 0)).Is(70);
            (await v.IncrementLimitByMin("a", -30, 0)).Is(40);
            (await v.IncrementLimitByMin("a", -42, 0)).Is(0);
            (await v.IncrementLimitByMin("a", -50, 0)).Is(0);

            (await v.Get("a")).Is(0);

            var v2 = new RedisDictionary <double>(settings, "test-hash");
            await v2.Set("a", 100);

            (await v2.IncrementLimitByMin("a", -10.5, 0.5)).Is(89.5);
            (await v2.IncrementLimitByMin("a", -20.5, 0.5)).Is(69);
            (await v2.IncrementLimitByMin("a", -40.5, 0.5)).Is(28.5);
            (await v2.IncrementLimitByMin("a", -40.5, 0.5)).Is(0.5);

            (await v2.Get("a")).Is(0.5);
        }
Ejemplo n.º 24
0
 public ActiveTrips(RedisClient redisClient, string id)
 {
     dict = new RedisDictionary<string, Trip>(redisClient, id);
     dict.Clear();
 }
Ejemplo n.º 25
0
 public GatewayServer(string ID, string name)
     : base(ID, name)
 {
     JsConfig.AssumeUtc = true;
     activeTrips = new ActiveTrips(redisClient, ID + ":" + MemberInfoGetting.GetMemberName(() => activeTrips));
     partnerAccounts = new RedisDictionary<string, PartnerAccount>(redisClient, ID + ":" + MemberInfoGetting.GetMemberName(() => partnerAccounts));
     clientIdByAccessToken = new RedisDictionary<string, string>(redisClient, ID + ":" + MemberInfoGetting.GetMemberName(() => clientIdByAccessToken));
 }
Ejemplo n.º 26
0
 public ActiveTrips(RedisClient redisClient, string id)
 {
     dict = new RedisDictionary <string, Trip>(redisClient, id);
     dict.Clear();
 }
Ejemplo n.º 27
0
        public static Task SetAsyncX <TValue>(RedisDictionary <string, object> _dictionary, string key, TValue value)
        {
            _dictionary.Add(key, value);

            return(Task.CompletedTask);
        }
Ejemplo n.º 28
0
        public async Task DictIncrMax()
        {
            var v = new RedisDictionary<int>(settings, "test-hash");

            await v.Set("a", 0);
            (await v.IncrementLimitByMax("a", 10, 100)).Is(10);
            (await v.IncrementLimitByMax("a", 20, 100)).Is(30);
            (await v.IncrementLimitByMax("a", 30, 100)).Is(60);
            (await v.IncrementLimitByMax("a", 40, 100)).Is(100);
            (await v.IncrementLimitByMax("a", 50, 100)).Is(100);

            (await v.Get("a")).Is(100);

            var v2 = new RedisDictionary<double>(settings, "test-hash");
            await v2.Set("a", 0);
            (await v2.IncrementLimitByMax("a", 10.5, 100)).Is(10.5);
            (await v2.IncrementLimitByMax("a", 20.5, 100)).Is(31);
            (await v2.IncrementLimitByMax("a", 40.5, 100)).Is(71.5);
            (await v2.IncrementLimitByMax("a", 40.5, 100.1)).Is(100.1);
            (await v2.IncrementLimitByMax("a", 50.0, 100)).Is(100);

            (await v2.Get("a")).Is(100);
        }
Ejemplo n.º 29
0
 public RedisDictionaryTests()
 {
     this._factory   = new RedisFactory();
     this.serializer = DefaultSerializer.Default;
     this.dict       = new RedisDictionary <string, Model> (_factory.Database, "_TEST:Model", serializer);
 }
Ejemplo n.º 30
0
        /// <summary>
        /// 仅当所有给定键都不存在,同时设置一个或多个键值。即使只有一个给定键已存在,MSETNX 也会拒绝执行所有给定键的设置操作。
        /// </summary>
        /// <param name="client">Redis 客户端。</param>
        /// <param name="keyValues">键值的字典。</param>
        /// <returns>当所有键都成功设置返回 true,否则返回 false。</returns>
        public static bool MSetNx(this IRedisClient client, RedisDictionary keyValues)
        {
            if(client == null) throw new ArgumentNullException(nameof(client));
            if(keyValues == null) throw new ArgumentNullException(nameof(keyValues));
            if(keyValues.Count == 0) return false;

            var args = RedisArgs.Parse(keyValues).ToArray();
            return client.Execute(new RedisBoolean("MSETNX", args));
        }
Ejemplo n.º 31
0
        public async Task DictDecrMin()
        {
            var v = new RedisDictionary<int>(settings, "test-hash");

            await v.Set("a", 100);
            (await v.IncrementLimitByMin("a", -10, 0)).Is(90);
            (await v.IncrementLimitByMin("a", -20, 0)).Is(70);
            (await v.IncrementLimitByMin("a", -30, 0)).Is(40);
            (await v.IncrementLimitByMin("a", -42, 0)).Is(0);
            (await v.IncrementLimitByMin("a", -50, 0)).Is(0);

            (await v.Get("a")).Is(0);

            var v2 = new RedisDictionary<double>(settings, "test-hash");
            await v2.Set("a", 100);
            (await v2.IncrementLimitByMin("a", -10.5, 0.5)).Is(89.5);
            (await v2.IncrementLimitByMin("a", -20.5, 0.5)).Is(69);
            (await v2.IncrementLimitByMin("a", -40.5, 0.5)).Is(28.5);
            (await v2.IncrementLimitByMin("a", -40.5, 0.5)).Is(0.5);

            (await v2.Get("a")).Is(0.5);
        }
Ejemplo n.º 32
0
        public void TestScan()
        {
            var reply1 = "*2\r\n$1\r\n0\r\n*3\r\n$5\r\ntest1\r\n$5\r\ntest2\r\n$5\r\ntest3\r\n";
            var reply2 = "*2\r\n$1\r\n0\r\n*0\r\n";
            var reply3 = "*2\r\n$1\r\n0\r\n*0\r\n";
            var reply4 = "*2\r\n$1\r\n0\r\n*0\r\n";
            using(var mock = new MockConnector("localhost", 9999, reply1, reply2, reply3, reply4))
            using(var redis = new RedisClient(mock))
            {
                var resp = (redis.Scan(0) as RedisScan<string>).GetEnumerator() as RedisScan<string>.Enumerator;
                for(int i = 0; i < 3; i++)
                {
                    resp.MoveNext();
                    Assert.Equal(3 - i - 1, resp.Items.Length);
                    Assert.Equal(0, resp.Cursor);
                    Assert.Equal("test" + (i + 1), resp.Current);
                }
                Assert.Equal("*2\r\n$4\r\nSCAN\r\n$1\r\n0\r\n", mock.GetMessage());

                redis.Scan(1, pattern: "pattern").ToArray();
                Assert.Equal("*4\r\n$4\r\nSCAN\r\n$1\r\n1\r\n$5\r\nMATCH\r\n$7\r\npattern\r\n", mock.GetMessage());

                redis.Scan(2, count: 5).ToArray();
                Assert.Equal("*4\r\n$4\r\nSCAN\r\n$1\r\n2\r\n$5\r\nCOUNT\r\n$1\r\n5\r\n", mock.GetMessage());

                redis.Scan(3, pattern: "pattern", count: 5).ToArray();
                Assert.Equal("*6\r\n$4\r\nSCAN\r\n$1\r\n3\r\n$5\r\nMATCH\r\n$7\r\npattern\r\n$5\r\nCOUNT\r\n$1\r\n5\r\n", mock.GetMessage());
            }


            this.RealCall(redis =>
            {
                var dict = new RedisDictionary();
                for(int i = 1; i < 21; i++)
                {
                    dict.Add("key" + i, "value" + i);
                }
                redis.MSet(dict);
                Assert.Equal(20, redis.Scan().Count());
            });
        }
Ejemplo n.º 33
0
        static public async Task RunAsync()
        {
            // string connectionString = "127.0.0.1:30001,127.0.0.1:30002,127.0.0.1:30003,127.0.0.1:30004,127.0.0.1:30005,127.0.0.1:30006";
            string connectionStrings = "localhost:6379";

            var configuration = new ConfigurationOptions
            {
                AbortOnConnectFail = false
            };

            try
            {
                using (var redisConnection = new RedisConnection(connectionStrings, configuration))
                {
                    var d     = new RedisDictionary <string, Crap>(redisConnection.GetDatabase(), "test", new JsonRedisSerializer()); // new MsgPackRedisSerializer());
                    var dtest = new Dictionary <int, string>();
                    dtest.Add(1, "Ross");
                    dtest.Add(2, "Tammy");

                    var v1 = await GetAsync(d, "age", name => 2);
                    await SetAsync(d, "age", (v1 + 2));

                    v1 = await GetAsync(d, "age", name => 3);

                    var v2 = await GetAsync(d, "dtest", name => dtest);

                    v2 = await GetAsync(d, "dtest", name => dtest);



                    ////var v1 = await GetAsync(d, "age", name => (decimal)2);
                    ////await SetAsync(d, "age", (v1 + 2));
                    ////v1 = await GetAsync(d, "age", name => (decimal)3);

                    //return;

                    var lockAcquisition = new RedisCacheKeyLockAcquisition();
                    var cacheContainer  = new StringRedisCacheContainer(redisConnection, "RedisCache"); // new HashRedisCacheContainer(redisConnection);
                    var cacheSeriizer   = new MsgPackRedisSerializer();                                 // new JsonRedisSerializer();
                    var cache           = new RedisCache(redisConnection, lockAcquisition, cacheContainer, cacheSeriizer);

                    var xxx = await cache.GetAsync("app:app:00000000-0000-0000-0000-0000000000a1", async (name, args) =>
                    {
                        var app = new AppEntity
                        {
                            AppId            = Guid.NewGuid(),
                            CreatedDateTime  = DateTime.Now,
                            ModifiedDateTime = DateTime.Now,
                            Description      = "Hello",
                            Enabled          = true,
                            Name             = "test",
                            TenantId         = Guid.Empty,
                        };

                        await Task.Delay(0);

                        return(new CacheDataInfo <AppEntity>
                        {
                            KeepAlive = TimeSpan.FromHours(1),
                            NewCacheData = app, //"Hello, Ross"
                        });

                        // return (CacheDataInfo<string>)null;
                    });

                    // cache.Remove("test");


                    //CacheThreadTest(redisConnection);
                    //LockThreadTest(redisConnection);
                    //SerializerTests(redisConnection);
                    //Test4(redisConnection);
                    //Test1(redisConnection);
                    //Test3(redisConnection);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }
Ejemplo n.º 34
0
        /// <summary>
        /// 同时设置一个或多个键值。
        /// </summary>
        /// <param name="client">Redis 客户端。</param>
        /// <param name="keyValues">键值的字典。</param>
        /// <returns>一个结果。</returns>
        public static Result MSet(this IRedisClient client, RedisDictionary keyValues)
        {
            if(client == null) throw new ArgumentNullException(nameof(client));
            if(keyValues == null) throw new ArgumentNullException(nameof(keyValues));
            if(keyValues.Count == 0) return Result.Successfully;

            var args = RedisArgs.Parse(keyValues).ToArray();
            return client.Execute(new RedisStatus("MSET", args));
        }
Ejemplo n.º 35
0
 private void InitializePersistantDataObjects()
 {
     partners = new Dictionary<string, Gateway>();
     var redisClient = (RedisClient)redis.GetClient();
     originatingPartnerByTrip = new RedisDictionary<string, string>(redisClient, ID + ":" + MemberInfoGetting.GetMemberName(() => originatingPartnerByTrip));
     originatingPartnerByTrip.Clear();
     servicingPartnerByTrip = new RedisDictionary<string, string>(redisClient, ID + "." + MemberInfoGetting.GetMemberName(() => servicingPartnerByTrip));
     servicingPartnerByTrip.Clear();
     partnerCoverage = new RedisDictionary<string, List<Zone>>(redisClient, ID + "." + MemberInfoGetting.GetMemberName(() => partnerCoverage));
     partnerCoverage.Clear();
     partnerAccounts.Clear();
     clientIdByAccessToken.Clear();
 }
Ejemplo n.º 36
0
 public RedisReadModelRepository()
 {
     _redisTypeFactory  = new RedisTypeFactory(RedisManager.Manager);
     _readModelHandlers = _redisTypeFactory.GetDictionary <string, ReadModelHandler>("ReadModelHandlersUpdated");
 }