Exemplo n.º 1
0
        public async Task <T> GetAsync <T>(string key, Func <Task <T> > dataRetriever, TimeSpan?timeToLive) where T : class
        {
            TimeSpan?staleTtl = null;

            if (timeToLive.HasValue)
            {
                staleTtl = timeToLive.Value.Add(_staleDuration);
            }

            var item = await _redisCache.GetAsync(key, dataRetriever, staleTtl);

            var ttl = await _database.KeyTimeToLiveAsync(key);

            if (!ttl.HasValue || ttl.Value < _staleDuration)
            {
                ttl = ttl == null
                    ? _staleDuration.Add(_staleDuration)
                    : ttl.Value.Add(_staleDuration);

                await _database.KeyExpireAsync(key, ttl);

                ThreadPool.QueueUserWorkItem(async o =>
                {
                    try
                    {
                        _redisCache.Add(key, await dataRetriever.Invoke(), staleTtl);
                    }
                    catch
                    { //make sure we do not crash.
                    }
                });
            }
            return(item);
        }
Exemplo n.º 2
0
        public async Task GetAsync_CallsThrough()
        {
            await _subscribingCache.GetAsync("a", typeof(string), null);

            A.CallTo(() => _decoratedCache.GetAsync("a", typeof(string), A <Func <Task <object> > > ._))
            .MustHaveHappened(Repeated.Exactly.Once);
        }
        public async Task GetAsync_CalledOnLocal()
        {
            await _doubleCache.GetAsync("A", typeof(string), null);

            A.CallTo(() => _local.GetAsync("A", A <Type> .Ignored, A <Func <Task <object> > > .Ignored)).MustHaveHappened(Repeated.Exactly.Once);
            A.CallTo(() => _remote.GetAsync("A", A <Type> .Ignored, A <Func <Task <object> > > .Ignored)).MustNotHaveHappened();
        }
Exemplo n.º 4
0
        public void GetAsync_CallsRemoteCache()
        {
            var cacheSubscriber = new RedisSubscriber(_connection, _remoteCache, _itemSerializer);

            cacheSubscriber.GetAsync("A", typeof(string));

            A.CallTo(() => _remoteCache.GetAsync("A", typeof(string), A <Func <Task <object> > > ._)).MustHaveHappened(Repeated.Exactly.Once);
        }
Exemplo n.º 5
0
        public Task <object> GetAsync(string key, Type type, Func <Task <object> > dataRetriever)
        {
            Func <Task <object> > wrappedAction = async() => {
                var result = await dataRetriever.Invoke();

                var qualifiedTypeName = result.GetType().AssemblyQualifiedName;
                _cachePublisher.NotifyUpdate(key, qualifiedTypeName);
                return(result);
            };

            return(_cache.GetAsync(key, type, wrappedAction));
        }
        public async Task GetAsync_Decorated_Called()
        {
            var fakeAction = A.Fake <Func <Task <object> > >();
            var result     = await _publishingCache.GetAsync("a", typeof(string), fakeAction);

            A.CallTo(() => _decoratedCache.GetAsync("a", typeof(string), A <Func <Task <object> > > ._)).MustHaveHappened(Repeated.AtLeast.Once);
        }
Exemplo n.º 7
0
        public async Task <object> GetAsync(string key, Type type, Func <Task <object> > dataRetriever)
        {
            bool   executed                     = false;
            string qualifiedTypeName            = null;
            Func <Task <object> > wrappedAction = async() => {
                var dataRetrieverResult = await dataRetriever.Invoke();

                qualifiedTypeName = dataRetrieverResult.GetType().AssemblyQualifiedName;
                executed          = true;
                return(dataRetrieverResult);
            };

            var result = await _cache.GetAsync(key, type, wrappedAction);

            if (executed)
            {
                _cachePublisher.NotifyUpdate(key, qualifiedTypeName);
            }

            return(result);
        }
        public async Task GetAsync_Add_CalledBefore_Publish()
        {
            var item = "B";

            await _redispublishingCache.GetAsync <string>("a", () => Task.FromResult(item), TimeSpan.FromMinutes(1));

            A.CallTo(() => _database.StringSet(
                         A <RedisKey> .Ignored,
                         A <RedisValue> .Ignored,
                         A <TimeSpan?> .Ignored,
                         When.Always,
                         CommandFlags.FireAndForget))
            .MustHaveHappened(Repeated.Exactly.Once)
            .Then(A.CallTo(
                      () => _cachePublisher.NotifyUpdate(
                          A <string> ._,
                          A <string> ._,
                          A <TimeSpan?> ._))
                  .MustHaveHappened());
        }
Exemplo n.º 9
0
 public Task <object> GetAsync(string key, Type type, Func <Task <object> > dataRetriever)
 {
     return(_localCache.GetAsync(key, type, () => _remoteCache.GetAsync(key, type, dataRetriever)));
 }
Exemplo n.º 10
0
 public Task <object> GetAsync(string key, Type type, Func <Task <object> > dataRetriever)
 {
     return(_cache.GetAsync(key, type, dataRetriever));
 }
Exemplo n.º 11
0
 public async Task <IHttpActionResult> GetSingle()
 {
     return(Ok(await _pubSubCache.GetAsync(Request.RequestUri.PathAndQuery, () => _repo.GetSingleDummyUser())));
 }
Exemplo n.º 12
0
        public async Task GetAsync_ExistingValue_ReturnsValue()
        {
            _cacheImplementation.Add(_key, "A");

            var result = await _cacheImplementation.GetAsync <string>(_key, null);

            result.ShouldBe("A");
        }
Exemplo n.º 13
0
 public Task <object> GetAsync(string key, Type type)
 {
     return(_remoteCache.GetAsync(key, type, () => null));
 }
Exemplo n.º 14
0
 public Task <object> GetAsync(string key, Type type)
 {
     return(_remoteCache.GetAsync(key, type, () => Task.FromResult <object>(null)));
 }