コード例 #1
0
        public void StaticUsage(bool compressValues, bool useBasic)
        {
            TimeSpan            cacheLifeTime = TimeSpan.FromSeconds(1);
            RedisCachingService service       = RedisHelpers.GetCustomRedis(compressValues: compressValues, useBasic: useBasic, defaultExpireTimeSpanSeconds: (int)cacheLifeTime.TotalSeconds);

            service.Clear();
            Foobar cachable = RedisHelpers.GetCachableObject();

            string   cachableKey    = RedisHelpers.CachableKey;
            DateTime startedCaching = DateTime.UtcNow;

            service.Cache(cachableKey, cachable);
            DateTime endedCaching = DateTime.UtcNow;
            ICachedObject <Foobar> existsRetrievedWrapper = service.Retrieve <Foobar>(cachableKey);

            RedisCachingService    service2 = RedisHelpers.GetCustomRedis(compressValues: compressValues, useBasic: useBasic, defaultExpireTimeSpanSeconds: (int)cacheLifeTime.TotalSeconds);
            ICachedObject <Foobar> existsRetrievedWrapper2 = service2.Retrieve <Foobar>(cachableKey);

            Assert.True(RedisHelpers.CheckFooBarEquality(cachable, existsRetrievedWrapper.Value));
            Assert.True(RedisHelpers.CheckFooBarEquality(existsRetrievedWrapper.Value, existsRetrievedWrapper2.Value));
            // they aren't reference equal
            Assert.NotEqual(existsRetrievedWrapper.Value, existsRetrievedWrapper2.Value);
            service2.Clear();
            Assert.Null(service.Retrieve <Foobar>(cachableKey));
        }
コード例 #2
0
        public void DoubleRetrieve(bool compressValues, bool useBasic)
        {
            RedisCachingService service = RedisHelpers.GetRedis(compressValues: compressValues, useBasic: useBasic);

            service.Clear();
            Foobar cachable = RedisHelpers.GetCachableObject();

            string cachableKey = RedisHelpers.CachableKey;

            service.Cache(cachableKey, cachable);
            ICachedObject <Foobar> existsRetrievedWrapper = service.Retrieve <Foobar>(cachableKey);
            ICachedObject <Foobar> retrievedWrapper       = service.Retrieve <Foobar>(cachableKey);

            Assert.NotNull(existsRetrievedWrapper);
            Assert.NotNull(retrievedWrapper);
            Assert.True(RedisHelpers.CheckFooBarEquality(cachable, existsRetrievedWrapper.Value));
            Assert.True(RedisHelpers.CheckFooBarEquality(existsRetrievedWrapper.Value, retrievedWrapper.Value));
            Assert.Single(service);
        }
コード例 #3
0
        public void Remove(bool compressValues, bool useBasic)
        {
            RedisCachingService service = RedisHelpers.GetRedis(compressValues: compressValues, useBasic: useBasic);

            service.Clear();
            Foobar cachable = RedisHelpers.GetCachableObject();

            string   cachableKey    = RedisHelpers.CachableKey;
            DateTime startedCaching = DateTime.UtcNow;

            service.Cache(cachableKey, cachable);
            DateTime endedCaching = DateTime.UtcNow;
            ICachedObject <Foobar> existsRetrievedWrapper = service.Retrieve <Foobar>(cachableKey);

            service.Remove(cachableKey);
            ICachedObject <Foobar> retrievedWrapper = service.Retrieve <Foobar>(cachableKey);

            Assert.Empty(service);
            Assert.NotNull(existsRetrievedWrapper);
            Assert.Null(retrievedWrapper);
        }
コード例 #4
0
        public void AutomatedCustomExpiration(bool compressValues, bool useBasic)
        {
            RedisCachingService service = RedisHelpers.GetRedis(compressValues: compressValues, useBasic: useBasic);

            service.Clear();
            Foobar cachable = RedisHelpers.GetCachableObject();

            string   cachableKey    = RedisHelpers.CachableKey;
            DateTime startedCaching = DateTime.UtcNow;

            // just setting it long enough that even slow machines can pass it
            service.Cache(cachableKey, cachable, DateTime.UtcNow, DateTime.UtcNow.AddMilliseconds(550));;
            DateTime endedCaching = DateTime.UtcNow;
            ICachedObject <Foobar> existsRetrievedWrapper = service.Retrieve <Foobar>(cachableKey);

            Thread.Sleep(560);
            ICachedObject <Foobar> retrievedWrapper = service.Retrieve <Foobar>(cachableKey);

            Assert.NotNull(existsRetrievedWrapper);
            Assert.Null(retrievedWrapper);
        }
コード例 #5
0
        public void AutomatedExpiration(bool compressValues, bool useBasic)
        {
            TimeSpan            cacheLifeTime = TimeSpan.FromSeconds(1);
            RedisCachingService service       = RedisHelpers.GetCustomRedis(compressValues: compressValues, useBasic: useBasic, defaultExpireTimeSpanSeconds: (int)cacheLifeTime.TotalSeconds);

            service.Clear();
            Foobar cachable = RedisHelpers.GetCachableObject();

            string   cachableKey    = RedisHelpers.CachableKey;
            DateTime startedCaching = DateTime.UtcNow;

            service.Cache(cachableKey, cachable);
            DateTime endedCaching = DateTime.UtcNow;
            ICachedObject <Foobar> existsRetrievedWrapper = service.Retrieve <Foobar>(cachableKey);

            Thread.Sleep(1000);
            ICachedObject <Foobar> retrievedWrapper = service.Retrieve <Foobar>(cachableKey);

            Assert.NotNull(existsRetrievedWrapper);
            Assert.Null(retrievedWrapper);
        }
