Example #1
0
        public CleanupManager(ILoggerProvider lp, CustomDiskCache cache, CleanupStrategy cs)
        {
            this.cache = cache;
            this.cs    = cs;
            this.lp    = lp;
            queue      = new CleanupQueue();
            //Called each request
            cache.CacheResultReturned += delegate(CustomDiskCache sender, CacheResult r) {
                if (r.Result == CacheQueryResult.Miss)
                {
                    this.AddedFile(r.RelativePath); //It was either updated or added.
                }
                else
                {
                    this.BeLazy();
                }
            };
            //Called when the filesystem changes unexpectedly.
            cache.Index.FileDisappeared += delegate(string relativePath, string physicalPath) {
                if (lp.Logger != null)
                {
                    lp.Logger.Warn("File disappeared from the cache unexpectedly - reindexing entire cache. File name: {0}", relativePath);
                }
                //Stop everything ASAP and start a brand new cleaning run.
                queue.ReplaceWith(new CleanupWorkItem(CleanupWorkItem.Kind.CleanFolderRecursive, "", cache.PhysicalCachePath));
                worker.MayHaveWork();
            };

            worker = new CleanupWorker(lp, cs, queue, cache);
        }
Example #2
0
 /// <summary>
 /// Creates and starts a thread that consumes the queue, pausing until notified when 'queue' empties.
 /// </summary>
 /// <param name="cs"></param>
 /// <param name="queue"></param>
 /// <param name="cache"></param>
 public CleanupWorker(ILoggerProvider lp, CleanupStrategy cs, CleanupQueue queue, CustomDiskCache cache) : base("DiskCache-CleanupWorker")
 {
     this.cs        = cs;
     this.queue     = queue;
     this.cache     = cache;
     this.lp        = lp;
     t              = new Thread(main);
     t.IsBackground = true;
     t.Start();
 }
Example #3
0
 /// <summary>
 /// Creates and starts a thread that consumes the queue, pausing until notified when 'queue' empties.
 /// </summary>
 /// <param name="cs"></param>
 /// <param name="queue"></param>
 /// <param name="cache"></param>
 public CleanupWorker(ILoggerProvider lp, CleanupStrategy cs, CleanupQueue queue, CustomDiskCache cache)
     : base("DiskCache-CleanupWorker")
 {
     this.cs = cs;
     this.queue = queue;
     this.cache = cache;
     this.lp = lp;
     t = new Thread(main);
     t.IsBackground = true;
     t.Start();
 }
Example #4
0
        /// <summary>
        /// Attempts to start the DiskCache using the current settings. Returns true if successful or if already started. Returns false on a configuration error.
        /// Called by Install()
        /// </summary>
        public bool Start()
        {
            if (!IsConfigurationValid())
            {
                return(false);
            }
            lock (_startSync) {
                if (_started)
                {
                    return(true);
                }
                if (!IsConfigurationValid())
                {
                    return(false);
                }

                //Init the writer.
                writer = new WebConfigWriter(this, PhysicalCacheDir);
                //Init the inner cache
                if (!AsyncModuleMode)
                {
                    cache = new CustomDiskCache(this, PhysicalCacheDir, Subfolders, AsyncBufferSize);
                }
                if (AsyncModuleMode)
                {
                    asyncCache = new AsyncCustomDiskCache(this, PhysicalCacheDir, Subfolders, AsyncBufferSize);
                }

                //Init the cleanup strategy
                if (AutoClean && cleanupStrategy == null)
                {
                    cleanupStrategy = new CleanupStrategy();                                       //Default settings if null
                }
                //Init the cleanup worker
                if (AutoClean)
                {
                    cleaner = new CleanupManager(this, AsyncModuleMode ? (ICleanableCache)asyncCache : (ICleanableCache)cache, cleanupStrategy);
                }
                //If we're running with subfolders, enqueue the cache root for cleanup (after the 5 minute delay)
                //so we don't eternally 'skip' files in the root or in other unused subfolders (since only 'accessed' subfolders are ever cleaned ).
                if (cleaner != null)
                {
                    cleaner.CleanAll();
                }

                if (log != null)
                {
                    log.Info("DiskCache started successfully.");
                }
                //Started successfully
                _started = true;
                return(true);
            }
        }
