Example #1
0
        /// <summary>
        /// Clean up the cache by deleting any cached items that have an expiration time
        /// older than the specified time.  This is a relatively expensive operation so should be
        /// called sparingly.
        /// </summary>
        /// <param name="maximumExpirationTime">The maximum expiration time to delete.  In other words, this call will delete
        /// any cache items who's expiration is older than this value.</param>
        /// <param name="complete">An Action that will be called when the cleanup operation has completed.</param>
        public void Cleanup(DateTime maximumExpirationTime, Action complete)
        {
            PriorityQueue.AddStorageWorkItem(
                () =>
            {
                int retries = 3;

                while (retries-- > 0)
                {
                    try {
                        // get the list of items.
                        //
                        var itemsToCleanup = from item in StoreProvider.GetItems()
                                             where item.ExpirationTime <= maximumExpirationTime
                                             select item;


                        // we snap the enumerable to an array to guard against any provider
                        // implementations that might have returned an enumerator that would be affected
                        // by the delete operation.
                        //
                        foreach (var item in itemsToCleanup.ToArray())
                        {
                            StoreProvider.Delete(item);
                        }
                    }
                    catch (Exception ex) {
                        Debug.WriteLine("Exception trying to clean up (attempt {0}): {1}", 3 - retries, ex);

                        // someetimes exceptions come out of the isostore stack trying to get the directory names,
                        // so we just wait a bit and try again.
                        //
                        Thread.Sleep(50);
                    }
                }

                // BUGBUG: What do to if this didn't ever actually complete succesfully.
                if (complete != null)
                {
                    PriorityQueue.AddUiWorkItem(() =>
                    {
                        complete();
                    }
                                                );
                }
            }
                );
        }