public void RegisterNetwork(Lpp.Dns.DataMart.Lib.NetWorkSetting network) { Parallel.ForEach(network.DataMartList, dm => { var cacheDuration = new CacheDuration(network.NetworkId, network.NetworkName, dm.DataMartId, dm.DataMartName, dm.EnableResponseCaching, dm.DaysToRetainCacheItems); CheckCache(BaseCachePath, cacheDuration); DataMartCacheDurations.Add(cacheDuration); }); }
static void CheckCache(string baseCachePath, CacheDuration cache) { if (cache.Enabled == false || cache.Duration == TimeSpan.Zero || cache.Duration == TimeSpan.MaxValue) { //do not remove files if caching is not enabled, or duration is for infinity //should this clear any existing files? return; } string cachePath = Path.Combine(baseCachePath, cache.NetworkID.ToString(), cache.DataMartID.ToString("D")); if (!Directory.Exists(cachePath)) { //no cache files to check return; } log.Debug($"Checking cache for expired entries for Network:{ cache.Network }, DataMart:{ cache.DataMart }, path:{ cachePath }"); string[] requestDirectories = Directory.GetDirectories(cachePath); if (requestDirectories.Length == 0) { log.Debug($"No cache entries for Network:{ cache.Network }, DataMart:{ cache.DataMart }, path:{ cachePath }"); return; } Parallel.ForEach(requestDirectories, (directory) => { string[] filePaths = Directory.GetFiles(directory, "*.meta"); foreach (string path in filePaths) { TimeSpan age = DateTime.UtcNow - File.GetLastWriteTimeUtc(path); if (age > cache.Duration) { string dataPath = Path.ChangeExtension(path, "data"); try { log.Debug("Deleting expired cache document meta:" + path); File.Delete(path); log.Debug("Deleting expired cache document data:" + dataPath); File.Delete(dataPath); }catch (IOException ex) { //log and continue log.Error($"Error deleting expired cache document:{ path }, data:{ dataPath }", ex); } } } }); }