/// <summary> /// Save item to disk and clear from Storage. /// </summary> /// <param name="StorageID"></param> public void Store(string StorageID) { // Lock is called prior to calling Store. if (Storage.ContainsKey(StorageID)) { if (PersistenceFilter.Invoke(Storage[StorageID])) { var di = Directory.CreateDirectory(FolderPath); var success = false; var startTime = DateTime.Now; while (success == false && DateTime.Now - startTime < TimeSpan.FromSeconds(5)) { try { File.WriteAllText(Path.Combine(di.FullName, $"{Storage[StorageID].StorageID}.json"), JSON.Encode(Storage[StorageID])); success = true; } catch { System.Threading.Thread.Sleep(500); } } if (success) { Storage.Remove(StorageID); } else { PersistErrorAction.Invoke(); } } } }
static ReportStore LoadReport(string fileName, TxtSettings textSettings) { using (var file = File.OpenRead(fileName)) using (var reader = new StreamReader(file)) { var report = TextRendering.PrepareReport(PersistenceFilter.Load(reader), textSettings); return(new ReportStore(report, new DefaultResourceLocator())); } }
public static Report Clone(this Report report) { var ms = new MemoryStream(); using (var xmlWriter = new XmlTextWriter(ms, Encoding.UTF8)) { new PersistenceFilter(ReportComponentFactory.Instance, report, new DefaultResourceLocator()).SerializeRoot(report, xmlWriter); } return(PersistenceFilter.Load(new StreamReader(new MemoryStream(ms.ToArray())))); }
/// <summary> /// Persists items in Storage every interval. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void SaveTimer_Elapsed(object sender, ElapsedEventArgs e) { lock (LockObject) { for (var i = Storage.Count - 1; i >= 0; i--) { var item = Storage.Values[i]; if (PersistenceFilter.Invoke(item)) { if (DateTime.Now - item.LastAccessed >= MemCacheTime) { Store(item.StorageID); } else { var di = Directory.CreateDirectory(FolderPath); var success = false; var startTime = DateTime.Now; while (success == false && DateTime.Now - startTime < TimeSpan.FromSeconds(5)) { try { File.WriteAllText(Path.Combine(di.FullName, $"{item.StorageID}.json"), JSON.Encode(item)); success = true; } catch { System.Threading.Thread.Sleep(500); } } if (!success) { PersistErrorAction.Invoke(); } } } } } }
/// <summary> /// Add new item to Storage. /// </summary> /// <param name="NewItem"></param> public void Add(T NewItem) { if (FolderPath == null) { throw new Exception("FolderPath must have a value."); } if (!(NewItem is IStorageItem)) { throw new Exception("Item must implement interface IStorageItem."); } if (String.IsNullOrWhiteSpace(NewItem.StorageID)) { throw new Exception("StorageID cannot be empty."); } foreach (var character in Path.GetInvalidFileNameChars()) { if (NewItem.StorageID.Contains(character)) { throw new Exception("StorageID can only contain characters allowable in file names."); } } lock (LockObject) { if (Storage.ContainsKey(NewItem.StorageID)) { throw new Exception("Item with same StorageID already exists."); } var di = Directory.CreateDirectory(FolderPath); if (Storage.ContainsKey(NewItem.StorageID) || File.Exists(Path.Combine(di.FullName, $"{NewItem.StorageID}.json"))) { throw new Exception("Item with same StorageID already exists."); } Storage.Add(NewItem.StorageID, NewItem); if (PersistenceFilter.Invoke(NewItem)) { File.WriteAllText(Path.Combine(di.FullName, $"{NewItem.StorageID}.json"), JSON.Encode(NewItem)); } } }