public void StringsWithSameContentHaveSameHashCode()
        {
            var string1 = new RedisString("test");
            var string2 = new RedisString("test");

            Assert.Equal(string1.GetHashCode(), string2.GetHashCode());
        }
Example #2
0
        static void Main(string[] args)
        {
            RedisManager.Initialize("localhost:6379,name=Isaac");

            var connect = new RedisString();

            Stopwatch watcher = new Stopwatch();

            string newkey = Guid.NewGuid().ToString();
            int    count  = 0;

            watcher.Start();
            connect.SetStrValue(newkey, "G");

            while (true)
            {
                count++;
                connect.AppendStrValue(newkey, "G");

                if (connect.GetStrValue(newkey).Length > 10000)
                {
                    watcher.Stop();
                    break;
                }
            }

            Console.WriteLine("Done! Takes {0}ms, R/W operates {1} times", watcher.ElapsedMilliseconds, count * 2);
            Console.ReadKey();
        }
Example #3
0
        /// <summary>
        /// 提取客户端相关信息
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        private string ExtractClient(ActionExecutingContext filterContext, out Users user)
        {
            var cookie = CookieHelper.GetCookieValue(ConstBaseData.LoginCookieKey);

            user = RedisString.GetValue <Users>(cookie);

            var str = ((System.Web.HttpRequestWrapper)filterContext.RequestContext.HttpContext.Request).Url.Query.Dencode();

            if (str.Contains("?before_page="))
            {
                str = str.Replace("?before_page=", "");
                if (str != null)
                {
                    var beforePage = str.AsAnyObject <BeforePage>();
                    if (beforePage != null)
                    {
                        CookieHelper.ClearCookie("brefore_page_url");
                        CookieHelper.ClearCookie("brefore_page_title");
                        CookieHelper.SetCookie("brefore_page_url", beforePage.Url);
                        CookieHelper.SetCookie("brefore_page_title", beforePage.Title);
                    }
                }
            }

            return(cookie);
        }
Example #4
0
        public void Object()
        {
            var s = new RedisString <MyClass>(GlobalSettings.Default, "CommandTracerTests1");

            s.Settings.GetConnection().GetServer("127.0.0.1:6379").FlushAllDatabases();

            s.Set(null).Result.IsFalse();
            s.Get().Result.HasValue.IsFalse();

            s.Set(new MyClass()).Result.IsTrue();
            s.Get().Result.Value.Array.IsNull();

            s.Set(new MyClass()
            {
                Array = new Nest[0]
            }).Result.IsTrue();
            s.Get().Result.Value.Array.Length.Is(0);


            s.Set(new MyClass()
            {
                Array = new Nest[] { new Nest {
                                         Huga = "hoge"
                                     } }
            }).Result.IsTrue();
            s.Get().Result.Value.Array[0].Huga.Is("hoge");
        }
        /// <summary>
        ///     初始化数据库环境、实例化子类中,所有Set属性
        /// </summary>
        public void Initializer()
        {
            if (IsInitializer)
            {
                return;
            }
            _client       = new RedisClient(_redisConnection);
            IsInitializer = true;

            Connection  = new RedisConnection(_client);
            Hash        = new RedisHash(_client);
            Key         = new RedisKey(_client);
            List        = new RedisList(_client);
            PubSub      = new RedisPubSub(_client);
            Script      = new RedisScript(_client);
            Server      = new RedisServer(_client);
            Set         = new RedisSet(_client);
            SortedSet   = new RedisSortedSet(_client);
            String      = new RedisString(_client);
            Transaction = new RedisTransaction(_client);
            Bit         = new RedisBit(_client);
            Expire      = new RedisExpire(_client);
            Sort        = new RedisSort(_client);
            Number      = new RedisNumber(_client);
        }
Example #6
0
        public override string DoProcess(Request request)
        {
            var redisObject = store.Get(request.args[0]);
            var valToIncBy  = long.Parse(request.args[1]);

            if (redisObject == null)
            {
                var  redisString = new RedisString("0");
                long longValue;
                redisString.IncrementBy(valToIncBy, out longValue);
                store.Set(request.args[0], redisString);
                return(Reply.IntgerReply(longValue));
            }
            if (!redisObject.IsRedisString())
            {
                return(Reply.ErrWrongType());
            }

            var s = redisObject as RedisString;

            if (s.IsConvertibleToLong())
            {
                long longValue;
                var  success = s.IncrementBy(valToIncBy, out longValue);
                return(success ? Reply.IntgerReply(longValue) : Reply.OverFlow());
            }
            return(Reply.ValueNotInt());
        }
