public void Resource_is_cached_indefinitely()
        {
            var cacheProvider = RedisCaches.NewRedisCacheProvider()
                                .WithRedisConnection(this.fixture.Connection)
                                .Build();

            this.CreateClient(cacheProvider);
            this.fakeHttpClient.SetupGet("/accounts/foobarAccount", 200, FakeJson.Account);

            // Prime the cache
            var account = this.client.GetResource <IAccount>("https://api.stormpath.com/v1/accounts/foobarAccount");

            // All cache hits
            this.client.GetResource <IAccount>(account.Href);
            this.client.GetResource <IAccount>(account.Href);
            this.client.GetResource <IAccount>(account.Href);

            var db     = this.fixture.Connection.GetDatabase();
            var key    = TestHelper.CreateKey(account);
            var cached = db.StringGetWithExpiry(key);

            cached.Expiry.ShouldBeNull(); // No TTI
            cached.Value.ToString().ShouldNotBeNullOrEmpty();
            this.fakeHttpClient.Calls.Count.ShouldBe(1);
        }
        public void Resource_with_custom_configuration_expired_by_TTI()
        {
            // Make the default TTL 10 minutes, but IAccounts expire in 1 second
            var cacheProvider = RedisCaches.NewRedisCacheProvider()
                                .WithRedisConnection(this.fixture.Connection)
                                .WithDefaultTimeToIdle(TimeSpan.FromMinutes(10))
                                .WithCache(Caches.ForResource <IAccount>()
                                           .WithTimeToIdle(TimeSpan.FromSeconds(1)))
                                .Build();

            this.CreateClient(cacheProvider);
            this.fakeHttpClient.SetupGet("/groups/group1", 200, FakeJson.Group);
            this.fakeHttpClient.SetupGet("/accounts/foobarAccount", 200, FakeJson.Account);

            var group   = this.client.GetResource <IGroup>("https://api.stormpath.com/v1/groups/group1");
            var account = this.client.GetResource <IAccount>("https://api.stormpath.com/v1/accounts/foobarAccount");

            this.fakeHttpClient.Calls.Count.ShouldBe(2);

            Thread.Sleep(1500);

            this.client.GetResource <IAccount>(account.Href);
            this.fakeHttpClient.Calls.Count.ShouldBe(3);

            this.client.GetResource <IGroup>(group.Href);
            this.fakeHttpClient.Calls.Count.ShouldBe(3);
        }
Esempio n. 3
0
        public async Task Resource_expired_by_TTI()
        {
            var cacheProvider = RedisCaches.NewRedisCacheProvider()
                                .WithRedisConnection(this.fixture.Connection)
                                .WithDefaultTimeToIdle(TimeSpan.FromSeconds(1))
                                .Build();

            this.CreateClient(cacheProvider);
            this.fakeHttpClient.SetupGet("/accounts/foobarAccount", 200, FakeJson.Account);

            var account = await this.client.GetResourceAsync <IAccount>("https://api.stormpath.com/v1/accounts/foobarAccount");

            await this.client.GetResourceAsync <IAccount>(account.Href);

            await this.client.GetResourceAsync <IAccount>(account.Href);

            this.fakeHttpClient.Calls.Count.ShouldBe(1);

            await Task.Delay(1500);

            await this.client.GetResourceAsync <IAccount>(account.Href);

            await this.client.GetResourceAsync <IAccount>(account.Href);

            this.fakeHttpClient.Calls.Count.ShouldBe(2);
        }
        public void Resource_expired_by_TTL()
        {
            var cacheProvider = RedisCaches.NewRedisCacheProvider()
                                .WithRedisConnection(this.fixture.Connection)
                                .WithDefaultTimeToLive(TimeSpan.FromSeconds(1))
                                .Build();

            this.CreateClient(cacheProvider);
            this.fakeHttpClient.SetupGet("/accounts/foobarAccount", 200, FakeJson.Account);

            var account = this.client.GetResource <IAccount>("https://api.stormpath.com/v1/accounts/foobarAccount");

            this.client.GetResource <IAccount>(account.Href);
            this.client.GetResource <IAccount>(account.Href);
            this.fakeHttpClient.Calls.Count.ShouldBe(1);

            Thread.Sleep(1500);

            this.client.GetResource <IAccount>(account.Href);
            this.client.GetResource <IAccount>(account.Href);
            this.fakeHttpClient.Calls.Count.ShouldBe(2);
        }
