Exemplo n.º 1
0
        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);
                }
            }
        }
Exemplo n.º 2
0
        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);
        }
Exemplo n.º 3
0
        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);
                }
            }
        }
Exemplo n.º 4
0
 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();
     }
 }
Exemplo n.º 5
0
        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");
        }
Exemplo n.º 6
0
        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);
        }
Exemplo n.º 7
0
        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);
        }
Exemplo n.º 8
0
        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);
        }
Exemplo n.º 9
0
        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);
        }
Exemplo n.º 10
0
        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);
                }
            }
        }
Exemplo n.º 11
0
 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);
         }
     }
 }
Exemplo n.º 12
0
 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);
         }
     }
 }
Exemplo n.º 13
0
        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);
        }
Exemplo n.º 14
0
        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();
            }
        }
Exemplo n.º 15
0
        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);
                }
            }
        }
Exemplo n.º 16
0
        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);
                }
            }
        }
Exemplo n.º 17
0
 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);
 }
Exemplo n.º 18
0
 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;
 }
Exemplo n.º 19
0
 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;
 }
Exemplo n.º 20
0
 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);
 }
Exemplo n.º 21
0
 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;
 }
Exemplo n.º 22
0
 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");
 }
Exemplo n.º 23
0
        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);
                }
            }
        }