Example #7
0
        public static Users GetSession()
        {
            // 获取用户信息
            var   cookie = CookieHelper.GetCookieValue(ConstBaseData.LoginCookieKey);
            Users user   = RedisString.GetValue <Users>(cookie);

            return(user);
        }
        static public async Task <RedisResult <TReturn> > GetValue <TReturn>(string key)
        {
            var defaultExpiry = TimeSpan.FromSeconds(60);
            var redis         = new RedisString <TReturn>(DBRedis.Connection, key, defaultExpiry);
            var cachedObject  = await redis.GetAsync();

            return(cachedObject);
        }
        static public async Task <bool> SetValue <T>(string key, T value) where T : class
        {
            var defaultExpiry = TimeSpan.FromSeconds(60);
            var redis         = new RedisString <T>(DBRedis.Connection, key, defaultExpiry);

            var result = await redis.SetAsync(value);

            return(result);
        }
Example #10
0
        public async Task StringIncrMin()
        {
            var v = new RedisString<int>(settings, "test-incr");
            await v.Set(0);
            (await v.IncrementLimitByMin(10, 100)).Is(100);
            (await v.IncrementLimitByMin(30, 100)).Is(130);
            (await v.IncrementLimitByMin(50, 100)).Is(180);

            (await v.Get()).Value.Is(180);
        }
Example #11
0
        public async Task StringDoubleIncrMax()
        {
            var v = new RedisString<double>(settings, "test-incr");
            await v.Set(0);
            (await v.IncrementLimitByMax(10.5, 105.3)).Is(10.5);
            (await v.IncrementLimitByMax(10.5, 105.3)).Is(21);
            (await v.IncrementLimitByMax(60.5, 105.3)).Is(81.5);
            (await v.IncrementLimitByMax(25.5, 105.3)).Is(105.3);

            (await v.GetValueOrDefault()).Is(105.3);
        }
Example #12
0
        public void StringsWithSameContentAreEqual()
        {
            var string1 = new RedisString("test");
            var string2 = new RedisString("test");

            Assert.True(string1.Equals(string2));
            Assert.True(string2.Equals(string1));

            Assert.True(string1 == string2);
            Assert.True(string2 == string1);
        }
Example #13
0
        public async Task StringDecrMax()
        {
            var v = new RedisString<int>(settings, "test-incr");
            await v.Set(100);
            (await v.IncrementLimitByMax(-10, 50)).Is(50);
            (await v.IncrementLimitByMax(-30, 100)).Is(20);
            (await v.IncrementLimitByMax(-50, 100)).Is(-30);
            (await v.IncrementLimitByMax(-10, 100)).Is(-40);

            (await v.Get()).Value.Is(-40);
        }
Example #14
0
        public async Task StringIncrMin()
        {
            var v = new RedisString <int>(settings, "test-incr");
            await v.Set(0);

            (await v.IncrementLimitByMin(10, 100)).Is(100);
            (await v.IncrementLimitByMin(30, 100)).Is(130);
            (await v.IncrementLimitByMin(50, 100)).Is(180);

            (await v.GetValueOrDefault()).Is(180);
        }
Example #15
0
        public void StringsWithDifferentContentAreNotEqual()
        {
            var string1 = new RedisString("test1");
            var string2 = new RedisString("test2");

            Assert.False(string1.Equals(string2));
            Assert.False(string2.Equals(string1));

            Assert.True(string1 != string2);
            Assert.True(string2 != string1);
        }
Example #16
0
        public void Redis_Cluster_Set_And_Change_Channel_Test()
        {
            RedisBoss.SetConnection("127.0.0.1:7000,127.0.0.1:7001");

            var rs = new RedisString();

            rs.Set("key", "value");

            RedisBoss.SetConnection("127.0.0.1:7003,127.0.0.1:7004");

            var val = rs.Get("key");
        }
        public void Get_Set()
        {
            var s = new RedisString <int>(GlobalSettings.Default, "RedisStringTest.Get_Set");

            s.Settings.GetConnection().GetServer("127.0.0.1:6379").FlushAllDatabases();

            s.Get().Result.HasValue.IsFalse();

            s.Set(1000).Result.IsTrue();

            s.Get().Result.Value.Is(1000);
        }
Example #18
0
        public async Task StringDoubleIncrMax()
        {
            var v = new RedisString <double>(settings, "test-incr");
            await v.Set(0);

            (await v.IncrementLimitByMax(10.5, 105.3)).Is(10.5);
            (await v.IncrementLimitByMax(10.5, 105.3)).Is(21);
            (await v.IncrementLimitByMax(60.5, 105.3)).Is(81.5);
            (await v.IncrementLimitByMax(25.5, 105.3)).Is(105.3);

            (await v.GetValueOrDefault()).Is(105.3);
        }
