static void Main(string[] args) { var config = CacheManager.Core.ConfigurationBuilder.BuildConfiguration( s => { s.WithMaxRetries(50); s.WithRetryTimeout(100); s.WithUpdateMode(CacheUpdateMode.Up); s.WithMicrosoftLogging( f => { f.AddDebug(LogLevel.Trace); f.AddConsole(LogLevel.Trace); }); s.WithProtoBufSerializer(); s.WithJsonSerializer(); //s.WithRedisBackplane("redis"); //s.WithRedisConfiguration("redis", // cfg => // cfg.WithEndpoint("127.0.0.1", 6379) // .WithDatabase(0) // .WithAllowAdmin()); s.WithDictionaryHandle("dic1"); s.WithDictionaryHandle("dic2"); s.WithMicrosoftMemoryCacheHandle("ms1"); s.WithSystemRuntimeCacheHandle("runtime1") .EnablePerformanceCounters() .WithExpiration(ExpirationMode.Sliding, TimeSpan.FromSeconds(10)); //s.WithRedisCacheHandle("redis", true) // .EnablePerformanceCounters() // .WithExpiration(ExpirationMode.Absolute, TimeSpan.FromMinutes(2)); }); Tests.TestEachMethod(CacheFactory.FromConfiguration<string>(config)); Tests.TestPoco(CacheFactory.FromConfiguration<Poco>(config)); // json test var logConfig = new Microsoft.Extensions.Configuration.ConfigurationBuilder() .AddJsonFile("cache.json").Build(); var cacheConfig = logConfig.GetCacheConfiguration() .Builder .WithMicrosoftLogging(f => { f.AddDebug(LogLevel.Trace); f.AddConsole(LogLevel.Trace); }) .Build(); var fromJsonCache = new BaseCacheManager<string>(cacheConfig); Tests.TestEachMethod(fromJsonCache); }
public virtual List <string> Reload() { List <string> names = new List <string>(); Dictionary <string, ICacheManagerConfiguration> configs = new Dictionary <string, ICacheManagerConfiguration>(); string jsonFilePath = CacheProvider.CACHE_CONFIG_FILE; if (File.Exists(jsonFilePath)) { string jsonText = File.ReadAllText(jsonFilePath); var memoryFileProvider = new InMemoryFileProvider(jsonText); var config = new Microsoft.Extensions.Configuration.ConfigurationBuilder() .AddJsonFile(memoryFileProvider, "fake.json", false, false) .Build(); var managerSections = config.GetSection(CacheProvider.CACHE_SECTION_NAME).GetChildren(); foreach (var managerSection in managerSections) { string itemName = managerSection["name"]; var cacheConfiguration = config.GetCacheConfiguration(itemName); if (configs.ContainsKey(itemName)) { configs.Remove(itemName); } configs.Add(itemName, cacheConfiguration); if (names.Contains(itemName)) { names.Remove(itemName); } names.Add(itemName); } m_configs = configs; } return(names); }
public void Refresh() { Dictionary <string, ICacheManager <object> > mgrs = new Dictionary <string, ICacheManager <object> >(); /* * CacheManagerSection section = ConfigurationManager.GetSection(CACHE_SECTION_NAME) as CacheManagerSection; * * if (section != null && section.CacheManagers != null) * { * foreach (var item in section.CacheManagers) * { * if (mgrs.ContainsKey(item.Name)) mgrs.Remove(item.Name); * var cache = CacheFactory.FromConfiguration<object>(item.Name); * if (cache != null) mgrs.Add(item.Name, cache); * } * } */ if (DataConfigHelper.CacheConfigLoader != null) { var cacheNames = DataConfigHelper.CacheConfigLoader.Reload(); foreach (var cacheName in cacheNames) { string itemName = cacheName; var cacheConfiguration = DataConfigHelper.CacheConfigLoader.GetCacheConfig(itemName); if (mgrs.ContainsKey(itemName)) { mgrs.Remove(itemName); } var cache = CacheFactory.FromConfiguration <object>(cacheConfiguration); if (cache != null) { mgrs.Add(itemName, cache); } } } else { var config = new Microsoft.Extensions.Configuration.ConfigurationBuilder() //.SetBasePath(Directory.GetCurrentDirectory()) // Directory where the json files are located .AddJsonFile(CACHE_CONFIG_FILE, optional: false, reloadOnChange: true) .Build(); var managerSections = config.GetSection(CACHE_SECTION_NAME).GetChildren(); foreach (var managerSection in managerSections) { string itemName = managerSection["name"]; var cacheConfiguration = config.GetCacheConfiguration(itemName); if (mgrs.ContainsKey(itemName)) { mgrs.Remove(itemName); } var cache = CacheFactory.FromConfiguration <object>(cacheConfiguration); if (cache != null) { mgrs.Add(itemName, cache); } } } m_Mgrs = mgrs; // thread-safe (reads and writes of reference types are atomic) }