private static void ConfigureHost(Uri proxyEndPoint, Uri replayEndPoint, EventWaitHandle terminationIndicator) { if (proxyEndPoint == null) { throw new ArgumentNullException(nameof(proxyEndPoint)); } if (replayEndPoint == null) { throw new ArgumentNullException(nameof(replayEndPoint)); } if (terminationIndicator == null) { throw new ArgumentNullException(nameof(terminationIndicator)); } new Thread(() => { // In this example, a Sqlite database is being used since it means that there is no dependency of this project on a SQL Server // installation somewhere. But it would be easy for the host to connect to a SQL Server database, SqlRunner.Instance would be // specified (referencing a class in the DataProviderService.Example project) rather than SqliteRunner.Instance for the // DiskCache and the first argument of the Host constructor would return a new SqlConnection, instead of a SQLiteConnection. var cache = new DiskCache(SqliteRunner.Instance, cacheFolder: new DirectoryInfo("Cache"), infoLogger: Console.WriteLine); using (var proxyHost = new Host(new SqlProxy(() => new SQLiteConnection(), cache.QueryRecorder, cache.ScalarQueryRecorder, cache.NonQueryRowCountRecorder), proxyEndPoint)) { using (var replayHost = new Host(new SqlReplayer(cache.DataRetriever, cache.ScalarDataRetriever, cache.NonQueryRowCountRetriever), replayEndPoint)) { terminationIndicator.WaitOne(); } } }).Start(); }
public void GetValidatedAbsolutePath_Absolute_Outside_WebRoot() { var absPath = DiskCache.GetValidatedCachePathsImpl(@"X:\Sites\OUTSIDE", this.TestMapPath, this.GetDirectoryInfo, out var virtualCachePath); Assert.AreEqual(@"X:\Sites\OUTSIDE", absPath); Assert.AreEqual(null, virtualCachePath); }
private static Configuration GetDefaultConfiguration(Configuration userDefinedConfig) { var httpClient = userDefinedConfig.HttpClient ?? new HttpClient(); if (userDefinedConfig.HttpReadTimeout > 0) { httpClient.Timeout = TimeSpan.FromSeconds(userDefinedConfig.HttpReadTimeout); } var logger = userDefinedConfig.Logger ?? new MiniLogger(); var scheduler = userDefinedConfig.Scheduler ?? new WorkScheduler(logger); var diskCache = userDefinedConfig.DiskCache ?? DiskCache.CreateCache(typeof(ImageService).Name); var downloadCache = userDefinedConfig.DownloadCache ?? new DownloadCache(httpClient, diskCache); return(new Configuration( userDefinedConfig.MaxCacheSize, httpClient, scheduler, logger, diskCache, downloadCache, userDefinedConfig.LoadWithTransparencyChannel, userDefinedConfig.FadeAnimationEnabled, userDefinedConfig.TransformPlaceholders, userDefinedConfig.DownsampleInterpolationMode, userDefinedConfig.HttpHeadersTimeout, userDefinedConfig.HttpReadTimeout )); }
/// <summary> /// Occurs when the ASP.NET event handler finishes execution. /// </summary> /// <param name="sender"> /// The source of the event. /// </param> /// <param name="e"> /// An <see cref="T:System.EventArgs">EventArgs</see> that contains the event data. /// </param> /// <returns> /// The <see cref="T:System.Threading.Tasks.Task"/>. /// </returns> private Task PostProcessImage(object sender, EventArgs e) { HttpContext context = ((HttpApplication)sender).Context; object cachedPathObject = context.Items[CachedPathKey]; if (cachedPathObject != null) { string cachedPath = cachedPathObject.ToString(); // Trim the cache. DiskCache.TrimCachedFolders(cachedPath); // Fire the post processing event. EventHandler <PostProcessingEventArgs> handler = OnPostProcessing; if (handler != null) { context.Items[CachedPathKey] = null; return(Task.Run(() => handler(this, new PostProcessingEventArgs { CachedImagePath = cachedPath }))); } } return(Task.FromResult <object>(null)); }
void RecursiveAddFilesContent(IDirectoryCache directory, RefDictionary <string, object> filesContent, HashSet <string> takenNames, string destDir) { DiskCache.UpdateIfNeeded(directory); foreach (var child in directory) { if (child.IsInvalid) { continue; } var outPathFileName = destDir + "/" + child.Name; takenNames.Add(outPathFileName); if (child is IDirectoryCache) { RecursiveAddFilesContent(child as IDirectoryCache, filesContent, takenNames, outPathFileName); continue; } if (child is IFileCache) { filesContent.GetOrAddValueRef(outPathFileName) = new Lazy <object>(() => { var res = ((IFileCache)child).ByteContent; ((IFileCache)child).FreeCache(); return(res); }); } } }
public void FailoverWithSwithcOnTest() { FailoverReactor.DISK_FILE_WRITER_DUETIME = 0; FailoverReactor.DISK_FILE_WRITER_PERIOD = 500; FailoverReactor.DIR_NOT_FOUND_DUETIME = 0; FailoverReactor.SWITCH_REFRESHER_PERIOD = 500; string path = AppDomain.CurrentDomain.BaseDirectory; string failoverPath = Path.Combine(path, FailoverReactor.FAILOVER_PATH); string failoverSwitchPath = Path.Combine(failoverPath, UtilAndComs.FAILOVER_SWITCH); DiskCache.WriteFile(failoverSwitchPath, "1" + DiskCache.GetLineSeparator()); var failover = new FailoverReactor(_hostReactor, path); Thread.Sleep(100); var info = failover.GetService(_serviceMap.First().Value.GetKey()); Assert.True(failover.IsFailoverSwitch()); Assert.NotNull(info); DiskCache.WriteFile(failoverSwitchPath, "0" + DiskCache.GetLineSeparator()); Thread.Sleep(550); Assert.False(failover.IsFailoverSwitch()); Directory.Delete(Path.Combine(path, FailoverReactor.FAILOVER_PATH), true); }
public void GetValidatedAbsolutePath_Absolute_In_WebRoot() { var absPath = DiskCache.GetValidatedCachePathsImpl(@"X:\Sites\MySite\App_Data\TEMP\IP", this.TestMapPath, this.GetDirectoryInfo, out var virtualCachePath); Assert.AreEqual(@"X:\Sites\MySite\App_Data\TEMP\IP", absPath); Assert.AreEqual("~/App_Data/TEMP/IP", virtualCachePath); }
public static void TryGetSetValue_WhenInvokedTogetherUpdatingValue_ReturnsTrueAndHasEqualInputAndOutput() { var testDirPath = Path.Combine(Environment.CurrentDirectory, "trygetsetvalue_test"); var testDir = new DirectoryInfo(testDirPath); if (!testDir.Exists) { testDir.Create(); } var cachePolicy = Mock.Of <ICachePolicy <string> >(); var input = new byte[] { 1, 2, 3, 4 }; var updatedInput = new byte[] { 3, 4, 5, 6 }; const ulong size = 20; using (var cache = new DiskCache <string>(testDir, cachePolicy, size)) { var setSuccess = cache.TrySetValue("test", new MemoryStream(input)); setSuccess = cache.TrySetValue("test", new MemoryStream(updatedInput)); _ = cache.TryGetValue("test", out var result); using (var reader = new BinaryReader(result)) { var resultBytes = reader.ReadBytes(4); var seqEqual = updatedInput.SequenceEqual(resultBytes); Assert.IsTrue(seqEqual); } } testDir.Delete(true); }
public static async Task GetSetValueAsync_WhenInvokedTogetherUpdatingValue_HasEqualInputAndOutput() { var testDirPath = Path.Combine(Environment.CurrentDirectory, "getsetvalueasync_test"); var testDir = new DirectoryInfo(testDirPath); if (!testDir.Exists) { testDir.Create(); } var cachePolicy = Mock.Of <ICachePolicy <string> >(); var input = new byte[] { 1, 2, 3, 4 }; var updatedInput = new byte[] { 3, 4, 5, 6 }; const ulong size = 20; using (var cache = new DiskCache <string>(testDir, cachePolicy, size)) { await cache.SetValueAsync("asd", new MemoryStream(input)).ConfigureAwait(false); await cache.SetValueAsync("asd", new MemoryStream(updatedInput)).ConfigureAwait(false); var result = await cache.GetValueAsync("asd").ConfigureAwait(false); using (var reader = new BinaryReader(result)) { var resultBytes = reader.ReadBytes(4); var seqEqual = updatedInput.SequenceEqual(resultBytes); Assert.IsTrue(seqEqual); } } testDir.Delete(true); }
public static void TryGetValue_WhenEmpty_ReturnsFalseValueOutNull() { var testDirPath = Path.Combine(Environment.CurrentDirectory, "trygetvalue_out_test"); var testDir = new DirectoryInfo(testDirPath); if (!testDir.Exists) { testDir.Create(); } var cachePolicy = Mock.Of <ICachePolicy <string> >(); const ulong size = 123; using (var cache = new DiskCache <string>(testDir, cachePolicy, size)) { var result = cache.TryGetValue("asd", out var stream); Assert.Multiple(() => { Assert.IsFalse(result); Assert.IsNull(stream); }); } testDir.Delete(true); }
public void InitializeOnce() { if (_wasFirstInitialize) { return; } _wasFirstInitialize = true; if (ProjectOptions.Localize) { ProjectOptions.InitializeTranslationDb(); } var bbTslint = DevDependencies?.FirstOrDefault(s => s.StartsWith("bb-tslint")); if (bbTslint != null) { var srcTsLint = PathUtils.Join(Owner.FullPath, $"node_modules/{bbTslint}/tslint.json"); var srcFile = DiskCache.TryGetItem(srcTsLint) as IFileCache; var dstTsLint = PathUtils.Join(Owner.FullPath, "tslint.json"); if (srcFile != null && (!(DiskCache.TryGetItem(dstTsLint) is IFileCache dstFile) || !dstFile.HashOfContent.SequenceEqual(srcFile.HashOfContent))) { File.WriteAllBytes(dstTsLint, srcFile.ByteContent); Console.WriteLine($"Updated tslint.json from {srcTsLint}"); } } }
public static async Task TryGetValueAsync_WhenValueExpired_ReturnsFalseAndNullStream() { var testDirPath = Path.Combine(Environment.CurrentDirectory, "trygetvalueexpired_test"); var testDir = new DirectoryInfo(testDirPath); if (!testDir.Exists) { testDir.Create(); } var cachePolicy = new FixedTimespanCachePolicy <string>(TimeSpan.FromMilliseconds(1)); var input = new byte[] { 1, 2, 3, 4 }; const ulong size = 20; using (var cache = new DiskCache <string>(testDir, cachePolicy, size, TimeSpan.FromMilliseconds(5))) { await cache.SetValueAsync("asd", new MemoryStream(input)).ConfigureAwait(false); await Task.Delay(100).ConfigureAwait(false); var(hasValue, stream) = await cache.TryGetValueAsync("asd").ConfigureAwait(false); Assert.IsFalse(hasValue); Assert.IsNull(stream); } testDir.Delete(true); }
public static async Task ClearAsync_WhenValuePresent_RemovesAnyPresentValues() { var testDirPath = Path.Combine(Environment.CurrentDirectory, "clearasync_test"); var testDir = new DirectoryInfo(testDirPath); if (!testDir.Exists) { testDir.Create(); } var cachePolicy = new FifoCachePolicy <string>(); var input = new byte[] { 1, 2, 3, 4 }; const ulong size = 20; using (var cache = new DiskCache <string>(testDir, cachePolicy, size)) { await cache.SetValueAsync("asd", new MemoryStream(input)).ConfigureAwait(false); await cache.ClearAsync().ConfigureAwait(false); var result = await cache.ContainsKeyAsync("asd").ConfigureAwait(false); Assert.IsFalse(result); } testDir.Delete(true); }
private static async Task <(CatalogPage, bool)> GetPage(CatalogPageIdentifier pageId, bool forceDownload) { string filePath = GetPageCachePath(pageId.Url); if (!File.Exists(filePath)) { Log.Debug($"Cannot find page {pageId.Url}, will download it.."); return(await DownloadPage(pageId).ContinueWith(CachePage), true); } if (forceDownload) { Log.Debug($"Download of page {pageId.Url} was forced.."); return(await DownloadPage(pageId).ContinueWith(CachePage), false); } string pageString = File.ReadAllText(filePath); return(JsonConvert.DeserializeObject <CatalogPage>(pageString), false); CatalogPage CachePage(Task <CatalogPage> i) { return(DiskCache.Cache(filePath, i)); } }
private async void ClearCacheButton_Click(object sender, RoutedEventArgs e) { var diskCache = new DiskCache(); await diskCache.DeleteAllAsync(); await new MessageDialog("done").ShowAsync(); }
private static void Main() { var cacheDir = new DirectoryInfo(@"C:\Users\sjp\Downloads\tmp\cache"); var cachePolicy = new LruCachePolicy <string>(); const ulong maxSize = 1024L * 1024L * 1024L * 1024L; // 1GB using (var diskCache = new DiskCache <string>(cacheDir, cachePolicy, maxSize)) { diskCache.EntryAdded += (_, e) => Console.WriteLine($"Added: { e.Key }, { e.Size }"); using (var flacStream = File.OpenRead(@"C:\Users\sjp\Downloads\05. End Of Days.flac")) diskCache.SetValue("flacFile", flacStream); var containsTest = diskCache.ContainsKey("flacFile"); if (containsTest) { using (var outStr = diskCache.GetValue("flacFile")) using (var tmpFile = File.OpenWrite(@"C:\Users\sjp\Downloads\tmp.flac")) outStr.CopyTo(tmpFile); } Console.WriteLine("Press any key to exit..."); Console.ReadKey(true); } }
void RecursiveAddFilesContent(IDirectoryCache directory, MainBuildResult buildResult, string destDir) { DiskCache.UpdateIfNeeded(directory); foreach (var child in directory) { if (child.IsInvalid) { continue; } var outPathFileName = (destDir != "" ? destDir + "/" : "") + child.Name; buildResult.TakenNames.Add(outPathFileName); if (child is IDirectoryCache) { RecursiveAddFilesContent(child as IDirectoryCache, buildResult, outPathFileName); continue; } if (child is IFileCache) { buildResult.FilesContent.GetOrAddValueRef(outPathFileName) = new Lazy <object>(() => { var res = ((IFileCache)child).ByteContent; ((IFileCache)child).FreeCache(); return(res); }); } } }
public static async Task TryGetValueAsync_WhenEmpty_ReturnsFalseValueNullStreamTuple() { var testDirPath = Path.Combine(Environment.CurrentDirectory, "trygetvalueasync_tuple_test"); var testDir = new DirectoryInfo(testDirPath); if (!testDir.Exists) { testDir.Create(); } var cachePolicy = Mock.Of <ICachePolicy <string> >(); const ulong size = 123; using (var cache = new DiskCache <string>(testDir, cachePolicy, size)) { var(hasValue, stream) = await cache.TryGetValueAsync("asd").ConfigureAwait(false); Assert.Multiple(() => { Assert.IsFalse(hasValue); Assert.IsNull(stream); }); } testDir.Delete(true); }
private async void RefreshButton_Click(object sender, RoutedEventArgs e) { var diskCache = new DiskCache(); var cacheSize = await diskCache.CalculateAllSizeAsync(); CacheSizeTextBlock.Text = cacheSize.ToString(); }
public RepresentationsSalesGenerator(string domain, string fontPath, DiskCache cache) { _domain = domain; _fontPath = fontPath; _cache = cache; _zipCompressor = new ZipCompressor(); }
public void GetValidatedAbsolutePath_Virtual_Outside_WebRoot() { string virtualCachePath; var absPath = DiskCache.GetValidatedCachePathsImpl("~/../OUTSIDE", TestMapPath, GetDirectoryInfo, out virtualCachePath); Assert.AreEqual(@"X:\Sites\OUTSIDE", absPath); Assert.AreEqual(null, virtualCachePath); }
private DependencyChain <AnalyzedPackage> CacheDependencies(IPackage package, Task <DependencyChain <AnalyzedPackage> > dependencies) { if (!this.config.PackageDetails.DoCache) { return(dependencies.Result); } return(DiskCache.Cache(string.Format(this.resolvedPathFormatStr, package.Id, package.Version), dependencies)); }
public async Task RunSwitchRefresh() { try { var switchFile = new FileInfo(Path.Combine(_failoverDir, UtilAndComs.FAILOVER_SWITCH)); if (!switchFile.Exists) { switchParams.AddOrUpdate(FAILOVER_MODE_PARAM, bool.FalseString, (x, y) => bool.FalseString); _logger?.LogDebug("failover switch is not found, {0}", switchFile.Name); return; } long modified = switchFile.LastWriteTime.ToTimestamp(); if (lastModifiedMillis < modified) { lastModifiedMillis = modified; string failover = await switchFile.ReadFileAsync().ConfigureAwait(false); if (failover.IsNotNullOrWhiteSpace()) { string[] lines = failover.SplitByString(DiskCache.GetLineSeparator()); foreach (var line in lines) { string l = line.Trim(); if (IS_FAILOVER_MODE.Equals(l)) { switchParams.AddOrUpdate(FAILOVER_MODE_PARAM, bool.TrueString, (x, y) => bool.TrueString); _logger?.LogInformation("failover-mode is on"); await RunFailoverFileRead().ConfigureAwait(false); } else if (NO_FAILOVER_MODE.Equals(l)) { switchParams.AddOrUpdate(FAILOVER_MODE_PARAM, bool.FalseString, (x, y) => bool.FalseString); _logger?.LogInformation("failover-mode is off"); } } } else { switchParams.AddOrUpdate(FAILOVER_MODE_PARAM, bool.FalseString, (x, y) => bool.FalseString); } } } catch (Exception e) { _logger?.LogError(e, "[NA] failed to read failover switch."); } }
private Context(ILogger logger, MemoryCache memoryCache, DiskCache diskCache, NativeCache nativeCache, Dictionary <Type, Type> awaiters, Dictionary <Type, Type> mappers) { Logger = logger; MemoryCache = memoryCache; DiskCache = diskCache; NativeCache = nativeCache; awaiterTypes = awaiters; mapperTypes = mappers; }
private VideoProcessor(LanguageShortName shortName) { _shortName = shortName; string cachePath = Path.Combine(YandexVideoSearcher.BASE_PATH, "VideoDataCache", _shortName.ToString()); _diskCache = new DiskCache(cachePath, false); _linksHelper = new VideoLinksHelper(YandexVideoSearcher.GetAllLinksFullFileName(YandexVideoSearcher.BASE_PATH, shortName)); }
internal async Task <DependencyChain <AnalyzedPackage> > FindPackageDependencies(Package package, HashSet <string> upstreamDependencies) { string resolvedDependenciesPath = string.Format(this.resolvedPathFormatStr, package.Id, package.Version); if (DiskCache.TryGetValue(resolvedDependenciesPath, this.config.ResolvedDependencies, out DependencyChain <AnalyzedPackage>?cached)) { return(cached); } string path = string.Format(this.detailsPathFormatStr, package.Id, package.Version); NpmPackage?npmPackage; if (!DiskCache.TryGetValue(path, this.config.PackageDetails, out npmPackage)) { npmPackage = await this.npm.GetPackage(package).ContinueWith(CachePackage); } if (npmPackage == null) { Log.Warning($"Could not find information about {package.Id} {package.Version} from npm"); return(new InvalidDependencyChain(package, "Could not find information from npm")); } var analyzedPackage = new AnalyzedPackage(package); var packageDependencies = npmPackage.Dependencies ?? new Dictionary <string, string>(); var ds = packageDependencies.Select(x => new PackageRange(x.Key, x.Value, package.OriginProject)); var dependencies = await FindPackageDependencies(ds, upstreamDependencies); var result = new DependencyChain <AnalyzedPackage>(analyzedPackage, dependencies); CacheDependencies(package, result); return(result); NpmPackage?CachePackage(Task <(PackageDetailsResultEnum, NpmPackage?)> package) { if (!this.config.PackageDetails.DoCache) { return(package.Result.Item2); } if (package.Result.Item1 != PackageDetailsResultEnum.Success) { return(null); } NpmPackage p = package.Result.Item2 !; string cachePath = string.Format(this.detailsPathFormatStr, p.Id, p.Version); DiskCache.Cache(cachePath, p); return(p); } }
public AllMaterialsSalesGenerator(string domain, string fontPath, DiskCache cache, Func <SectionId, bool> forbiddenSectionsChecker) { _domain = domain; _fontPath = fontPath; _cache = cache; _forbiddenSectionsChecker = forbiddenSectionsChecker; _zipCompressor = new ZipCompressor(); }
public void Destroy() { memoryCache = null; diskCache?.Dispose(); diskCache = null; nativeCache?.Dispose(); nativeCache = null; Instance = null; }
static bool Pull(Options opt) { bool result = false; opt.Status = Options.TheadStatus.Running; opt.ThreadStartTime = DateTime.Now; opt.ThreadCounterTime = DateTime.Now; opt.LogInformation("\r\nPull " + opt.Name + "\r\n Pulling from : " + opt.InputUri + "\r\n Storing in : " + opt.OutputUri); DiskCache d = new DiskCache(); d.Initialize(opt.OutputUri); ManifestManager mc = ManifestManager.CreateManifestCache(new Uri(opt.InputUri), (ulong)opt.MinBitrate, (ulong)opt.MaxBitrate, opt.AudioTrackName, opt.TextTrackName, opt.MaxDuration, (ulong)opt.BufferSize, opt.LiveOffset); mc.SetManifestOutput(d); var t = d.RemoveAsset(mc); t.Wait(); t = mc.DownloadManifest(); t.Wait(); result = t.Result; if (result == true) { var tt = mc.StartDownloadChunks(); tt.Wait(); result = tt.Result; while (mc.GetAssetStatus() != AssetStatus.ChunksDownloaded) { if (opt.CounterPeriod > 0) { System.Threading.Tasks.Task.Delay(opt.CounterPeriod * 1000 / 10).Wait(); if ((opt.ListCounters == null) || (opt.ListCounters.Count == 0)) { CreatePullCounters(opt, mc); } else { UpdatePullCounters(opt, mc); } } else { System.Threading.Tasks.Task.Delay(1000).Wait(); } } } opt.LogInformation("Pull " + opt.Name + " done"); opt.Status = Options.TheadStatus.Stopped; return(result); }
/// <summary> /// 定时刷新灾备状态,主要根据灾备目录下对应文件的内容 /// </summary> private void SwitchRefresher() { _switchRefresher = new Timer(x => { try { string filePath = Path.Combine(_failoverDir, UtilAndComs.FAILOVER_SWITCH); if (!DiskCache.IsFileExists(filePath)) { _switchParams.AddOrUpdate(FAILOVER_MODE_NAME, "false", (k, v) => "false"); _logger.Debug($"failover switch is not found, {filePath}"); return; } long modified = DiskCache.GetFileLastModifiedTime(filePath); if (_failoverLastModifiedMillis < modified) { _failoverLastModifiedMillis = modified; string failover = DiskCache.ReadFile(filePath); if (!string.IsNullOrEmpty(failover)) { var lines = failover.Split(new string[] { DiskCache.GetLineSeparator() }, StringSplitOptions.RemoveEmptyEntries); foreach (var line in lines) { if ("1".Equals(line.Trim())) { _switchParams.AddOrUpdate(FAILOVER_MODE_NAME, "true", (k, v) => "true"); _logger.Info($"{FAILOVER_MODE_NAME} is on"); FailoverFileReader(); } else if ("0".Equals(line.Trim())) { _switchParams.AddOrUpdate(FAILOVER_MODE_NAME, "false", (k, v) => "false"); _logger.Info($"{FAILOVER_MODE_NAME} is off"); } } } else { _switchParams.AddOrUpdate(FAILOVER_MODE_NAME, "false", (k, v) => "false"); } } } catch (Exception ex) { _logger.Error(ex, "[NA] failed to read failover switch."); } _switchRefresher.Change(SWITCH_REFRESHER_PERIOD, Timeout.Infinite); }, null, SWITCH_REFRESHER_DUETIME, Timeout.Infinite); }