public void Clear(string identifier) { lock (GetLockObject(identifier)) { try { lock (memoryCache) memoryCache.Remove(identifier); SQLiteCommand cmd = new SQLiteCommand(sqlSelect, conn); cmd.Parameters.Add(new SQLiteParameter("@Identifier", identifier)); SQLiteDataReader reader = cmd.ExecuteReader(); if (reader.Read()) { CacheItemInfo info = new CacheItemInfo(reader); DeleteFile(info.File); cmd = new SQLiteCommand(sqlDelete, conn); cmd.Parameters.Add(new SQLiteParameter("@Identifier", info.IdentifierString)); cmd.ExecuteNonQuery(); } reader.Close(); } finally { ReleaseLockObject(identifier); } } }
private CacheItemInfo ValidateCacheItem(CacheItemInfo info) { bool fileExists = File.Exists(info.PhysicalPath); SQLiteCommand cmd; if (info.ExpiryDate < SprocketDate.Now || !fileExists) { cmd = new SQLiteCommand(sqlDelete, conn); cmd.Parameters.Add(new SQLiteParameter("@Identifier", info.IdentifierString)); cmd.ExecuteNonQuery(); if (fileExists) { try { DeleteFile(info.File); } catch { } } return(null); } if (!info.ForceExpiryAfterDuration && info.ExpiryDate != DateTime.MaxValue) { info.ExpiryDate = SprocketDate.Now.Add(info.ExpiryDate - info.LastAccess); } info.LastAccess = SprocketDate.Now; cmd = new SQLiteCommand(sqlUpdate, conn); cmd.Parameters.Add(new SQLiteParameter("@Identifier", info.IdentifierString)); cmd.Parameters.Add(new SQLiteParameter("@LastAccess", info.LastAccess)); cmd.Parameters.Add(new SQLiteParameter("@ExpiryDate", info.ExpiryDate)); new Thread(new ParameterizedThreadStart(AccessThread)).Start(cmd); //cmd.ExecuteNonQuery(); return(info); }
public void FlushCache(long toSize) { lock (lockObjects) { SQLiteCommand cmd = new SQLiteCommand(ResourceLoader.LoadTextResource("Sprocket.Web.Cache.cache-select-all.sql"), conn); SQLiteDataReader reader = cmd.ExecuteReader(); long size = 0; bool killitems = false; lock (memoryCache) while (reader.Read()) { CacheItemInfo info = new CacheItemInfo(reader); if (killitems) { memoryCache.Remove(info.IdentifierString); DeleteFile(info.File); } else { size += info.File.Length; if (size >= toSize) { killitems = true; } } } diskSpace = size; reader.Close(); } }
public void Initialise() { basePath = WebUtility.MapPath("datastore/content-cache") + @"\"; conn = new SQLiteConnection(SQLiteDatabase.CreateConnectionString("datastore/content-cache/cache.s3db")); SQLiteDatabase.CheckFileExists(conn.ConnectionString); conn.Open(); SQLiteCommand cmd = new SQLiteCommand(ResourceLoader.LoadTextResource("Sprocket.Web.Cache.cache-init.sql"), conn); cmd.ExecuteNonQuery(); cmd = new SQLiteCommand(ResourceLoader.LoadTextResource("Sprocket.Web.Cache.cache-select-all.sql"), conn); SQLiteDataReader reader = cmd.ExecuteReader(); while (reader.Read()) { CacheItemInfo info = new CacheItemInfo(reader); if (File.Exists(info.PhysicalPath)) { if (info.SprocketPath != null) { lock (sprocketPathMemoryCache) sprocketPathMemoryCache[info.SprocketPath] = info; } lock (memoryCache) memoryCache[info.IdentifierString] = info; diskSpace += info.File.Length; } } reader.Close(); sqlInsert = ResourceLoader.LoadTextResource("Sprocket.Web.Cache.cache-insert.sql"); sqlDelete = ResourceLoader.LoadTextResource("Sprocket.Web.Cache.cache-delete.sql"); sqlDeletePartialMatches = ResourceLoader.LoadTextResource("Sprocket.Web.Cache.cache-delete-partial-matches.sql"); sqlUpdate = ResourceLoader.LoadTextResource("Sprocket.Web.Cache.cache-update.sql"); sqlSelect = ResourceLoader.LoadTextResource("Sprocket.Web.Cache.cache-select.sql"); sqlSelectAll = ResourceLoader.LoadTextResource("Sprocket.Web.Cache.cache-select-from-path.sql"); }
public bool Transmit(HttpResponse response, string sprocketPath) { CacheItemInfo info = null; bool memCached; lock (sprocketPathMemoryCache) memCached = sprocketPathMemoryCache.TryGetValue(sprocketPath, out info); if (info != null) { info = ValidateCacheItem(info); } if (info == null) { info = CheckDBForSprocketPath(sprocketPath); if (info == null) { return(false); } lock (sprocketPathMemoryCache) sprocketPathMemoryCache[sprocketPath] = info; } if (!File.Exists(info.PhysicalPath)) { if (memCached) { lock (sprocketPathMemoryCache) sprocketPathMemoryCache.Remove(sprocketPath); } return(false); } response.ContentType = info.ContentType; response.TransmitFile(info.PhysicalPath); return(true); }
public static void Store(string identifier, TimeSpan?expiresAfter, bool forceExpiryAfterDuration, Stream data) { CacheItemInfo info = new CacheItemInfo(identifier, expiresAfter, forceExpiryAfterDuration, null, null); CacheManager cache = HttpContext.Current.Application[appStateKey] as CacheManager; if (cache.DiskSpaceUsed >= Instance.DiskCacheMaxSize) { cache.FlushCache(Instance.DiskCacheTrimSize); } cache.Write(info, data); }
public static void StoreText(string identifier, TimeSpan?expiresAfter, bool forceExpiryAfterDuration, string sprocketPath, string contentType, string text) { CacheItemInfo info = new CacheItemInfo(identifier, expiresAfter, forceExpiryAfterDuration, sprocketPath, contentType); CacheManager cache = HttpContext.Current.Application[appStateKey] as CacheManager; if (cache.DiskSpaceUsed >= Instance.DiskCacheMaxSize) { cache.FlushCache(Instance.DiskCacheTrimSize); } cache.WriteText(info, text); }
public static string StoreText(string identifier, TimeSpan?expiresAfter, bool forceExpiryAfterDuration, string text) { CacheItemInfo info = new CacheItemInfo(identifier, expiresAfter, forceExpiryAfterDuration, null, null); CacheManager cache = HttpContext.Current.Application[appStateKey] as CacheManager; if (cache.DiskSpaceUsed >= Instance.DiskCacheMaxSize) { cache.FlushCache(Instance.DiskCacheTrimSize); } cache.WriteText(info, text); return(info.PhysicalPath); }
public void WriteText(CacheItemInfo info, string text) { lock (memoryCache) if (memoryCache.ContainsKey(info.IdentifierString)) { CacheItemInfo c = memoryCache[info.IdentifierString]; memoryCache.Remove(c.IdentifierString); DeleteFile(c.File); } if (info.SprocketPath != null) { lock (sprocketPathMemoryCache) sprocketPathMemoryCache.Remove(info.SprocketPath); } lock (GetLockObject(info.IdentifierString)) { try { FileInfo file = info.File; if (!file.Directory.Exists) { file.Directory.Create(); } SQLiteCommand cmd = new SQLiteCommand(sqlInsert, conn); info.AddSqlParameters(cmd); cmd.ExecuteNonQuery(); File.WriteAllText(info.PhysicalPath, text, Encoding.Unicode); lock (memoryCache) memoryCache[info.IdentifierString] = info; file.Refresh(); diskSpace += file.Length; } catch { DeleteFile(info.File); throw; } finally { ReleaseLockObject(info.IdentifierString); } } }
public string ReadText(string identifier) { lock (GetLockObject(identifier)) { try { CacheItemInfo info = FromIdentifier(identifier); if (info == null) { return(null); } return(File.ReadAllText(info.PhysicalPath, Encoding.Unicode)); } finally { ReleaseLockObject(identifier); } } }
public Stream Read(string identifier) { lock (GetLockObject(identifier)) { try { CacheItemInfo info = FromIdentifier(identifier); if (info == null) { return(null); } MemoryStream stream = new MemoryStream(File.ReadAllBytes(info.PhysicalPath)); stream.Seek(0, SeekOrigin.Begin); return(stream); } finally { ReleaseLockObject(identifier); } } }
private CacheItemInfo FromIdentifier(string identifier) { CacheItemInfo info = null; bool memCached; // try to get the item from the memory cache lock (memoryCache) memCached = memoryCache.TryGetValue(identifier, out info); if (info != null) { info = ValidateCacheItem(info); } // if the item wasn't in the memory cache, check the db cache if (info == null) { info = CheckDBForIdentifier(identifier); if (info == null) { return(null); } // put the item into the memory cache lock (memoryCache) memoryCache[identifier] = info; diskSpace += info.File.Length; } if (!File.Exists(info.PhysicalPath)) { lock (memoryCache) memoryCache.Remove(identifier); return(null); } return(info); }
public void FlushCache(long toSize) { lock (lockObjects) { SQLiteCommand cmd = new SQLiteCommand(ResourceLoader.LoadTextResource("Sprocket.Web.Cache.cache-select-all.sql"), conn); SQLiteDataReader reader = cmd.ExecuteReader(); long size = 0; bool killitems = false; lock (memoryCache) while (reader.Read()) { CacheItemInfo info = new CacheItemInfo(reader); if (killitems) { memoryCache.Remove(info.IdentifierString); DeleteFile(info.File); } else { size += info.File.Length; if (size >= toSize) killitems = true; } } diskSpace = size; reader.Close(); } }
public void Write(CacheItemInfo info, Stream stream) { lock (memoryCache) if (memoryCache.ContainsKey(info.IdentifierString)) { CacheItemInfo c = memoryCache[info.IdentifierString]; memoryCache.Remove(c.IdentifierString); DeleteFile(c.File); } if (info.SprocketPath != null) lock (sprocketPathMemoryCache) sprocketPathMemoryCache.Remove(info.SprocketPath); lock (GetLockObject(info.IdentifierString)) { try { SQLiteCommand cmd = new SQLiteCommand(sqlInsert, conn); info.AddSqlParameters(cmd); cmd.ExecuteNonQuery(); if (stream.CanSeek) stream.Seek(0, SeekOrigin.Begin); FileInfo file = info.File; if (!file.Directory.Exists) file.Directory.Create(); DeleteFile(info.File); using (FileStream fs = File.Create(info.PhysicalPath)) { byte[] buffer = new byte[100000]; int n; do { n = stream.Read(buffer, 0, buffer.Length); if (n == 0) break; fs.Write(buffer, 0, n); } while (true); fs.Flush(); fs.Close(); } lock (memoryCache) memoryCache[info.IdentifierString] = info; file.Refresh(); diskSpace += file.Length; } catch (IOException) { return; } catch { DeleteFile(info.File); throw; } finally { ReleaseLockObject(info.IdentifierString); } } }
public void WriteText(CacheItemInfo info, string text) { lock (memoryCache) if (memoryCache.ContainsKey(info.IdentifierString)) { CacheItemInfo c = memoryCache[info.IdentifierString]; memoryCache.Remove(c.IdentifierString); DeleteFile(c.File); } if (info.SprocketPath != null) lock (sprocketPathMemoryCache) sprocketPathMemoryCache.Remove(info.SprocketPath); lock (GetLockObject(info.IdentifierString)) { try { FileInfo file = info.File; if (!file.Directory.Exists) file.Directory.Create(); SQLiteCommand cmd = new SQLiteCommand(sqlInsert, conn); info.AddSqlParameters(cmd); cmd.ExecuteNonQuery(); File.WriteAllText(info.PhysicalPath, text, Encoding.Unicode); lock (memoryCache) memoryCache[info.IdentifierString] = info; file.Refresh(); diskSpace += file.Length; } catch { DeleteFile(info.File); throw; } finally { ReleaseLockObject(info.IdentifierString); } } }
public static void StoreText(string identifier, TimeSpan? expiresAfter, bool forceExpiryAfterDuration, string sprocketPath, string contentType, string text) { CacheItemInfo info = new CacheItemInfo(identifier, expiresAfter, forceExpiryAfterDuration, sprocketPath, contentType); CacheManager cache = HttpContext.Current.Application[appStateKey] as CacheManager; if (cache.DiskSpaceUsed >= Instance.DiskCacheMaxSize) cache.FlushCache(Instance.DiskCacheTrimSize); cache.WriteText(info, text); }
public static string StoreText(string identifier, TimeSpan? expiresAfter, bool forceExpiryAfterDuration, string text) { CacheItemInfo info = new CacheItemInfo(identifier, expiresAfter, forceExpiryAfterDuration, null, null); CacheManager cache = HttpContext.Current.Application[appStateKey] as CacheManager; if (cache.DiskSpaceUsed >= Instance.DiskCacheMaxSize) cache.FlushCache(Instance.DiskCacheTrimSize); cache.WriteText(info, text); return info.PhysicalPath; }
public static string Store(string identifier, TimeSpan? expiresAfter, bool forceExpiryAfterDuration, string sprocketPath, string contentType, Stream data) { CacheItemInfo info = new CacheItemInfo(identifier, expiresAfter, forceExpiryAfterDuration, sprocketPath, contentType); CacheManager cache = HttpContext.Current.Application[appStateKey] as CacheManager; if (cache.DiskSpaceUsed >= Instance.DiskCacheMaxSize) cache.FlushCache(Instance.DiskCacheTrimSize); cache.Write(info, data); return info.PhysicalPath; }
public static void Store(string identifier, TimeSpan? expiresAfter, bool forceExpiryAfterDuration, Stream data) { CacheItemInfo info = new CacheItemInfo(identifier, expiresAfter, forceExpiryAfterDuration, null, null); CacheManager cache = HttpContext.Current.Application[appStateKey] as CacheManager; if (cache.DiskSpaceUsed >= Instance.DiskCacheMaxSize) cache.FlushCache(Instance.DiskCacheTrimSize); cache.Write(info, data); }
private CacheItemInfo ValidateCacheItem(CacheItemInfo info) { bool fileExists = File.Exists(info.PhysicalPath); SQLiteCommand cmd; if (info.ExpiryDate < DateTime.UtcNow || !fileExists) { cmd = new SQLiteCommand(sqlDelete, conn); cmd.Parameters.Add(new SQLiteParameter("@Identifier", info.IdentifierString)); cmd.ExecuteNonQuery(); if (fileExists) try { DeleteFile(info.File); } catch { } return null; } if (!info.ForceExpiryAfterDuration && info.ExpiryDate != DateTime.MaxValue) info.ExpiryDate = DateTime.UtcNow.Add(info.ExpiryDate - info.LastAccess); info.LastAccess = DateTime.UtcNow; cmd = new SQLiteCommand(sqlUpdate, conn); cmd.Parameters.Add(new SQLiteParameter("@Identifier", info.IdentifierString)); cmd.Parameters.Add(new SQLiteParameter("@LastAccess", info.LastAccess)); cmd.Parameters.Add(new SQLiteParameter("@ExpiryDate", info.ExpiryDate)); new Thread(new ParameterizedThreadStart(AccessThread)).Start(cmd); //cmd.ExecuteNonQuery(); return info; }
public void Initialise() { basePath = WebUtility.MapPath("datastore/content-cache") + @"\"; conn = new SQLiteConnection(SQLiteDatabase.CreateConnectionString("datastore/content-cache/cache.s3db")); SQLiteDatabase.CheckFileExists(conn.ConnectionString); conn.Open(); SQLiteCommand cmd = new SQLiteCommand(ResourceLoader.LoadTextResource("Sprocket.Web.Cache.cache-init.sql"), conn); cmd.ExecuteNonQuery(); cmd = new SQLiteCommand(ResourceLoader.LoadTextResource("Sprocket.Web.Cache.cache-select-all.sql"), conn); SQLiteDataReader reader = cmd.ExecuteReader(); while (reader.Read()) { CacheItemInfo info = new CacheItemInfo(reader); if (File.Exists(info.PhysicalPath)) { if (info.SprocketPath != null) lock (sprocketPathMemoryCache) sprocketPathMemoryCache[info.SprocketPath] = info; lock (memoryCache) memoryCache[info.IdentifierString] = info; diskSpace += info.File.Length; } } reader.Close(); sqlInsert = ResourceLoader.LoadTextResource("Sprocket.Web.Cache.cache-insert.sql"); sqlDelete = ResourceLoader.LoadTextResource("Sprocket.Web.Cache.cache-delete.sql"); sqlDeletePartialMatches = ResourceLoader.LoadTextResource("Sprocket.Web.Cache.cache-delete-partial-matches.sql"); sqlUpdate = ResourceLoader.LoadTextResource("Sprocket.Web.Cache.cache-update.sql"); sqlSelect = ResourceLoader.LoadTextResource("Sprocket.Web.Cache.cache-select.sql"); sqlSelectAll = ResourceLoader.LoadTextResource("Sprocket.Web.Cache.cache-select-from-path.sql"); }
public void Write(CacheItemInfo info, Stream stream) { lock (memoryCache) if (memoryCache.ContainsKey(info.IdentifierString)) { CacheItemInfo c = memoryCache[info.IdentifierString]; memoryCache.Remove(c.IdentifierString); DeleteFile(c.File); } if (info.SprocketPath != null) { lock (sprocketPathMemoryCache) sprocketPathMemoryCache.Remove(info.SprocketPath); } lock (GetLockObject(info.IdentifierString)) { try { SQLiteCommand cmd = new SQLiteCommand(sqlInsert, conn); info.AddSqlParameters(cmd); cmd.ExecuteNonQuery(); if (stream.CanSeek) { stream.Seek(0, SeekOrigin.Begin); } FileInfo file = info.File; if (!file.Directory.Exists) { file.Directory.Create(); } DeleteFile(info.File); using (FileStream fs = File.Create(info.PhysicalPath)) { byte[] buffer = new byte[100000]; int n; do { n = stream.Read(buffer, 0, buffer.Length); if (n == 0) { break; } fs.Write(buffer, 0, n); } while (true); fs.Flush(); fs.Close(); } lock (memoryCache) memoryCache[info.IdentifierString] = info; file.Refresh(); diskSpace += file.Length; } catch (IOException) { return; } catch { DeleteFile(info.File); throw; } finally { ReleaseLockObject(info.IdentifierString); } } }