/// <summary> /// Closes the cache session /// </summary> public override Possible <string, Failure> Close() { if (m_session != null) { return(m_session.CloseAsync(LoggingContext.ActivityId).GetAwaiter().GetResult()); } return(new Possible <string, Failure>("Success, no session to close.")); }
internal static async Task <Possible <CacheCoreCacheInitializer> > TryInitializeCacheInternalAsync( LoggingContext loggingContext, PathTable pathTable, string cacheDirectory, ICacheConfiguration config, bool enableFingerprintLookup, RootTranslator rootTranslator) { Contract.Requires(pathTable != null); Contract.Requires(pathTable.IsValid); Contract.Requires(config != null); Contract.Requires(config.CacheLogFilePath.IsValid); Contract.Requires(config.CacheConfigFile.IsValid); Contract.Requires(!string.IsNullOrWhiteSpace(cacheDirectory)); bool succeeded = false; ICacheCoreCache cache = null; ICacheCoreSession session = null; try { Possible <ICacheConfigData> cacheConfigData = TryGetCacheConfigData(pathTable, cacheDirectory, config, rootTranslator); if (!cacheConfigData.Succeeded) { return(cacheConfigData.Failure); } Possible <ICacheCoreCache> maybeCache = await CacheFactory.InitializeCacheAsync(cacheConfigData.Result, loggingContext.ActivityId, config); if (!maybeCache.Succeeded) { return(maybeCache.Failure); } // We are now responsible for shutting this down (even if something later fails). cache = maybeCache.Result; cache.SuscribeForCacheStateDegredationFailures( failure => { Tracing.Logger.Log.CacheReportedRecoverableError(loggingContext, failure.DescribeIncludingInnerFailures()); }); // Log the cache ID we got. Tracing.Logger.Log.CacheInitialized(loggingContext, cache.CacheId); Possible <ICacheCoreSession> maybeSession = string.IsNullOrWhiteSpace(config.CacheSessionName) ? await cache.CreateSessionAsync() : await cache.CreateSessionAsync(config.CacheSessionName); if (!maybeSession.Succeeded) { return(maybeSession.Failure); } session = maybeSession.Result; succeeded = true; return(new CacheCoreCacheInitializer( loggingContext, cache, session, new List <IDisposable>(), enableFingerprintLookup: enableFingerprintLookup, rootTranslator: rootTranslator, replaceExistingFileOnMaterialization: config.ReplaceExistingFileOnMaterialization)); } finally { if (!succeeded) { // Note that we clean up in reverse order that we initialized things. if (session != null) { Analysis.IgnoreResult(await session.CloseAsync(), justification: "Okay to ignore close"); Analysis.IgnoreResult(await cache.ShutdownAsync(), justification: "Okay to ignore shutdown"); } } } }