Example #19
0
 public static void DeleteStringNoReturn <T>(string key)
 {
     try
     {
         var redis = new RedisString <T>(redisGroupBasic, key);
         redis.Delete();
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex.Message);
     }
 }
Example #20
0
 public static void SetStringNoReturn <T>(string key, T dataObject)
 {
     try
     {
         var redis = new RedisString <T>(redisGroupBasic, key);
         redis.Set(dataObject);
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex.Message);
     }
 }
Example #21
0
        public async Task StringDoubleIncrMin()
        {
            var v = new RedisString <double>(settings, "test-incr");
            await v.Set(100);

            (await v.IncrementLimitByMin(-10.5, 0.25)).Is(89.5);
            (await v.IncrementLimitByMin(-10.5, 0.25)).Is(79.0);
            (await v.IncrementLimitByMin(-60.5, 0.25)).Is(18.5);
            (await v.IncrementLimitByMin(-25.5, 0.25)).Is(0.25);

            (await v.GetValueOrDefault()).Is(0.25);
        }
        public void BitCount()
        {
            var s = new RedisString<int>(GlobalSettings.Default, "test-bitcount");
            s.Delete().Wait();

            s.SetBit(7, true).Result.Is(false);
            s.GetBit(0).Result.Is(false);
            s.GetBit(7).Result.Is(true);
            s.GetBit(100).Result.Is(false);

            s.SetBit(7, false).Result.Is(true);
        }
Example #23
0
        public async Task StringIncrMaxWithExpiry()
        {
            var v = new RedisString<int>(settings, "test-incr2");

            await v.Set(0);
            (await v.IncrementLimitByMax(10, 100)).Is(10);
            (await v.IncrementLimitByMax(30, 100)).Is(40);
            (await v.IncrementLimitByMax(50, 100, TimeSpan.FromSeconds(1))).Is(90);
            (await v.Get()).HasValue.IsTrue();
            await Task.Delay(TimeSpan.FromMilliseconds(1500));
            (await v.Get()).HasValue.IsFalse();
        }
Example #24
0
        public async Task StringDecrMax()
        {
            var v = new RedisString <int>(settings, "test-incr");
            await v.Set(100);

            (await v.IncrementLimitByMax(-10, 50)).Is(50);
            (await v.IncrementLimitByMax(-30, 100)).Is(20);
            (await v.IncrementLimitByMax(-50, 100)).Is(-30);
            (await v.IncrementLimitByMax(-10, 100)).Is(-40);

            (await v.GetValueOrDefault()).Is(-40);
        }
        public void Nullable()
        {
            var s = new RedisString<int?>(GlobalSettings.Default, "CommandTracerTests1");
            s.Settings.GetConnection().GetServer("127.0.0.1:6379").FlushAllDatabases();

            s.Set(null).Result.IsFalse();
            s.Get().Result.HasValue.IsFalse();

            s.Set(100).Result.IsTrue();
            s.Get().Result.Value.Is(100);

            s.Increment(200).Result.Is(300);
        }
        public void BitCount()
        {
            var s = new RedisString <int>(GlobalSettings.Default, "test-bitcount");

            s.Delete().Wait();

            s.SetBit(7, true).Result.Is(false);
            s.GetBit(0).Result.Is(false);
            s.GetBit(7).Result.Is(true);
            s.GetBit(100).Result.Is(false);

            s.SetBit(7, false).Result.Is(true);
        }
Example #27
0
        public void Nullable()
        {
            var s = new RedisString <int?>(GlobalSettings.Default, "CommandTracerTests1");

            s.Settings.GetConnection().GetServer("127.0.0.1:6379").FlushAllDatabases();

            s.Set(null).Result.IsFalse();
            s.Get().Result.HasValue.IsFalse();

            s.Set(100).Result.IsTrue();
            s.Get().Result.Value.Is(100);

            s.Increment(200).Result.Is(300);
        }
