Exemplo n.º 1
0
        private object interceptUsingCache(MethodInfo method, object[] arguments)
        {
            var key = _cacheKey.Key(_configurationForType.ComponentType, _cachingComponent, method, arguments);
            var eventInformation = new EventInformation(key, _configurationForType.ComponentType.ConfiguredType, method, arguments);

            if (key == null)
            {
                return(callOriginalMethod(method, arguments));
            }
            var cachedValue = _cache.Get(eventInformation);

            if (cachedValue != null)
            {
                return(cachedValue);
            }
            var lockObject = _lockObjectGenerator.GetFor(key);

            if (lockObject == null)
            {
                return(executeAndPutInCache(method, arguments, eventInformation));
            }
            lock (lockObject)
            {
                var cachedValue2 = _cache.Get(eventInformation);
                return(cachedValue2 ?? executeAndPutInCache(method, arguments, eventInformation));
            }
        }
Exemplo n.º 2
0
        public void Get_KeyIsNull_ArgumentNullExceptionIsThrown()
        {
            var cacheMock = new Mock <ICache <object> >();
            var sut       = new CacheAdapter(cacheMock.Object);

            Assert.Throws <ArgumentNullException>(() => sut.Get(null));
        }
Exemplo n.º 3
0
        private bool tryGetValueFromCache(IInvocation invocation, EventInformation eventInfo)
        {
            var cachedValue = _cache.Get(eventInfo);

            if (cachedValue != null)
            {
                invocation.ReturnValue = cachedValue;
                return(true);
            }
            return(false);
        }
Exemplo n.º 4
0
        public void Get_KeyIsValid_CacheGetIsCalled()
        {
            var cacheMock = new Mock <ICache <object> >();

            cacheMock.Setup(x => x.Get("key")).Returns("value");
            var sut = new CacheAdapter(cacheMock.Object);

            var result = sut.Get("key") as string;

            Assert.Equal("value", result);
            cacheMock.Verify(x => x.Get("key"), Times.Once);
        }
Exemplo n.º 5
0
        static void Main(string[] args)
        {
            ICacheAdapter       cacheAdapter       = new CacheAdapter();
            ICustomerRepository customerRepository = new CustomerRepository();

            var customerService = new CustomerService(customerRepository, cacheAdapter);

            customerService.GetAllCustomers();

            var cachedCustomers = (List <Customer>)cacheAdapter.Get(customerService.StorageKey);

            cachedCustomers.ForEach(x => Console.WriteLine(x.FullName));
        }