예제 #1
0
        /// <summary>
        /// Delete the oldest items in the cache to shrink the chache by the
        /// specified amount (in bytes).
        /// </summary>
        /// <returns>The amount of data that was actually removed</returns>
        private long DeleteOldestFiles(long amount, string regionName = null)
        {
            // Verify that we actually need to shrink
            if (amount <= 0)
            {
                return(0);
            }

            //Heap of all CacheReferences
            PriortyQueue <CacheItemReference> cacheReferences = new PriortyQueue <CacheItemReference>();

            //build a heap of all files in cache region
            foreach (string key in GetKeys(regionName))
            {
                try
                {
                    //build item reference
                    string             cachePath  = GetCachePath(key, regionName);
                    string             policyPath = GetPolicyPath(key, regionName);
                    CacheItemReference ci         = new CacheItemReference(key, cachePath, policyPath);
                    cacheReferences.Enqueue(ci);
                }
                catch (FileNotFoundException)
                {
                }
            }

            //remove cache items until size requirement is met
            long removedBytes = 0;

            while (removedBytes < amount && cacheReferences.GetSize() > 0)
            {
                //remove oldest item
                CacheItemReference oldest = cacheReferences.Dequeue();
                removedBytes += oldest.Length;
                Remove(oldest.Key, regionName);
            }
            return(removedBytes);
        }
예제 #2
0
        /// <summary>
        /// Loop through the cache and delete all expired files
        /// </summary>
        /// <returns>The amount removed (in bytes)</returns>
        public long CleanCache(string regionName = null)
        {
            long removed = 0;

            //lock down other treads from trying to shrink or clean
            using (FileStream cLock = GetCleaningLock())
            {
                if (cLock == null)
                {
                    return(0);
                }

                foreach (string key in GetKeys(regionName))
                {
                    CacheItemPolicy policy = GetPolicy(key, regionName);
                    if (policy.AbsoluteExpiration < DateTime.Now)
                    {
                        try
                        {
                            string             cachePath  = GetCachePath(key, regionName);
                            string             policyPath = GetPolicyPath(key, regionName);
                            CacheItemReference ci         = new CacheItemReference(key, cachePath, policyPath);
                            Remove(key, regionName); // CT note: Remove will update CurrentCacheSize
                            removed += ci.Length;
                        }
                        catch (Exception) // skip if the file cannot be accessed
                        { }
                    }
                }

                // mark that we've cleaned the cache
                WriteSysFile(LastCleanedDateFile, DateTime.Now);

                // unlock
                cLock.Close();
            }

            return(removed);
        }