Beispiel #1
0
        public T GetOrAdd <T>(string key, Func <ICacheEntry, T> addItemFactory)
        {
            ValidateKey(key);

            object cacheItem;

            locker.Wait(); //TODO: do we really need this? Could we just lock on the key?
            try
            {
                cacheItem = CacheProvider.GetOrCreate <object>(key, entry =>
                                                               new Lazy <T>(() =>
                {
                    var result = addItemFactory(entry);
                    EnsureEvictionCallbackDoesNotReturnTheAsyncOrLazy <T>(entry.PostEvictionCallbacks);
                    return(result);
                })
                                                               );
            }
            finally
            {
                locker.Release();
            }

            try
            {
                return(GetValueFromLazy <T>(cacheItem));
            }
            catch //addItemFactory errored so do not cache the exception
            {
                CacheProvider.Remove(key);
                throw;
            }
        }
        public void SlidingExpiredItemIsRemoved()
        {
            var stringKey = CacheKey.Create <string>("hello");
            var myObject1 = CacheProvider.GetOrCreate(stringKey,
                                                      () => new CacheValueOf <TestCacheObject>(new TestCacheObject("hello-1"), new StaticCachePolicy(TimeSpan.FromSeconds(1))));

            Assert.NotNull(myObject1);

            Assert.That(myObject1.AlreadyExisted, Is.False);

            Assert.That(myObject1.WasInserted, Is.True);

            Assert.NotNull(myObject1.Value);

            if (CacheIsAsync)
            {
                Thread.Sleep(500);
            }

            Assert.NotNull(CacheProvider.GetValue <TestCacheObject>(stringKey));

            Assert.NotNull(CacheProvider.Get <TestCacheObject>(stringKey));

            Thread.Sleep(TimeSpan.FromSeconds(1.2d));

            Assert.Null(CacheProvider.Get <TestCacheObject>(stringKey));
        }
        public void WhenAddingNonExistantItemResultShowsItemWasCreated()
        {
            bool check = false;

            var result = CacheProvider.GetOrCreate(
                CacheKey.Create <string>("hello"),
                () =>
            {
                check = true;
                return(new CacheValueOf <TestCacheObject>(new TestCacheObject("whatever")));
            });

            Assert.That(check, Is.True);
            Assert.That(result.AlreadyExisted, Is.False);
            Assert.That(result.WasInserted, Is.True);
            Assert.That(result.WasUpdated, Is.False);
            Assert.That(result.ExistsButWrongType, Is.False);
            Assert.That(result.Value.Item.Text, Is.EqualTo("whatever"));
        }
Beispiel #4
0
        public virtual T GetOrAdd <T>(string key, Func <ICacheEntry, T> addItemFactory)
        {
            ValidateKey(key);

            T cacheItem;

            _lock.Wait();
            try
            {
                cacheItem = CacheProvider.GetOrCreate <T>(key, (entry) =>
                {
                    // thread safety is up to the provider
                    var result = addItemFactory(entry);
                    EnsureEvictionCallbackDoesNotReturnTheAsyncOrLazy <T>(entry.PostEvictionCallbacks);
                    return(result);
                });
            }
            finally
            {
                _lock.Release();
            }

            return(cacheItem);
        }
Beispiel #5
0
 protected TModel GetOrCreateCacheItem <TModel>(string id) where TModel : IModelKey <string>
 {
     return(CacheProvider.GetOrCreate <TModel>(ControllerContext, id));
 }