示例#1
0
        /// <summary>
        /// <see cref="M:Microsoft.Samples.NLayerApp.Infrastructure.CrossCutting.Caching.TryGet{TResult}"/>
        /// </summary>
        /// <typeparam name="TResult"><see cref="M:Microsoft.Samples.NLayerApp.Infrastructure.CrossCutting.Caching.TryGet{TResult}"/></typeparam>
        /// <param name="cacheItemConfig"><see cref="M:Microsoft.Samples.NLayerApp.Infrastructure.CrossCutting.Caching.TryGet{TResult}"/></param>
        /// <param name="result"<see cref="M:Microsoft.Samples.NLayerApp.Infrastructure.CrossCutting.Caching.TryGet{TResult}"/>></param>
        /// <returns><see cref="M:Microsoft.Samples.NLayerApp.Infrastructure.CrossCutting.Caching.TryGet{TResult}"/></returns>
        public bool TryGet <TResult>(CacheItemConfig cacheItemConfig, out TResult result)
        {
            if (cacheItemConfig != null)
            {
                //get default cache
                DataCache defaultCache = _cacheFactory.GetDefaultCache();

                string cacheKey = cacheItemConfig.CacheKey.GetCacheKey();

                //get object from cache and check if exists
                object cachedItem = defaultCache.Get(cacheKey);

                if (cachedItem != null)
                {
                    result = (TResult)cachedItem;

                    return(true);
                }
                else
                {
                    result = default(TResult);

                    return(false);
                }
            }
            else
            {
                throw new ArgumentNullException("cacheItem");
            }
        }
        /// <summary>
        /// <see cref="Microsoft.Samples.NLayerApp.Application.MainModule.SalesManagement.ISalesManagementService"/>
        /// </summary>
        /// <param name="pageIndex"><see cref="Microsoft.Samples.NLayerApp.Application.MainModule.SalesManagement.ISalesManagementService"/></param>
        /// <param name="pageCount"><see cref="Microsoft.Samples.NLayerApp.Application.MainModule.SalesManagement.ISalesManagementService"/></param>
        /// <returns><see cref="Microsoft.Samples.NLayerApp.Application.MainModule.SalesManagement.ISalesManagementService"/></returns>
        public List <Order> FindPagedOrders(int pageIndex, int pageCount)
        {
            if (pageIndex < 0)
            {
                throw new ArgumentException(Resources.Messages.exception_InvalidPageIndex, "pageIndex");
            }

            if (pageCount <= 0)
            {
                throw new ArgumentException(Resources.Messages.exception_InvalidPageCount, "pageCount");
            }

            //implement cache-aside pattern

            List <Order>    orderResult     = null;
            CacheKey        key             = new CacheKey("FindPagedOrders", new { PageIndex = pageIndex, PageCount = pageCount });
            CacheItemConfig cacheItemConfig = new CacheItemConfig(key, new TimeSpan(0, 0, 10));

            if (_cacheManager.TryGet <List <Order> >(cacheItemConfig, out orderResult))
            {
                return(orderResult);
            }
            else
            {
                //query repository
                orderResult = _orderRepository.GetPagedElements(pageIndex, pageCount, o => o.OrderId, true)
                              .ToList();

                _cacheManager.Add(cacheItemConfig, orderResult);

                return(orderResult);
            }
        }
示例#3
0
        public void CacheItem_NullCacheKeyThrowNewArgumentException_Test()
        {
            //Arrange
            CacheKey key = null;

            //Act
            CacheItemConfig item = new CacheItemConfig(key, new TimeSpan());
        }
示例#4
0
        public void CacheItem_DefaultConstructorSet10SecondsToExpirationTime_Test()
        {
            //Arrrange
            CacheKey key = new CacheKey("fakeName");

            //Act
            CacheItemConfig item = new CacheItemConfig(key);

            //Assert
            Assert.IsTrue(item.ExpirationTime.TotalSeconds == 10);
        }
示例#5
0
        public void CacheItem_Constructor_Test()
        {
            //Arrange
            CacheKey key = new CacheKey("fakeName");

            //Act
            CacheItemConfig item = new CacheItemConfig(key, new TimeSpan(0, 0, 10));

            //assert

            Assert.AreEqual(key, item.CacheKey);
            Assert.IsTrue(item.ExpirationTime.TotalSeconds == 10);
        }
