public bool HasCache(string filter = "*") { if (_dir == null) { _dir = GetCacheFolder(); } return(_dir?.Files(filter)?.Count > 0); }
internal D7FileDownloader(string targetDir, string subUrlPattern, IFileSystemShim fsShim, ID7Client d7Client) { _client = ForwardLogs(d7Client); _fs = ForwardLogs(fsShim); _foldr = _fs.Folder(targetDir); _urlPattern = subUrlPattern; _suffix = "_" + DateTime.Now.TimeOfDay.TotalMinutes; DeleteOldReplacements(); }
private void AddToCache <T>(string resource, T result) where T : new() { if (_dir == null) { _dir = GetCacheFolder(); } string fName; try { fName = GetCacheFileName(resource); } catch (Exception ex) { LogError("GetCacheFileName", ex); return; } FileShim file; try { file = _dir.File(fName, false); } catch (Exception ex) { LogError("_dir.File", ex); return; } string json; try { json = _serialzr.Write(result, false); } catch (Exception ex) { LogError("_serialzr.Write", ex); return; } if (json.IsBlank()) { Error_n("json.IsBlank", ""); return; } if (file == null) { Error_n("file == null", ""); return; } if (_fsShim == null) { Error_n("_fsShim == null", ""); return; } string err = null; try { file.Write(json, raiseLogEvents: false); //_fsShim.TryWriteFile(file.Path, out err, json, EncodeAs.UTF8); } catch (Exception ex) { Error_n("Failed to write file.", file?.Path + L.f + err); LogError("file.Write", ex); } }
public bool ClearCache(string filter = "*") { if (_dir == null) { _dir = GetCacheFolder(); } var files = _dir?.Files(filter); if (files.Count == 0) { return(true); } return(files.All(x => x.Delete())); }
protected virtual async Task <T> TryReadCache <T>(string resource) where T : new() { if (_dir == null) { _dir = GetCacheFolder(); } var file = _dir.File(GetCacheFileName(resource), false); if (!file._Found) { Debug_n("Resource NOT in cache.", resource); return(default(T)); } //Trace_n("Resource is cached.", resource); var ret = _serialzr.Read <T>(file, false); await TaskEx.Delay(1); return(ret); }