コード例 #6
0
        public void ManualExpiration(bool compressValues, bool useBasic)
        {
            TimeSpan            cacheLifeTime = TimeSpan.FromMinutes(5);
            RedisCachingService service       = RedisHelpers.GetRedis(compressValues: compressValues, useBasic: useBasic);

            service.Clear();
            Foobar cachable = RedisHelpers.GetCachableObject();

            string   cachableKey    = RedisHelpers.CachableKey;
            DateTime startedCaching = DateTime.UtcNow;

            service.Cache(cachableKey, cachable);
            DateTime endedCaching = DateTime.UtcNow;
            ICachedObject <Foobar> existsRetrievedWrapper = service.Retrieve <Foobar>(cachableKey);

            service.Invalidate(cachableKey);
            ICachedObject <Foobar> retrievedWrapper = service.Retrieve <Foobar>(cachableKey);

            Assert.Empty(service);
            Assert.NotNull(existsRetrievedWrapper);
            Assert.Null(retrievedWrapper);
        }
コード例 #7
0
        public void AddKvp(bool compressValues, bool useBasic)
        {
            TimeSpan            cacheLifeTime = TimeSpan.FromMinutes(5);
            RedisCachingService service       = RedisHelpers.GetRedis(compressValues: compressValues, useBasic: useBasic);

            service.Clear();
            Foobar cachable    = RedisHelpers.GetCachableObject();
            string cachableKey = RedisHelpers.CachableKey;
            KeyValuePair <string, ICachedObjectBasic> item = RedisHelpers.GetCachableKvp(DateTime.UtcNow, cacheLifeTime, cachable, cachableKey);

            service.Add(item);
            ICachedObject <Foobar> result = service.Retrieve <Foobar>(cachableKey);

            Assert.True(RedisHelpers.CheckFooBarEquality(cachable, result.Value));
        }
コード例 #8
0
        public void Clear(bool compressValues, bool useBasic)
        {
            RedisCachingService service = RedisHelpers.GetRedis(compressValues: compressValues, useBasic: useBasic);

            service.Clear();
            string cachableKey = RedisHelpers.CachableKey;
            Foobar cachable    = RedisHelpers.GetCachableObject();

            service.Cache <Foobar>(cachableKey, cachable);
            Assert.Single(service);
            ICachedObject <Foobar> cached = service.Retrieve <Foobar>(cachableKey);

            Assert.True(RedisHelpers.CheckFooBarEquality(cachable, cached.Value));

            service.Clear();
            Assert.Empty(service);
        }
コード例 #9
0
        public void Retrieval(bool compressValues, bool useBasic)
        {
            TimeSpan            cacheLifeTime = TimeSpan.FromMinutes(5);
            RedisCachingService service       = RedisHelpers.GetRedis(compressValues: compressValues, useBasic: useBasic);

            service.Clear();
            Foobar cachable = RedisHelpers.GetCachableObject();

            string   cachableKey    = RedisHelpers.CachableKey;
            DateTime startedCaching = DateTime.UtcNow;

            Thread.Sleep(10);
            service.Cache(cachableKey, cachable);
            Thread.Sleep(10);
            DateTime endedCaching = DateTime.UtcNow;

            ICachedObject <Foobar> retrievedWrapper = service.Retrieve <Foobar>(cachableKey);

            Assert.NotNull(retrievedWrapper);
            Assert.True(RedisHelpers.CheckFooBarEquality(cachable, retrievedWrapper.Value));
            Assert.True(retrievedWrapper.CachedTime >= startedCaching && retrievedWrapper.CachedTime <= endedCaching);
            Assert.True(retrievedWrapper.ExpireTime >= startedCaching.Add(cacheLifeTime) &&
                        retrievedWrapper.ExpireTime <= endedCaching.Add(cacheLifeTime));
        }
コード例 #10
0
        public void CopyTo(bool compressValues, bool useBasic)
        {
            RedisCachingService service = RedisHelpers.GetRedis(compressValues: compressValues, useBasic: useBasic);

            service.Clear();
            Foobar cachable = RedisHelpers.GetCachableObject();

            string cachableKey = RedisHelpers.CachableKey;

            service.Cache(cachableKey, cachable);
            ICachedObject <Foobar> existsRetrievedWrapper = service.Retrieve <Foobar>(cachableKey);

            KeyValuePair <string, ICachedObjectBasic>[] cache = new KeyValuePair <string, ICachedObjectBasic> [1];
            service.CopyTo(cache, 0);
            Assert.NotNull(cache);
            Assert.NotEmpty(cache);
            Assert.Single(cache);
            Foobar returned = RedisHelpers.SerializationService.DeserializeObject <Foobar>((string)cache[0].Value.UntypedValue, RedisHelpers.SerializationSettings);

            Assert.NotNull(returned);
            Assert.NotEqual(cachable, returned);
            Assert.True(RedisHelpers.CheckFooBarEquality(existsRetrievedWrapper.Value, returned));
            Assert.Equal(cachableKey, ((RedisId)cache[0].Key).ObjectIdentifier);
        }