コード例 #1
0
        public void Get_CalledOnLocal()
        {
            _doubleCache.Get("A", typeof(string), null);

            A.CallTo(() => _local.Get("A", A <Type> .Ignored, A <Func <object> > .Ignored)).MustHaveHappened(Repeated.Exactly.Once);
            A.CallTo(() => _remote.Get("A", A <Type> .Ignored, A <Func <object> > .Ignored)).MustNotHaveHappened();
        }
コード例 #2
0
        public T Get <T>(string key, Func <T> dataRetriever, TimeSpan?timeToLive) where T : class
        {
            TimeSpan?staleTtl = null;

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

            var item = _redisCache.Get(key, dataRetriever, staleTtl);
            var ttl  = _database.KeyTimeToLive(key);

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

                _database.KeyExpire(key, ttl);

                ThreadPool.QueueUserWorkItem(o =>
                {
                    try
                    {
                        _redisCache.Add(key, dataRetriever.Invoke(), staleTtl);
                    }
                    catch
                    { //make sure we do not crash.
                    }
                });
            }
            return(item);
        }
コード例 #3
0
        public void Get_ExistingValue_ReturnsValue()
        {
            _cacheImplementation.Add(_key, "A");

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

            result.ShouldBe("A");
        }
コード例 #4
0
 public T Get <T>(string key, Func <T> dataRetriever) where T : class
 {
     return(_cache.Get(key, () => {
         var result = dataRetriever.Invoke();
         _cachePublisher.NotifyUpdate(key, result.GetType().AssemblyQualifiedName);
         return result;
     }));
 }
コード例 #5
0
        public void ExpiredItem_UpdateTtl()
        {
            var key = Guid.NewGuid().ToString();

            TimeSpan?ttl = TimeSpan.FromMinutes(1);

            _staleCache.Add(key, new object());
            _staleCache.Get(key, () => new object());

            A.CallTo(() => _database.KeyExpire(A <RedisKey> ._, ttl, A <CommandFlags> ._)).MustHaveHappened(Repeated.Exactly.Once);
        }
コード例 #6
0
        public void Get_Decorated_Called()
        {
            var fakeAction = A.Fake <Func <object> >();
            var result     = _publishingCache.Get("a", typeof(string), fakeAction);

            A.CallTo(() => _decoratedCache.Get("a", typeof(string), A <Func <object> > ._)).MustHaveHappened(Repeated.AtLeast.Once);
        }
コード例 #7
0
        public T Get <T>(string key, Func <T> dataRetriever) where T : class
        {
            var executed = false;

            var result = _cache.Get(key, () => {
                var dataRetrieverResult = dataRetriever.Invoke();
                executed = true;
                return(dataRetrieverResult);
            });

            if (executed)
            {
                _cachePublisher.NotifyUpdate(key, result.GetType().AssemblyQualifiedName);
            }
            return(result);
        }
コード例 #8
0
        public void Get_Add_CalledBefore_Publish()
        {
            var item = "B";

            _redispublishingCache.Get("a", () => 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());
        }
コード例 #9
0
ファイル: DoubleCache.cs プロジェクト: snap608/DoubleCache
 public T Get <T>(string key, Func <T> dataRetriever) where T : class
 {
     return(_localCache.Get(key, () => _remoteCache.Get(key, dataRetriever)));
 }
コード例 #10
0
 public void Get_CallsThrough()
 {
     _subscribingCache.Get("a", typeof(string), null);
     A.CallTo(() => _decoratedCache.Get("a", typeof(string), A <Func <object> > ._))
     .MustHaveHappened(Repeated.Exactly.Once);
 }
コード例 #11
0
 public T Get <T>(string key, Func <T> dataRetriever) where T : class
 {
     return(_cache.Get(key, dataRetriever));
 }