Esempio n. 5
0
 /// <summary>
 /// 获取Redis缓存
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="key"></param>
 /// <returns></returns>
 public static T RedisCacheGet <T>(object key)
 {
     return(RedisCaches.StringGet <T>(key.ToString()));
 }
Esempio n. 6
0
 /// <summary>
 /// 添加Redis缓存
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="key"></param>
 /// <param name="value"></param>
 /// <param name="MinutesOrSecond"></param>
 /// <param name="UseSecond"></param>
 public static void RedisCacheSet <T>(string key, T value, int MinutesOrSecond = 5, bool UseSecond = false)
 {
     RedisCaches.StringSet <T>(key, value, (UseSecond ? (DateTime.Now.AddSeconds(MinutesOrSecond) - DateTime.Now) : (DateTime.Now.AddMinutes(MinutesOrSecond) - DateTime.Now)));
 }
Esempio n. 7
0
 /// <summary>
 /// 删除Redis缓存
 /// </summary>
 /// <param name="key"></param>
 public static async Task RedisCacheRemoveAsync(object key)
 {
     await RedisCaches.KeyDeleteAsync(key.ToString());
 }
Esempio n. 8
0
 /// <summary>
 /// 获取Redis缓存
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="key"></param>
 /// <returns></returns>
 public static async Task <T> RedisCacheGetAsync <T>(object key)
 {
     return(await RedisCaches.StringGetAsync <T>(key.ToString()));
 }
Esempio n. 9
0
 /// <summary>
 /// 添加Redis缓存
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="key"></param>
 /// <param name="value"></param>
 /// <param name="MinutesOrSecond"></param>
 /// <param name="UseSecond"></param>
 /// <returns></returns>
 public static async Task RedisCacheSetAsync <T>(string key, T value, int MinutesOrSecond = 5, bool UseSecond = false)
 {
     await RedisCaches.StringSetAsync <T>(key, value, (UseSecond ? (DateTime.Now.AddSeconds(MinutesOrSecond) - DateTime.Now) : (DateTime.Now.AddMinutes(MinutesOrSecond) - DateTime.Now)));
 }
Esempio n. 10
0
 /// <summary>
 /// 删除Redis缓存
 /// </summary>
 /// <param name="key"></param>
 public static void RedisCacheRemove(object key)
 {
     RedisCaches.KeyDelete(key.ToString());
 }
Esempio n. 11
0
        /// <summary>
        /// 添加Redis缓存
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="MinutesOrSecond"></param>
        /// <param name="UseSecond"></param>
        public static void RedisCacheSet <T>(string key, T value, int MinutesOrSecond = 5, bool UseSecond = false)
        {
            TimeSpan?exp = (UseSecond ? (DateTime.Now.AddSeconds(MinutesOrSecond) - DateTime.Now) : (DateTime.Now.AddMinutes(MinutesOrSecond) - DateTime.Now));

            RedisCaches.StringSet <T>(key, value, MinutesOrSecond == 0?null:exp);
        }
Esempio n. 12
0
 /// <summary>
 /// 添加Redis缓存
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="key"></param>
 /// <param name="value"></param>
 /// <param name="Minutes"></param>
 public static void RedisCacheSet <T>(string key, T value, int Minutes = 5)
 {
     RedisCaches.StringSet <T>(key, value, (DateTime.Now.AddMinutes(Minutes) - DateTime.Now));
 }