Example #28
0
        public async Task StringIncrMaxWithExpiry()
        {
            var v = new RedisString <int>(settings, "test-incr2");

            await v.Set(0);

            (await v.IncrementLimitByMax(10, 100)).Is(10);
            (await v.IncrementLimitByMax(30, 100)).Is(40);
            (await v.IncrementLimitByMax(50, 100, TimeSpan.FromSeconds(1))).Is(90);
            (await v.Get()).HasValue.IsTrue();
            await Task.Delay(TimeSpan.FromMilliseconds(1500));

            (await v.Get()).HasValue.IsFalse();
        }
        public void Bit()
        {
            var s = new RedisString<int>(GlobalSettings.Default, "test-bit");
            s.Delete().Wait();

            var db = s.Settings.GetConnection().GetDatabase();

            s.SetBit(7, true).Result.Is(false);
            s.GetBit(0).Result.Is(false);
            s.GetBit(7).Result.Is(true);
            s.GetBit(100).Result.Is(false);

            s.SetBit(7, false).Result.Is(true);
        }
        public void Incr()
        {
            var s = new RedisString <int>(GlobalSettings.Default, "test-incr");

            s.Delete().Wait();

            s.Increment(100).Result.Is(100);

            s.Increment(100, TimeSpan.FromSeconds(1)).Result.Is(200);

            s.Get().Result.HasValue.IsTrue();
            Thread.Sleep(TimeSpan.FromSeconds(2));
            s.Get().Result.HasValue.IsFalse();
        }
Example #31
0
 public static Int64 Increment(string key, Int64 value)
 {
     try
     {
         var redis  = new RedisString <Int64>(redisGroupBasic, key);
         var result = redis.Increment(value);
         return(result.Result);
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex.Message);
         return(0);
     }
 }
Example #32
0
        // float은 사용불가. 대신 double 사용 가능

        public static async Task <bool> SetString <T>(string key, T dataObject)
        {
            try
            {
                var redis = new RedisString <T>(redisGroupBasic, key);
                await redis.Set(dataObject);

                return(true);
            }
            catch (Exception ex)
            {
                Logger.Exception(ex.Message);
                return(false);
            }
        }
Example #33
0
        public static async Task <Tuple <bool, T> > GetStringAsync <T>(string key)
        {
            try
            {
                var redis = new RedisString <T>(_redisGroupBasic, key);
                var value = await redis.Get();

                return(value.Value == null?Tuple.Create(false, default(T)) : Tuple.Create(true, value.Value));
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                throw;
            }
        }
        // float을 사용할 수 없다(Redis). 대신 double을 사용하자.
        public static async Task <bool> SetStringAsync <T>(string key, T dataObject)
        {
            try
            {
                var redis = new RedisString <T>(redisGroupBasic, key);
                await redis.Set(dataObject);

                return(true);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return(false);
            }
        }
        public static async Task <bool> DeleteStringAsync <T>(string key)
        {
            try
            {
                var redis  = new RedisString <T>(redisGroupBasic, key);
                var result = await redis.Delete();

                return(result);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                throw;
            }
        }
        public static async Task <bool> SetStringAsyncWhenNoExists <T>(string key, T dataObject)
        {
            try
            {
                var redis  = new RedisString <T>(redisGroupBasic, key);
                var result = await redis.Set(dataObject, null, StackExchange.Redis.When.NotExists);

                return(result);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return(false);
            }
        }
        public void Bit()
        {
            var s = new RedisString <int>(GlobalSettings.Default, "test-bit");

            s.Delete().Wait();

            var db = s.Settings.GetConnection().GetDatabase();

            s.SetBit(7, true).Result.Is(false);
            s.GetBit(0).Result.Is(false);
            s.GetBit(7).Result.Is(true);
            s.GetBit(100).Result.Is(false);

            s.SetBit(7, false).Result.Is(true);
        }
Example #38
0
        public async Task StringDecrMin()
        {
            var v = new RedisString<int>(settings, "test-incr");
            await v.Set(100);
            (await v.IncrementLimitByMin(-10, 0)).Is(90);
            (await v.IncrementLimitByMin(-30, 0)).Is(60);
            (await v.IncrementLimitByMin(-50, 0)).Is(10);
            (await v.IncrementLimitByMin(-10, 0)).Is(0);
            (await v.IncrementLimitByMin(-25, 0)).Is(0);

            (await v.Get()).Value.Is(0);

            await v.Set(50);
            (await v.IncrementLimitByMin(-100, 0)).Is(0);
            (await v.Get()).Value.Is(0);
        }
        public void Object()
        {
            var s = new RedisString<MyClass>(GlobalSettings.Default, "CommandTracerTests1");
            s.Settings.GetConnection().GetServer("127.0.0.1:6379").FlushAllDatabases();

            s.Set(null).Result.IsFalse();
            s.Get().Result.HasValue.IsFalse();

            s.Set(new MyClass()).Result.IsTrue();
            s.Get().Result.Value.Array.IsNull();

            s.Set(new MyClass() { Array = new Nest[0] }).Result.IsTrue();
            s.Get().Result.Value.Array.Length.Is(0);

            s.Set(new MyClass() { Array = new Nest[] { new Nest { Huga = "hoge" } } }).Result.IsTrue();
            s.Get().Result.Value.Array[0].Huga.Is("hoge");
        }
