Exemple #1
0
        /// <summary>
        /// This method returns a full entry for a given id while using as much from the cache as possible
        /// </summary>
        /// <param name="id">The id</param>
        /// <param name="doNotUseCache">if you want to load everything from Proxer, ignoring the cache. WARNING: Do not use this regularly</param>
        /// <returns>A full entry from Proxer</returns>
        public async Task <FullEntry> GetFullEntry(int id, bool doNotUseCache = false)
        {
            var       fullEntryEndPoint = "https://Proxer.me/api/v1/info/fullentry";
            var       entryEndPoint     = "https://Proxer.me/api/v1/info/entry";
            FullEntry result;

            var postParameters = new Dictionary <string, string>();

            if (id > 0)
            {
                postParameters.Add("id", id.ToString());
            }

            if (doNotUseCache) // insert Spongebob Meme
            {
                result = await GetData <FullEntry>(postParameters, fullEntryEndPoint);
            }
            else
            {
                // Database stuff for enablin the cache

                if (DatabaseConnection != null)
                {
                    // DatabaseCaching is enabled so we use it!
                    var dbResult = DatabaseConnection.Get(id, typeof(EntryDetail)).Cast <EntryDetail>();
                    if (dbResult.Any())
                    {
                        result      = new FullEntry();
                        result.data = dbResult.First();
                        result.code = 1;
                    }
                    else
                    {
                        result = await GetData <FullEntry>(postParameters, entryEndPoint);

                        DatabaseConnection.Put(result.data);
                    }
                }
                else
                {
                    // Database caching is not available, so use memory caching
                    result = await GetData <FullEntry>(postParameters, entryEndPoint);

                    if (CachingController.CanPopulateEntryFromCache(result.data, result.data.id))
                    {
                        CachingController.PopulateEntryFromCache(result.data, result.data.id);
                    }
                    else
                    {
                        result = await GetData <FullEntry>(postParameters, fullEntryEndPoint);

                        CachingController.CacheAllCacheableProperties(result.data, result.data.id);
                    }
                }
            }

            return(result);
        }
Exemple #2
0
        public void GivenResultAlreadyRetrieved_ShouldNotCallRepositoryAgain()
        {
            //assert
            var cache      = new MemoryCache(new MemoryCacheOptions());
            var repository = new Mock <IRepository>();

            repository.Setup(x => x.GetAllProducts()).Returns(new List <Code.DomainModels.Product>()
            {
                new Code.DomainModels.Product()
                {
                    ProductId = 1,
                    Name      = "iPhone"
                }
            });

            var sut = new CachingController(cache, repository.Object);

            //act
            var result     = sut.SomeExample();
            var nextResult = sut.SomeExample();

            //assert
            repository.Verify(x => x.GetAllProducts(), Times.Once);
        }
 public CachingControllerTests()
 {
     _cache      = new Mock <IMemoryCache>();
     _controller = new CachingController(_cache.Object);
 }