示例#6
0
        /// <summary>
        /// <see cref="M:Microsoft.Samples.NLayerApp.Infrastructure.CrossCutting.Caching.TryGet{TResult}"/>
        /// </summary>
        /// <param name="cacheItemConfig"><see cref="M:Microsoft.Samples.NLayerApp.Infrastructure.CrossCutting.Caching.TryGet{TResult}"/></param>
        /// <param name="value"><see cref="M:Microsoft.Samples.NLayerApp.Infrastructure.CrossCutting.Caching.TryGet{TResult}"/></param>
        public void Add(CacheItemConfig cacheItemConfig, object value)
        {
            if (value != null
                &&
                cacheItemConfig != null)
            {
                //get default cache
                DataCache defaultCache = _cacheFactory.GetDefaultCache();

                string   cachekey       = cacheItemConfig.CacheKey.GetCacheKey();
                TimeSpan expirationTime = cacheItemConfig.ExpirationTime;

                defaultCache.Put(cachekey, value, expirationTime);
            }
        }
        public void CacheManager_TryGet_NotInCacheReturnFalse_Test()
        {
            //Arrange
            CacheManager cacheManager = new CacheManager();

            CacheKey        key  = new CacheKey(Guid.NewGuid().ToString());
            CacheItemConfig item = new CacheItemConfig(key);

            //Act
            object cacheditem = null;
            bool   result     = cacheManager.TryGet <object>(item, out cacheditem);

            //Assert
            Assert.IsFalse(result);
        }
示例#8
0
        /// <summary>
        /// <see cref="ICacheManager"/>
        /// </summary>
        /// <param name="cacheItemConfig"><see cref="ICacheManager"/></param>
        /// <param name="value"><see cref="ICacheManager"/></param>
        public void Add(CacheItemConfig cacheItemConfig, object value)
        {
            if (value != null && cacheItemConfig != null)
            {
                //get default cache
                _defaultCache = MemoryCache.Default;

                var cachekey        = cacheItemConfig.CacheKey.GetCacheKey();
                var expirationTime  = cacheItemConfig.ExpirationTime;
                var cacheItemPolicy = new CacheItemPolicy
                {
                    AbsoluteExpiration = DateTimeOffset.Now.AddTicks(expirationTime.Ticks)
                };

                _defaultCache.Add(cachekey, value, cacheItemPolicy);
            }
        }
        public void CacheManager_Add_Test()
        {
            //Arrange
            CacheManager cacheManager = new CacheManager();

            CacheKey        key       = new CacheKey("fakeKey");
            CacheItemConfig cacheItem = new CacheItemConfig(key);

            //act

            cacheManager.Add(cacheItem, new object());

            //assert
            object result;

            Assert.IsTrue(cacheManager.TryGet(cacheItem, out result));
            Assert.IsNotNull(result);
        }
        public void CacheManager_TryGet_InCacheReturnTrue_Test()
        {
            //Arrange
            CacheManager cacheManager = new CacheManager();

            CacheKey        key       = new CacheKey(Guid.NewGuid().ToString());
            CacheItemConfig cacheitem = new CacheItemConfig(key);

            //Act
            object item = new object();
            object expected;

            cacheManager.Add(cacheitem, item);
            bool result = cacheManager.TryGet <object>(cacheitem, out expected);

            //Assert
            Assert.IsTrue(result);
        }
