public static T CheckCache <T>(CacheIdentifier i) { string cachedir = ConfigurationManager.AppSettings["cache_dir"]; string totalfile = System.IO.Path.Combine(cachedir, i.ToString()); if (!System.IO.File.Exists(totalfile)) { return(default(T)); } System.IO.FileStream fs = new System.IO.FileStream(totalfile, System.IO.FileMode.Open, System.IO.FileAccess.Read); if (typeof(T) == typeof(byte[])) { byte[] data = new byte[fs.Length]; fs.Read(data, 0, data.Length); fs.Close(); return((T)(object)data); } else { System.Runtime.Serialization.Json.DataContractJsonSerializer ser = new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(T)); byte[] data = new byte[fs.Length]; fs.Read(data, 0, data.Length); fs.Close(); T ret = (T)ser.ReadObject(new System.IO.MemoryStream(data)); return(ret); } fs.Close(); return(default(T)); }
public static CacheFile GetCacheFile(CacheIdentifier i) { CacheFile ret = null; string cachedir = ConfigurationManager.AppSettings["cache_dir"]; string totalfile = System.IO.Path.Combine(cachedir, i.ToString()); if (System.IO.File.Exists(totalfile)) { System.IO.FileStream fs = new System.IO.FileStream(totalfile, System.IO.FileMode.Open, System.IO.FileAccess.Read); ret = new CacheFile(); ret.data = new byte[fs.Length]; fs.Read(ret.data, 0, (int)fs.Length); fs.Close(); ret.age = DateTime.Now - System.IO.File.GetCreationTime(totalfile); ret.filename = totalfile; ret.hashcode = i.ToString(); } return(ret); }
public static bool ExpireCache(CacheIdentifier i) { string cachedir = ConfigurationManager.AppSettings["cache_dir"]; string totalfile = System.IO.Path.Combine(cachedir, i.ToString()); try{ System.IO.File.Delete(totalfile); return(true); } catch (Exception e) { return(false); } return(false); }
public static void Cache <T>(ref T obj, CacheIdentifier i) { string cachedir = ConfigurationManager.AppSettings["cache_dir"]; string totalfile = System.IO.Path.Combine(cachedir, i.ToString()); if (System.IO.File.Exists(totalfile)) { System.IO.File.Delete(totalfile); } System.IO.FileStream fs = new System.IO.FileStream(totalfile, System.IO.FileMode.Create, System.IO.FileAccess.Write); if (obj.GetType() == typeof(byte[])) { byte[] data = (byte[])(Object)obj; fs.Write(data, 0, data.Length); } else { System.Runtime.Serialization.Json.DataContractJsonSerializer ser = new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(T)); ser.WriteObject(fs, obj); } fs.Close(); }