コード例 #1
0
        public static ICacheProvider GetCacheProvider()
        {
            var provider = CacheConfig.Create()
                           .BuildCacheProviderWithTraceLogging();

            return(provider);
        }
コード例 #2
0
        public void ShouldLogInfoMessages()
        {
            var provider = CacheConfig.Create()
                           .UseMemoryCache()
                           .BuildCacheProvider(new TestLogger());

            provider.Add("test1", DateTime.Now.AddMinutes(1), "data");

            Assert.IsTrue(TestLogger.InfoCount > 1, "Expected some informational messages to be logged but they were not");
        }
コード例 #3
0
        public static ICache BuildTestCache()
        {
            if (_cache != null)
            {
                return(_cache);
            }

            _cache = CacheConfig.Create()
                     .BuildCacheProviderWithTraceLogging()
                     .InnerCache;

            return(_cache);
        }
コード例 #4
0
        public void ShouldLogInfoAndErrorMEssages()
        {
            try {
                var provider = CacheConfig.Create()
                               .UseMemcachedCache()
                               .UsingDistributedServerNode("1.2.3.4", 1)
                               .BuildCacheProvider(new TestLogger());
                provider.Add("test1", DateTime.Now.AddMinutes(1), "data");
            } catch {  }

            Assert.IsTrue(TestLogger.InfoCount > 1, "Expected some informational messages to be logged but they were not");
            Assert.IsTrue(TestLogger.ErrorCount > 1, "Expected some error messages to be logged but they were not");
        }
コード例 #5
0
        public CacheProvider(ICache cache, ILogging logger, CacheConfig config, ICacheDependencyManager cacheDependencyManager, ICacheFeatureSupport featureSupport)
        {
            _cache                  = cache;
            _logger                 = logger;
            _featureSupport         = featureSupport;
            _config                 = config ?? CacheConfig.Create();
            _cacheDependencyManager = cacheDependencyManager;

            if (_config.IsCacheDependencyManagementEnabled && _cacheDependencyManager != null)
            {
                _logger.WriteInfoMessage(string.Format("CacheKey dependency management enabled, using {0}.", _cacheDependencyManager.Name));
            }
            else
            {
                _cacheDependencyManager = null;  // Dependency Management is disabled
                _logger.WriteInfoMessage("CacheKey dependency management not enabled.");
            }
        }
コード例 #6
0
        public static bool ExampleAddUsingAsyncCalls()
        {
            ConsoleHelper.WriteHeadingSeparator("Simple Cache examples using Async calls");
            var cacheProvider = CacheConfig.Create().BuildCacheProvider(new InMemoryLogger());

            cacheProvider.ClearAll();
            const string cacheData = "AsyncData";
            const string cacheKey  = "async-key";

            Console.WriteLine("Call GetAsync with long running task to populate cache");
            var expensiveData = cacheProvider.GetAsync <string>(cacheKey, DateTime.Now.AddDays(1), () =>
            {
                return(System.Threading.Tasks.Task.Run(() =>
                {
                    ConsoleHelper.Wait(2);  // simulate some long running operation
                    return cacheData;
                }));
            });

            Console.WriteLine("...Doing some small work and then checking if item is in cache yet (which it should not be)");
            // Do some work
            ConsoleHelper.Wait(1);
            var doesCacheItemExistInCacheYet = (cacheProvider.InnerCache.Get <string>(cacheKey) != null);

            if (doesCacheItemExistInCacheYet == true)
            {
                ConsoleHelper.WriteErrMsgToConsole("Cache item was already in cache when it was not expected to be in there yet via the async method");
                return(false);
            }

            Console.WriteLine("...Doing a little more work which should take longer than the async task that places data in cache so we expect it to exist in cache.");
            ConsoleHelper.Wait(1.1);

            var doesCacheItemExistInCacheNow = (cacheProvider.InnerCache.Get <string>(cacheKey) != null);

            if (doesCacheItemExistInCacheNow == false)
            {
                ConsoleHelper.WriteErrMsgToConsole("Cache item was NOT in cache when it was expected to be in there via the async method");
                return(false);
            }

            Console.WriteLine(">> Item was placed in cache via async method as expected\n\n");
            return(true);
        }
