コード例 #1
0
        public void TestEnumerators(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;

            service.Cache(cachableKey, cachable);

            IEnumerator <KeyValuePair <string, ICachedObjectBasic> > typedEnumerator
                = service.GetEnumerator();
            IEnumerator enumerator = ((IEnumerable)service).GetEnumerator();

            typedEnumerator.MoveNext();
            enumerator.MoveNext();

            Foobar returned = RedisHelpers.SerializationService.DeserializeObject <Foobar>(
                (string)((ICachedObjectBasic)(typedEnumerator.Current).Value).UntypedValue, RedisHelpers.SerializationSettings);

            Assert.NotNull(returned);
            Assert.NotEqual(cachable, returned);
            Assert.True(RedisHelpers.CheckFooBarEquality(cachable, returned));

            Foobar returned2 = RedisHelpers.SerializationService.DeserializeObject <Foobar>(
                (string)((ICachedObjectBasic)((KeyValuePair <string, ICachedObjectBasic>)
                                                  (enumerator.Current)).Value).UntypedValue, RedisHelpers.SerializationSettings);

            Assert.NotNull(returned2);
            Assert.NotEqual(cachable, returned2);
            Assert.True(RedisHelpers.CheckFooBarEquality(cachable, returned2));
        }
コード例 #2
0
        public void Caching(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;

            service.Cache(cachableKey, cachable);
            Assert.Single(service);
            Assert.Equal(cacheLifeTime, service.DefaultCacheLifespan);
            Assert.False(service.IsReadOnly);
            Assert.Single(service.Keys);
            Assert.Equal(cachableKey, ((RedisId)service.Keys.Single()).ObjectIdentifier);
            Assert.Single(service.Values);
            string returnedString = service.Values.Single().UntypedValue as string;

            Assert.False(string.IsNullOrWhiteSpace(returnedString));
            Foobar returned = RedisHelpers.SerializationService.DeserializeObject <Foobar>(returnedString, RedisHelpers.SerializationSettings);

            Assert.NotNull(returned);
            Assert.NotEqual(cachable, returned);
            Assert.True(RedisHelpers.CheckFooBarEquality(cachable, returned));
        }
コード例 #3
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));
        }
コード例 #4
0
        public void ContainsKey(bool compressValues, bool useBasic)
        {
            TimeSpan            cacheLifeTime = TimeSpan.FromMinutes(5);
            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);
            Assert.True(service.ContainsKey(cachableKey));
        }
コード例 #5
0
        public void Count(bool compressValues, bool useBasic)
        {
            RedisCachingService service = RedisHelpers.GetRedis(compressValues: compressValues, useBasic: useBasic);

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

            string cachableKey = RedisHelpers.CachableKey;

            service.Cache(cachableKey, cachable);
            Assert.Single(service);
        }
コード例 #6
0
        public void Keys(bool compressValues, bool useBasic)
        {
            RedisCachingService service = RedisHelpers.GetRedis(compressValues: compressValues, useBasic: useBasic);

            service.Clear();
            Assert.Empty(service.Keys);
            Foobar cachable = RedisHelpers.GetCachableObject();

            string cachableKey = RedisHelpers.CachableKey;

            service.Cache(cachableKey, cachable);
            Assert.Single(service.Keys);
            Assert.Equal(cachableKey, ((RedisId)(service.Keys.Single())).ObjectIdentifier);
        }
コード例 #7
0
        public void DoubleCache(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);
            Foobar cachable2 = RedisHelpers.GetCachableObject();

            service.Cache(cachableKey, cachable2);

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

            Assert.NotNull(retrievedWrapper);
            Assert.True(RedisHelpers.CheckFooBarEquality(cachable, existsRetrievedWrapper.Value));
            Assert.NotEqual(existsRetrievedWrapper.Value, retrievedWrapper.Value);
            Assert.True(RedisHelpers.CheckFooBarEquality(cachable2, retrievedWrapper.Value));
            Assert.True(RedisHelpers.CheckFooBarEquality(retrievedWrapper.Value, existsRetrievedWrapper.Value, false));
            Assert.Single(service);
        }
コード例 #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 Values(bool compressValues, bool useBasic)
        {
            RedisCachingService service = RedisHelpers.GetRedis(compressValues: compressValues, useBasic: useBasic);

            service.Clear();
            Assert.Empty(service.Values);
            Foobar cachable = RedisHelpers.GetCachableObject();

            string cachableKey = RedisHelpers.CachableKey;

            service.Cache(cachableKey, cachable);
            Assert.Single(service.Values);
            Assert.NotNull(service.Values.Single());
            string returnedString = service.Values.Single().UntypedValue as string;

            Assert.False(string.IsNullOrWhiteSpace(returnedString));
            Foobar returned = RedisHelpers.SerializationService.DeserializeObject <Foobar>(returnedString, RedisHelpers.SerializationSettings);

            Assert.True(RedisHelpers.CheckFooBarEquality(cachable, returned));
        }
コード例 #10
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);
        }
コード例 #11
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);
        }
コード例 #12
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);
        }
コード例 #13
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);
        }
コード例 #14
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));
        }
コード例 #15
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);
        }