Example #40
0
        public async Task StringIncrMax()
        {
            var v = new RedisString<int>(settings, "test-incr");

            await v.Set(0);
            (await v.IncrementLimitByMax(10, 100)).Is(10);
            (await v.IncrementLimitByMax(30, 100)).Is(40);
            (await v.IncrementLimitByMax(50, 100)).Is(90);
            (await v.IncrementLimitByMax(10, 100)).Is(100);
            (await v.IncrementLimitByMax(1, 100)).Is(100);
            (await v.IncrementLimitByMax(10, 100)).Is(100);

            (await v.Get()).Value.Is(100);

            await v.Set(40);
            (await v.IncrementLimitByMax(100, 100)).Is(100);
            (await v.Get()).Value.Is(100);
        }
        public void GetOrSet()
        {
            var s = new RedisString<int>(GlobalSettings.Default, "test-string");
            s.Delete().Wait();

            var loaded = false;
            s.GetOrSet(() =>
            {
                loaded = true;
                return 1000;
            }).Result.Is(1000);

            loaded.IsTrue();

            s.GetOrSet(() =>
            {
                Assert.Fail();
                return 2000;
            }).Result.Is(1000);
        }
        /// <summary>
        ///     初始化数据库环境、实例化子类中,所有Set属性
        /// </summary>
        public void Initializer()
        {
            if (IsInitializer) { return; }
            _client = new RedisClient(_redisConnection);
            IsInitializer = true;

            Connection = new RedisConnection(_client);
            Hash = new RedisHash(_client);
            Key = new RedisKey(_client);
            List = new RedisList(_client);
            PubSub = new RedisPubSub(_client);
            Script = new RedisScript(_client);
            Server = new RedisServer(_client);
            Set = new RedisSet(_client);
            SortedSet = new RedisSortedSet(_client);
            String = new RedisString(_client);
            Transaction = new RedisTransaction(_client);
            Bit = new RedisBit(_client);
            Expire = new RedisExpire(_client);
            Sort = new RedisSort(_client);
            Number = new RedisNumber(_client);
        }
        public void SetAndGetString()
        {
            // Create RedisString... (or you can use RedisSettings.Default.String<Person>("key"))
            var redis = new RedisString<Person>(RedisServer.Default, StringKey0);
            var person = new Person { Name = "John", Age = 34 };

            // call command(IntelliSense helps you)
            var result = Task.Run(async () => { return await redis.Set(person).ConfigureAwait(false); });

            Assert.IsTrue(result.Result);

            // call command(IntelliSense helps you)
            var copy = Task.Run(async () => { return await redis.Get().ConfigureAwait(false); });

            var copyResult = copy.Result.Value;

            Assert.AreEqual(person, copyResult);
            Assert.AreEqual(person.Name, copyResult.Name);
            Assert.AreEqual(person.Age, copyResult.Age);
        }
        public void Get_Set()
        {
            var s = new RedisString<int>(GlobalSettings.Default, "RedisStringTest.Get_Set");
            s.Settings.GetConnection().GetServer("127.0.0.1:6379").FlushAllDatabases();

            s.Get().Result.HasValue.IsFalse();

            s.Set(1000).Result.IsTrue();

            s.Get().Result.Value.Is(1000);
        }
        public void Incr()
        {
            var s = new RedisString<int>(GlobalSettings.Default, "test-incr");
            s.Delete().Wait();

            s.Increment(100).Result.Is(100);

            s.Increment(100, TimeSpan.FromSeconds(1)).Result.Is(200);

            s.Get().Result.HasValue.IsTrue();
            Thread.Sleep(TimeSpan.FromSeconds(2));
            s.Get().Result.HasValue.IsFalse();
        }
Example #46
0
        public async Task StringDoubleIncrMin()
        {
            var v = new RedisString<double>(settings, "test-incr");
            await v.Set(100);
            (await v.IncrementLimitByMin(-10.5, 0.25)).Is(89.5);
            (await v.IncrementLimitByMin(-10.5, 0.25)).Is(79.0);
            (await v.IncrementLimitByMin(-60.5, 0.25)).Is(18.5);
            (await v.IncrementLimitByMin(-25.5, 0.25)).Is(0.25);

            (await v.Get()).Value.Is(0.25);
        }