コード例 #7
0
        public static bool ExampleAddAndClearWithDependencies()
        {
            ConsoleHelper.WriteHeadingSeparator("Simple Cache Dependency examples");

            // Use cache from config file.
            var cacheProvider = CacheConfig.Create()
                                .BuildCacheProvider(new InMemoryLogger());

            cacheProvider.ClearAll();
            const string masterDataKey = "MasterData";


            Console.WriteLine("1. Adding the main data to cache which acts as the trigger for the other added items");
            var masterData = cacheProvider.Get <string>(masterDataKey, DateTime.Now.AddDays(1), () => "Master Data Item");

            Console.WriteLine("\n2. Attempting to get some related Data from cache provider which will\nimplicitly add it to the cache and related it to the 'MasterItem'\nadded previously.");
            var bitOfRelatedData1 = cacheProvider.Get <string>("Bit1", DateTime.Now.AddDays(1), () => "Some Bit Of Data1", masterDataKey);
            var bitOfRelatedData2 = cacheProvider.Get <string>("Bit2", DateTime.Now.AddDays(1), () => "Some Bit Of Data2", masterDataKey);

            Console.WriteLine("\n3. Make sure everything is in the cache as expected.");
            var bit1Ok       = cacheProvider.InnerCache.Get <string>("Bit1") != null;
            var bit2Ok       = cacheProvider.InnerCache.Get <string>("Bit2") != null;
            var masterDataOk = cacheProvider.InnerCache.Get <string>(masterDataKey) != null;

            Console.WriteLine("Bit1 is in the cache?:{0}", bit1Ok);
            Console.WriteLine("Bit2 is in the cache?:{0}", bit2Ok);
            Console.WriteLine("MasterData item is in the cache?:{0}", masterDataOk);


            if (!bit1Ok || !bit2Ok || !masterDataOk)
            {
                ConsoleHelper.WriteErrMsgToConsole("Items were not present in cache.");
                return(false);
            }

            Console.WriteLine("\n5. Now clearing the master data item which should also clear the related items");
            cacheProvider.InvalidateCacheItem(masterDataKey);
            ConsoleHelper.Wait(2);

            Console.WriteLine("\n6. Make sure everything is NOT in the cache as expected.");
            var bit1Value = cacheProvider.InnerCache.Get <string>("Bit1");
            var bit2Value = cacheProvider.InnerCache.Get <string>("Bit2");

            bit1Ok = bit1Value == null;
            bit2Ok = bit2Value == null;
            var masterDataValue = cacheProvider.InnerCache.Get <string>(masterDataKey);

            masterDataOk = masterDataValue == null;

            Console.WriteLine("Bit1 is NOT in the cache?:{0}", bit1Ok);
            Console.WriteLine("Bit2 is NOT in the cache?:{0}", bit2Ok);
            Console.WriteLine("MasterData item is NOT in the cache?:{0}", masterDataOk);

            if (!bit1Ok || !bit2Ok || !masterDataOk)
            {
                ConsoleHelper.WriteErrMsgToConsole("Items were present in cache when they should have been removed.");
                return(false);
            }

            Console.WriteLine("All dependencies worked as expected.");

            cacheProvider.ClearAll();
            return(true);
        }
