public async Task Can_retrieve_TimeToLive_on_IAuthSession()
        {
            IAuthSession session = new CustomAuthSession
            {
                Id         = "sess-1",
                UserAuthId = "1",
                Custom     = "custom"
            };

            var sessionKey = SessionFeature.GetSessionKey(session.Id);
            await Cache.RemoveAsync(sessionKey);

            var ttl = await Cache.GetTimeToLiveAsync(sessionKey);

            Assert.That(ttl, Is.Null);

            await Cache.SetAsync(sessionKey, session);

            ttl = await Cache.GetTimeToLiveAsync(sessionKey);

            Assert.That(ttl.Value, Is.EqualTo(TimeSpan.MaxValue));

            var sessionExpiry = SessionFeature.DefaultSessionExpiry;
            await Cache.SetAsync(sessionKey, session, sessionExpiry);

            ttl = await Cache.GetTimeToLiveAsync(sessionKey);

            Assert.That(ttl.Value, Is.GreaterThan(TimeSpan.FromSeconds(0)));
            Assert.That(ttl.Value, Is.LessThan(sessionExpiry).
                        Or.EqualTo(sessionExpiry).Within(TimeSpan.FromSeconds(1)));
        }
Example #2
0
        public async Task Expired_item_returns_correct_GetTimeToLive()
        {
            var key = "int:key";

            var value = await Cache.GetOrCreateAsync(key, TimeSpan.FromMilliseconds(2000), () => Task.FromResult(1));

            var ttl = await Cache.GetTimeToLiveAsync(key);

            Assert.That(value, Is.EqualTo(1));
            Assert.That(ttl.Value.TotalMilliseconds, Is.GreaterThan(0));

            await Cache.RemoveAsync(key);

            value = await Cache.GetAsync <int>(key);

            ttl = await Cache.GetTimeToLiveAsync(key);

            Assert.That(value, Is.EqualTo(0));
            Assert.That(ttl, Is.Null);
        }
Example #3
0
        public async Task Can_GetTimeToLive()
        {
            var    model = ModelWithIdAndName.Create(1);
            string key   = "model:" + model.CreateUrn();
            await cacheClient.AddAsync(key, model);

            var ttl = await cacheClient.GetTimeToLiveAsync(key);

            Assert.That(ttl, Is.EqualTo(TimeSpan.MaxValue));

            await cacheClient.SetAsync(key, model, expiresIn : TimeSpan.FromSeconds(10));

            ttl = await cacheClient.GetTimeToLiveAsync(key);

            Assert.That(ttl.Value, Is.GreaterThanOrEqualTo(TimeSpan.FromSeconds(9)));
            Assert.That(ttl.Value, Is.LessThanOrEqualTo(TimeSpan.FromSeconds(10)));

            await cacheClient.RemoveAsync(key);

            ttl = await cacheClient.GetTimeToLiveAsync(key);

            Assert.That(ttl, Is.Null);
        }
Example #4
0
        public async Task Expired_item_returns_correct_GetTimeToLive()
        {
            var ormliteCache = Cache as OrmLiteCacheClient;
            var key          = "int:key";

            var value = await Cache.GetOrCreateAsync(key, TimeSpan.FromMilliseconds(2000), () => Task.FromResult(1));

            var ttl = await Cache.GetTimeToLiveAsync(key);

            if (ormliteCache != null)
            {
                using var db = ormliteCache.DbFactory.OpenDbConnection();
                var row = await db.SingleByIdAsync <CacheEntry>(key);

                Assert.That(row, Is.Not.Null);
                Assert.That(row.ExpiryDate, Is.Not.Null);
            }

            Assert.That(value, Is.EqualTo(1));
            Assert.That(ttl.Value.TotalMilliseconds, Is.GreaterThan(0));

            await Cache.RemoveAsync(key);

            value = await Cache.GetAsync <int>(key);

            ttl = await Cache.GetTimeToLiveAsync(key);

            Assert.That(value, Is.EqualTo(0));
            Assert.That(ttl, Is.Null);

            if (ormliteCache != null)
            {
                using var db = ormliteCache.DbFactory.OpenDbConnection();
                var row = db.SingleById <CacheEntry>(key);
                Assert.That(row, Is.Null);
            }
        }
Example #5
0
 public async Task <TimeSpan?> GetTimeToLiveAsync(string key, CancellationToken token = default)
 {
     return(await cache.GetTimeToLiveAsync(EnsurePrefix(key), token).ConfigAwait());
 }
Example #6
0
        public static async Task <TimeSpan?> GetSessionTimeToLiveAsync(this ICacheClientAsync cache, string sessionId, CancellationToken token = default)
        {
            var sessionKey = SessionFeature.GetSessionKey(sessionId);

            return(await cache.GetTimeToLiveAsync(sessionKey, token).ConfigAwait());
        }