/// <summary> /// Get a string from a file cache if it's newer than a given expiry date /// </summary> /// <param name="cacheRoot">path to the root of the cache directory</param> /// <param name="cacheName">Name of the subdirectory in which to look for this item</param> /// <param name="itemName">Name of the cache file to create</param> /// <param name="Expires">If the date on the file is older than this date, don't return the string. /// This value will contain the actual date of the file on return from this method</param> /// <param name="Value">String containing the contents of this cache item</param> /// <returns>True if a valid item is returned from the cache, false if no item found or it's out of date</returns> public static bool GetItem(IDnaDiagnostics diagnostics, string cacheRoot, string cacheName, string itemName, ref DateTime Expires, ref string Value) { if (cacheRoot.Length == 0) { // Can't cache if we don't have a root directory return false; } string safeItemName = StringUtils.MakeStringFileNameSafe(itemName); string fullPath = Path.Combine(Path.Combine(cacheRoot, cacheName), safeItemName); // WIN32_FIND_DATA FindFileData; FileInfo info = new System.IO.FileInfo(fullPath); bool gotCachedFile = false; if (info.Exists) { // Check the expiry date DateTime fileDate = info.LastWriteTime; if (fileDate >= (Expires)) { gotCachedFile = true; try { using (StreamReader reader = new StreamReader(fullPath)) { Value = reader.ReadToEnd(); } } catch (System.IO.IOException ex) { ex.Data["MethodName"] = "CacheGetItem"; diagnostics.WriteExceptionToLog(ex); return false; } } Expires = fileDate; } else { diagnostics.WriteWarningToLog("CacheGetItem", "Cannot find " + fullPath); return false; } if (gotCachedFile) { Statistics.AddCacheHit(); } else { Statistics.AddCacheMiss(); } return gotCachedFile; }
/// <summary> /// Returns the status of the objects within the signal /// </summary> /// <returns></returns> static public SignalStatus GetStatus(IDnaDiagnostics dnaDiagnostics) { var signalStatus = new SignalStatus(); foreach (var signalObj in signalObjects) { try { var obj = (ISignalBase)signalObj.Value; signalStatus.Members.Add(obj.GetStats(obj.GetType())); } catch (Exception e) { dnaDiagnostics.WriteExceptionToLog(e); } } return signalStatus; }
/// <summary> /// Puts a string value into a file-based cache. Compatible with Ripley's caching methods /// </summary> /// <param name="cacheRoot">Directory root of the global file cache</param> /// <param name="cacheName">Name of subdirectory to cache in</param> /// <param name="itemName">Name of file to cache</param> /// <param name="Text">String value to store in cache (usualy XML but doesn't have to be</param> /// <returns>True if cached successfully. False otherwise.</returns> public static bool PutItem(IDnaDiagnostics diagnostics, string cacheRoot, string cacheName, string itemName, string Text) { if (cacheRoot.Length == 0) { // Can't cache if we don't have a root directory return false; } string safeItemName = StringUtils.MakeStringFileNameSafe(itemName); string cacheDir = Path.Combine(cacheRoot, cacheName); string fullPath = Path.Combine(cacheDir, safeItemName); bool directoryExists = false; try { DirectoryInfo hFind = new DirectoryInfo(cacheDir); if (hFind.Exists) { directoryExists = true; } else { directoryExists = Directory.CreateDirectory(cacheDir).Exists; // Gosh that's so much simpler } if (directoryExists) { using (StreamWriter writer = new StreamWriter(fullPath)) { writer.Write(Text); } } else { return false; } return true; } catch (Exception ex) { ex.Data["MethodName"] = "CachePutItem"; diagnostics.WriteExceptionToLog(ex); return false; } }