コード例 #8
0
        public static bool ExampleAddAndRetrieveFromCache()
        {
            int  _accessCounter  = 0;
            bool _allTestsPassed = true;

            ConsoleHelper.WriteHeadingSeparator("Simple Add and Retrieve Examples");

            var cacheProvider = CacheConfig
                                .Create()
                                .BuildCacheProvider(new InMemoryLogger()); // Use the settings from config

            // Can do via dependency helper too
            //var cacheProvider = AppServices.Cache;

            // First try and get some data. It wont be in the cache, so the anonymous function is executed,
            // the item is automatically added to the cache and returned.
            Console.WriteLine("#1: Getting Some Data.");
            var data1 = cacheProvider.Get <SomeData>("cache-key", DateTime.Now.AddSeconds(5), () =>
            {
                // This is the anonymous function which gets called if the data is not in the cache.
                // This method is executed and whatever is returned, is added to the cache with the
                // passed in expiry time.
                _accessCounter++;
                Console.WriteLine("... => Adding data to the cache... 1st call");
                var someData = new SomeData()
                {
                    SomeText = "cache example1", SomeNumber = 1
                };
                return(someData);
            });

            Console.WriteLine("... => SomeData values: SomeText=[{0}], SomeNumber={1}\n", data1.SomeText, data1.SomeNumber);
            if (_accessCounter != 1)
            {
                ConsoleHelper.WriteErrMsgToConsole("Cache not added to, test result failed!");
            }

            // Now try and get some data using the same cache-key and before the cached data expiry time elapses.
            // The data will be located in the cache and returned, and the anonymous function is NOT executed.

            Console.WriteLine("#2: Getting Some More Data which should now be cached.");
            var data2 = cacheProvider.Get <SomeData>("cache-key", DateTime.Now.AddSeconds(3), () =>
            {
                // This is the anonymous function which gets called if the data is not in the cache.
                // This method is executed and whatever is returned, is added to the cache with the
                // passed in expiry time.
                _accessCounter++;
                Console.WriteLine("... => Adding data to the cache...2nd call- should not be displayed!");
                var someData = new SomeData()
                {
                    SomeText = "cache example2", SomeNumber = 2
                };
                return(someData);
            });

            Console.WriteLine("... => SomeData values: SomeText=[{0}], SomeNumber={1}\n", data2.SomeText, data2.SomeNumber);
            if (_accessCounter == 2)
            {
                ConsoleHelper.WriteErrMsgToConsole("Data item not found in cachewhen it should have been found in cache, test result failed!");
            }

            // Here we wait a period of time so that the cached data expiry time has passed and the data is
            // removed from the cache. We make the same call to retrieve the data using the same cache key, the data
            // is not found in the cache, so once again the anonymous function is executed, whatever is returned is
            // added to the cache, and then returned to the caller.

            ConsoleHelper.Wait(5);
            Console.WriteLine("#3: Getting Some More Data which should not be cached.");
            var data3 = cacheProvider.Get <SomeData>("cache-key", DateTime.Now.AddSeconds(5), () =>
            {
                // This is the anonymous function which gets called if the data is not in the cache.
                // This method is executed and whatever is returned, is added to the cache with the
                // passed in expiry time.
                _accessCounter++;
                Console.WriteLine("... => Adding data to the cache...3rd call");
                var someData = new SomeData()
                {
                    SomeText = "cache example3", SomeNumber = 3
                };
                return(someData);
            });

            Console.WriteLine("... => SomeData values: SomeText=[{0}], SomeNumber={1}\n", data3.SomeText, data3.SomeNumber);
            if (_accessCounter != 2)
            {
                ConsoleHelper.WriteErrMsgToConsole("Cache not added to, test result failed!");
            }


            Func <SomeData> getCacheData = new Func <SomeData>(() =>
            {
                // This is the anonymous function which gets called if the data is not in the cache.
                // This method is executed and whatever is returned, is added to the cache with the
                // passed in expiry time.
                _accessCounter++;
                Console.WriteLine("... => Adding data to the cache...4th call, with generated cache key - should only see this msg twice, NOT 3 times");
                var someData = new SomeData()
                {
                    SomeText = "cache example4 - generated cache key", SomeNumber = 4
                };
                return(someData);
            });

            // Here we use the really simple API call to get an item from the cache without a cache key specified.
            // The cache key is generated from the function we pass in as the delegate used to retrieve the data
            Console.WriteLine("#4: Getting Some Data which should NOT BE cached.");
            var data4 = cacheProvider.Get <SomeData>(DateTime.Now.AddSeconds(3), getCacheData);

            if (_accessCounter != 3)
            {
                ConsoleHelper.WriteErrMsgToConsole("Cache not added to, test result failed!");
                _allTestsPassed = false;
            }

            Console.WriteLine("#5: Getting Some More Data which should BE cached.");
            var data5 = cacheProvider.Get <SomeData>(DateTime.Now.AddSeconds(2), getCacheData);

            if (_accessCounter != 3)
            {
                ConsoleHelper.WriteErrMsgToConsole("Data item not found in cache when it should have been found in cache, test result failed!");
                _allTestsPassed = false;
            }

            ConsoleHelper.Wait(3);
            Console.WriteLine("#6: Getting Some More Data which should NOT be cached.");
            var data6 = cacheProvider.Get <SomeData>(DateTime.Now.AddSeconds(2), getCacheData);

            if (_accessCounter != 4)
            {
                ConsoleHelper.WriteErrMsgToConsole("Cache not added to, test result failed!");
                _allTestsPassed = false;
            }

            return(_allTestsPassed);
        }
コード例 #9
0
        public static ICacheDependencyManager GetDependencyManager()
        {
            var config = CacheConfig.Create();

            return(new GenericDependencyManager(config.BuildCacheProviderWithTraceLogging().InnerCache, new Core.Diagnostics.Logger(config)));
        }