Example #5
0
        public CleanupManager(ILoggerProvider lp, CustomDiskCache cache, CleanupStrategy cs)
        {
            this.cache = cache;
            this.cs = cs;
            this.lp = lp;
            queue = new CleanupQueue();
            //Called each request
            cache.CacheResultReturned += delegate(CustomDiskCache sender, CacheResult r) {
                if (r.Result == CacheQueryResult.Miss)
                    this.AddedFile(r.RelativePath); //It was either updated or added.
                else
                    this.BeLazy();
            };
            //Called when the filesystem changes unexpectedly.
            cache.Index.FileDisappeared += delegate(string relativePath, string physicalPath) {
                if (lp.Logger != null) lp.Logger.Warn("File disappeared from the cache unexpectedly - reindexing entire cache. File name: {0}", relativePath);
                //Stop everything ASAP and start a brand new cleaning run.
                queue.ReplaceWith(new CleanupWorkItem(CleanupWorkItem.Kind.CleanFolderRecursive, "", cache.PhysicalCachePath));
                worker.MayHaveWork();
            };

            worker = new CleanupWorker(lp, cs,queue,cache);
        }
Example #6
0
        /// <summary>
        /// Attempts to start the DiskCache using the current settings. Returns true if succesful or if already started. Returns false on a configuration error.
        /// Called by Install()
        /// </summary>
        public bool Start()
        {
            if (!IsConfigurationValid()) return false;
            lock (_startSync) {
                if (_started) return true;
                if (!IsConfigurationValid()) return false;

                //Init the writer.
                writer = new WebConfigWriter(this,PhysicalCacheDir);
                //Init the inner cache
                cache = new CustomDiskCache(this, PhysicalCacheDir, Subfolders, HashModifiedDate,AsyncBufferSize);
                //Init the cleanup strategy
                if (AutoClean && cleanupStrategy == null) cleanupStrategy = new CleanupStrategy(); //Default settings if null
                //Init the cleanup worker
                if (AutoClean) cleaner = new CleanupManager(this, cache, cleanupStrategy);
                //If we're running with subfolders, enqueue the cache root for cleanup (after the 5 minute delay)
                //so we don't eternally 'skip' files in the root or in other unused subfolders (since only 'accessed' subfolders are ever cleaned ).
                if (cleaner != null) cleaner.CleanAll();

                if (log != null) log.Info("DiskCache started successfully.");
                //Started successfully
                _started = true;
                return true;
            }
        }
Example #7
0
        /// <summary>
        /// Attempts to start the DiskCache using the current settings. Returns true if succesful or if already started. Returns false on a configuration error.
        /// Called by Install()
        /// </summary>
        public bool Start()
        {
            if (!IsConfigurationValid()) return false;
            lock (_startSync)
            {
                if (_started) return true;
                if (!IsConfigurationValid()) return false;

                //Init the writer.
                writer = new WebConfigWriter(this, PhysicalCacheDir);
                //Init the inner cache
                cache = new CustomDiskCache(this, PhysicalCacheDir, 512, true, 1024 * 1024 * 30); //30MB
                //Init the cleanup strategy
                var cleanupStrategy = new CleanupStrategy(); //Default settings if null
                cleanupStrategy.TargetItemsPerFolder = 50;
                //Init the cleanup worker
                cleaner = new CleanupManager(this, cache, cleanupStrategy);
                //If we're running with subfolders, enqueue the cache root for cleanup (after the 5 minute delay)
                //so we don't eternally 'skip' files in the root or in other unused subfolders (since only 'accessed' subfolders are ever cleaned ).
                if (cleaner != null) cleaner.CleanAll();

                //Started successfully
                _started = true;
                return true;
            }
        }