Beispiel #1
0
        public override void Run()
        {
            // create and inline-configure a client that works with `Personnel` class
            var client = new RedisClient <Personnel>(settings => settings
                                                     .ReadBufferSize(8192)                                 // large reads
                                                     .WriteBufferSize(256)                                 // small writes
                                                     .EncodingBufferSize(32)                               // encode small stuff
                                                     .SerializationBufferSize(8192)                        // as large as reads
                                                     .OverrideSerializer(new PersonnelSerializer())        // custom serializer
                                                     .OverrideCulture(CultureInfo.GetCultureInfo("th-TH")) // non-default culture
                                                     .OverrideEncoding(Encoding.GetEncoding("tis-620"))    // thai language encoding
                                                     .ReconnectOnIdle(true)                                // auto reconnect on idle
                                                     .ReissueCommandsOnReconnect(false));                  // never retry automatically

            // create some test data
            var personnels = Personnel.GetSamplePersonnels();
            var dataAdded  = client
                             .Pipeline(c =>
            {
                client.Del("p:salary", "p:sal_week", "p:age"); // reset old values
                Array.ForEach(personnels, p => c.ZAdd("p:salary", p.Salary, p));
                Array.ForEach(personnels, p => c.ZAdd("p:sal_week", (double)p.Salary / 4.0, p));
                Array.ForEach(personnels, p => c.ZAdd("p:age", p.Age, p));
            })
                             .Skip(1) // skip DEL result
                             .All(b => (bool)b);

            Debug.Assert(dataAdded, "Data not properly added.");

            while (true)
            {
                WriteLine("0. List personnels sorted by yearly income.");
                WriteLine("1. List personnels sorted by weekly income.");
                WriteLine("2. List personnels sorted by age.");

                Personnel[] result;

                var min = double.NegativeInfinity;
                var max = double.PositiveInfinity;

                switch (ReadLine())
                {
                case "0": result = client.ZRangeByScore("p:salary", min, max); break;

                case "1": result = client.ZRangeByScore("p:sal_week", min, max); break;

                case "2": result = client.ZRangeByScore("p:age", min, max); break;

                default: continue;
                }

                Array.ForEach(result, p => WriteLine(p.ToString()));
            }
        }
Beispiel #2
0
        public override void Run()
        {
            var personnels = Personnel.GetSamplePersonnels();
            var client     = new RedisClient();

            var listKey = "personnels";

            // setup sample data
            client.Pipeline(c =>
            {
                client.Del(listKey);

                foreach (var person in personnels)
                {
                    var idStr = person.Id.ToString();

                    c.LPush(listKey, idStr);
                    c.HSet(idStr, "name", person.Name);
                    c.HSet(idStr, "email", person.Email);
                    c.HIncrBy(idStr, "age", person.Age);
                    c.HIncrBy(idStr, "salary", person.Salary);
                    c.HSet(idStr, "json", person.ToJson());
                }
            });

            while (true)
            {
                WriteLine("0. List personnels sorted by yearly income.");
                WriteLine("1. List personnels sorted by age.");
                WriteLine("2. List personnels sorted by id descending.");
                WriteLine("3. List personnels sorted by name.");
                WriteLine("4. List all personnel emails.");

                string[] result;
                var      parseResult = true;

                switch (ReadLine())
                {
                case "0": {
                    result = client.Sort(listKey,
                                         byPattern: "*->salary",
                                         getPattern: new[] { "*->json" });
                } break;

                case "1": {
                    parseResult = true;
                    result      = client.Sort(listKey,
                                              byPattern: "*->age",
                                              getPattern: new[] { "*->json" });
                } break;

                case "2": {
                    parseResult = true;
                    result      = client.Sort(listKey,
                                              descending: true,
                                              getPattern: new[] { "*->json" });
                } break;

                case "3": {
                    result = client.Sort(listKey,
                                         byPattern: "*->name",
                                         alpha: true,
                                         getPattern: new[] { "*->json" });
                } break;

                case "4": {
                    result = client.Sort(listKey,
                                         byPattern: "nosort", // non-existent key
                                         getPattern: new[] { "*->email" });
                    parseResult = false;
                } break;

                default: continue;
                }

                if (parseResult)
                {
                    result = result
                             .Select(json => Personnel.FromJson(json).ToString())
                             .ToArray();
                }

                WriteLine("\r\nResults:");
                Array.ForEach(result, WriteLine);
                WriteLine("\r\n");
            }
        }