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); }
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(); }
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); }
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); }
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()); }
public Task <object> GetAsync(string key, Type type, Func <Task <object> > dataRetriever) { return(_localCache.GetAsync(key, type, () => _remoteCache.GetAsync(key, type, dataRetriever))); }
public Task <object> GetAsync(string key, Type type, Func <Task <object> > dataRetriever) { return(_cache.GetAsync(key, type, dataRetriever)); }
public async Task <IHttpActionResult> GetSingle() { return(Ok(await _pubSubCache.GetAsync(Request.RequestUri.PathAndQuery, () => _repo.GetSingleDummyUser()))); }
public async Task GetAsync_ExistingValue_ReturnsValue() { _cacheImplementation.Add(_key, "A"); var result = await _cacheImplementation.GetAsync <string>(_key, null); result.ShouldBe("A"); }
public Task <object> GetAsync(string key, Type type) { return(_remoteCache.GetAsync(key, type, () => null)); }
public Task <object> GetAsync(string key, Type type) { return(_remoteCache.GetAsync(key, type, () => Task.FromResult <object>(null))); }