public void KeyNotFoundInt()
        {
            var cache = new CacheEngine(new MemoryCacheProvider());

            Assert.Equal(10, cache.Get(() => KeyNotFoundIntMethod()));
            Assert.Equal(10, MemoryCache.Default[cache.BuildKey(() => KeyNotFoundIntMethod())]);
            Assert.Equal(10, cache.Get(() => KeyNotFoundIntMethod()));
        }
        public void AddCacheWithMultipleParameter()
        {
            var cache = new CacheEngine(new MemoryCacheProvider());

            Assert.Equal("CARROT & POTATO", cache.Get(() => MultipleParameterMethod("carrot", "potato")));
            Assert.Equal("CARROT & POTATO", MemoryCache.Default[cache.BuildKey(() => MultipleParameterMethod("carrot", "potato"))]);
            Assert.Equal("CARROT & POTATO", cache.Get(() => MultipleParameterMethod("carrot", "potato")));
        }
        public void AddCacheWithEnumParameter()
        {
            var cache = new CacheEngine(new MemoryCacheProvider());

            Assert.Equal("apa", cache.Get(() => OneEnumParameterMethod(EnumTest.Apa)));
            Assert.Equal("apa", MemoryCache.Default[cache.BuildKey(() => OneEnumParameterMethod(EnumTest.Apa))]);
            Assert.Equal("apa", cache.Get(() => OneEnumParameterMethod(EnumTest.Apa)));
        }
        public void AddCacheWithNoParameter()
        {
            var cache = new CacheEngine(new MemoryCacheProvider());

            Assert.Equal("This is a test!", cache.Get(() => NoParameterMethod()));
            Assert.Equal("This is a test!", MemoryCache.Default[cache.BuildKey(() => NoParameterMethod())]);
            Assert.Equal("This is a test!", cache.Get(() => NoParameterMethod()));
        }
        public void AddCacheWithOneParameter()
        {
            var cache = new CacheEngine(new MemoryCacheProvider());

            Assert.Equal("Banana", cache.Get(() => OneParameterMethod("banana")));
            Assert.Equal("Banana", MemoryCache.Default[cache.BuildKey(() => OneParameterMethod("banana"))]);
            Assert.Equal("Banana", cache.Get(() => OneParameterMethod("banana")));
        }
Esempio n. 6
0
        public void TestRedis()
        {
            Assert.False(_multiplexer.GetDatabase().KeyExists(_cacheEngine.BuildKey(() => TestMethod())));
            var result = _cacheEngine.Get(() => TestMethod());
            var result2 = _multiplexer.GetDatabase().Get<Morot>(_cacheEngine.BuildKey(() => TestMethod()));

            Assert.Equal(result.Name, result2.Name);
            Assert.Equal(result.Size, result2.Size);
        }
        public void AddCacheWithExpiry()
        {
            var cache = new CacheEngine(new MemoryCacheProvider());

            Assert.Equal("This is a test!", cache.Get(() => NoParameterMethod(), TimeSpan.FromMilliseconds(200)));
            Thread.Sleep(100);
            Assert.NotEqual(null, MemoryCache.Default[cache.BuildKey(() => NoParameterMethod())]);
            Assert.Equal("This is a test!", cache.Get(() => NoParameterMethod(), TimeSpan.FromMilliseconds(200)));
            Thread.Sleep(100);
            Assert.Equal(null, MemoryCache.Default[cache.BuildKey(() => NoParameterMethod())]);
            Assert.Equal("This is a test!", cache.Get(() => NoParameterMethod(), TimeSpan.FromMilliseconds(200)));
            Assert.NotEqual(null, MemoryCache.Default[cache.BuildKey(() => NoParameterMethod())]);
        }
        public void AddCacheWithNoParameterAndSetExpires()
        {
            var cache = new CacheEngine(new MemoryCacheProvider());

            cache.Get(() => NoParameterMethod(), TimeSpan.FromMilliseconds(200));
            Assert.NotNull(MemoryCache.Default[cache.BuildKey(() => NoParameterMethod())]);
            Thread.Sleep(250);
            Assert.Null(MemoryCache.Default[cache.BuildKey(() => NoParameterMethod())]);
        }
        public ActionResult Index()
        {
            const string key   = "books";
            var          books = CacheEngine.Get <List <Book> >(key);

            if (books == null)
            {
                books = BookData.GetBooks();
                CacheEngine.Add(key, books);
            }

            ViewBag.books = books;
            return(View());
        }
        /// <summary>
        /// Get element(s) from cache
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="cacheKey"></param>
        /// <param name="action"></param>
        /// <returns></returns>
        protected T GetFromCache <T>(string cacheKey, Action action)
        {
            T output = default(T);

            if (CacheEngine.Exists(cacheKey))
            {
                output = CacheEngine.Get <T>(cacheKey);
            }
            else
            {
                action();
            }

            return(output);
        }
Esempio n. 11
0
        static void Main(string[] args)
        {
            const string key = "books";

            var books = CacheEngine.Get <List <Book> >(key);

            if (books == null)
            {
                books = BookData.GetBooks();
                CacheEngine.Add(key, books);
            }

            var cachedBooks = CacheEngine.Get <List <Book> >(key);

            foreach (var book in books)
            {
                System.Console.WriteLine(book.Name);
            }

            System.Console.ReadKey();
        }