示例#11
0
        /// <summary>
        /// <see cref="ICacheManager"/>
        /// </summary>
        /// <typeparam name="TResult"><see cref="ICacheManager"/></typeparam>
        /// <param name="cacheItemConfig"><see cref="ICacheManager"/></param>
        /// <param name="result"><see cref="ICacheManager"/></param>
        /// <returns><see cref="ICacheManager"/></returns>
        public bool TryGet <TResult>(CacheItemConfig cacheItemConfig, out TResult result)
        {
            if (cacheItemConfig != null)
            {
                //get default cache
                _defaultCache = MemoryCache.Default;
                string cacheKey = cacheItemConfig.CacheKey.GetCacheKey();


                object cacheItem = _defaultCache[cacheKey];

                //Check if Cache is Enabled
                if (!_cacheEnabled)
                {
                    cacheItem = null;
                }

                if (cacheItem != null && _cacheEnabled)
                {
                    try
                    {
                        result = (TResult)cacheItem;
                        return(true);
                    }
                    catch (Exception)
                    {
                        result = default(TResult);
                        return(false);
                    }
                }
                else
                {
                    result = default(TResult);
                    return(false);
                }
            }
            else
            {
                throw new ArgumentNullException("cacheItemConfig");
            }
        }
        /// <summary>
        /// <see cref="Microsoft.Samples.NLayerApp.Application.MainModule.CustomersManagement.ICustomerManagementService"/>
        /// </summary>
        /// <param name="pageIndex"><see cref="Microsoft.Samples.NLayerApp.Application.MainModule.CustomersManagement.ICustomerManagementService"/></param>
        /// <param name="pageCount"><see cref="Microsoft.Samples.NLayerApp.Application.MainModule.CustomersManagement.ICustomerManagementService"/></param>
        /// <returns><see cref="Microsoft.Samples.NLayerApp.Application.MainModule.CustomersManagement.ICustomerManagementService"/></returns>
        public List <Country> FindPagedCountries(int pageIndex, int pageCount)
        {
            //implement cache aside pattern
            List <Country>  countryResults  = null;
            CacheKey        key             = new CacheKey("FindPagedCountries", new { PageIndex = pageIndex, PageCount = pageCount });
            CacheItemConfig cacheItemConfig = new CacheItemConfig(key, new TimeSpan(0, 10, 0));


            if (_cacheManager.TryGet <List <Country> >(cacheItemConfig, out countryResults))
            {
                return(countryResults);
            }
            else
            {
                countryResults = _countryRepository.GetPagedElements(pageIndex, pageCount, c => c.CountryName, true)
                                 .ToList();

                _cacheManager.Add(cacheItemConfig, countryResults);

                return(countryResults);
            }
        }
        /// <summary>
        /// <see cref="Microsoft.Samples.NLayerApp.Application.MainModule.CustomersManagement.ICustomerManagementService"/>
        /// </summary>
        /// <param name="pageIndex"><see cref="Microsoft.Samples.NLayerApp.Application.MainModule.CustomersManagement.ICustomerManagementService"/></param>
        /// <param name="pageCount"><see cref="Microsoft.Samples.NLayerApp.Application.MainModule.CustomersManagement.ICustomerManagementService"/></param>
        /// <returns><see cref="Microsoft.Samples.NLayerApp.Application.MainModule.CustomersManagement.ICustomerManagementService"/></returns>
        public List <Customer> FindPagedCustomers(int pageIndex, int pageCount)
        {
            if (pageIndex < 0)
            {
                throw new ArgumentException(Resources.Messages.exception_InvalidPageIndex, "pageIndex");
            }

            if (pageCount <= 0)
            {
                throw new ArgumentException(Resources.Messages.exception_InvalidPageCount, "pageCount");
            }

            //implement cache-aside pattern

            List <Customer> customerResults = null;
            CacheKey        key             = new CacheKey("FindPagedCustomers", new { PageIndex = pageIndex, PageCount = pageCount });
            CacheItemConfig cacheItemConfig = new CacheItemConfig(key, new TimeSpan(0, 0, 30));

            if (_cacheManager.TryGet <List <Customer> >(cacheItemConfig, out customerResults))
            {
                return(customerResults);
            }
            else
            {
                //Create "enabled variable" transform adhoc execution plan in prepared plan
                //for more info: http://geeks.ms/blogs/unai/2010/07/91/ef-4-0-performance-tips-1.aspx
                bool enabled = true;
                Specification <Customer> onlyEnabledSpec = new DirectSpecification <Customer>(c => c.IsEnabled == enabled);

                customerResults = _customerRepository.GetPagedElements(pageIndex, pageCount, c => c.CustomerCode, onlyEnabledSpec, true)
                                  .ToList();

                _cacheManager.Add(cacheItemConfig, customerResults);

                return(customerResults);
            }
        }
        /// <summary>
        /// <see cref="Microsoft.Samples.NLayerApp.Application.MainModule.CustomersManagement.ICustomerManagementService"/>
        /// </summary>
        /// <param name="countryName"><see cref="Microsoft.Samples.NLayerApp.Application.MainModule.CustomersManagement.ICustomerManagementService"/></param>
        /// <returns><see cref="Microsoft.Samples.NLayerApp.Application.MainModule.CustomersManagement.ICustomerManagementService"/></returns>
        public List <Country> FindCountriesByName(string countryName)
        {
            //implement cache-aside pattern

            List <Country>  countryResults  = null;
            CacheKey        key             = new CacheKey("FindCountriesByName", new { CountryName = countryName });
            CacheItemConfig cacheItemConfig = new CacheItemConfig(key, new TimeSpan(0, 10, 0));

            if (_cacheManager.TryGet <List <Country> >(cacheItemConfig, out countryResults))
            {
                return(countryResults);
            }
            else
            {
                CountryNameSpecification spec = new CountryNameSpecification(countryName);

                countryResults = _countryRepository.GetBySpec(spec as ISpecification <Country>)
                                 .ToList();

                _cacheManager.Add(cacheItemConfig, countryResults);

                return(countryResults);
            }
        }