private bool TryGetModuleModel(string moduleName, string dbPath, out ModuleModel model)
        {
            model = null;

            if (string.IsNullOrEmpty(dbPath))
            {
                return(false);
            }

            if (_modelsCache.TryGetValue(moduleName, out model))
            {
                return(true);
            }

            model = WithRetries.Execute(() => {
                using (var db = new LiteDatabase(dbPath)) {
                    var modules              = db.GetCollection <ModuleModel>("modules");
                    var storedModel          = modules.FindOne(m => m.Name == moduleName);
                    _modelsCache[moduleName] = storedModel;
                    return(storedModel);
                }
            }, $"Unable to locate database for module {moduleName}.", _log);

            return(model != null);
        }
        private void Write(ModuleModel model, CancellationToken cancellationToken)
        {
            if (cancellationToken.IsCancellationRequested)
            {
                return;
            }

            WithRetries.Execute(() => {
                if (!_fs.DirectoryExists(_cacheFolder))
                {
                    _fs.CreateDirectory(_cacheFolder);
                }
                return(true);
            }, $"Unable to create directory {_cacheFolder} for modules cache.", _log);

            WithRetries.Execute(() => {
                if (cancellationToken.IsCancellationRequested)
                {
                    return(false);
                }
                using (var db = new LiteDatabase(Path.Combine(_cacheFolder, $"{model.UniqueId}.db"))) {
                    var modules = db.GetCollection <ModuleModel>("modules");
                    modules.Upsert(model);
                    modules.EnsureIndex(x => x.Name);
                }
                return(true);
            }, $"Unable to write analysis of {model.Name} to database.", _log);
        }
        /// <summary>
        /// Determines if module analysis exists in the storage.
        /// </summary>
        public bool ModuleExistsInStorage(string name, string filePath, ModuleType moduleType)
        {
            if (GetCachingLevel() == AnalysisCachingLevel.None)
            {
                return(false);
            }

            var key = new AnalysisModuleKey(name, filePath);

            if (_searchResults.TryGetValue(key, out var result))
            {
                return(result);
            }

            WithRetries.Execute(() => {
                var dbPath          = FindDatabaseFile(name, filePath, moduleType);
                _searchResults[key] = result = !string.IsNullOrEmpty(dbPath);
                return(result);
            }, "Unable to find database file for {name}.", _log);
            return(false);
        }