public bool FileExists(string filename)
        {
            // get our lock
            lock ( locker )
            {
                // first make sure it's in our cahce. If not,
                // we want to consider it not existing. It was orphaned.
                // (Maybe we crashed before we could save?)
                if (CacheMap.ContainsKey(filename))
                {
                    // validate it does exist on disk
                    if (File.Exists(CachePath + "/" + filename))
                    {
                        // and return
                        return(true);
                    }
                    else
                    {
                        // otherwise, it didn't exist on disk, but
                        // was somehow in our cache. Get rid of it.
                        CacheMap.Remove(filename);
                    }
                }

                return(false);
            }
        }
        public bool SaveFile(object buffer, string filename, TimeSpan?expirationTime = null)
        {
            // sync point so we don't read multiple times at once.
            // Note: If we ever need to support multiple threads reading from the cache at once, we'll need
            // a table to hash multiple locks per filename. This serializes all file loads across all threads.
            lock ( locker )
            {
                bool result = false;

                try
                {
                    // attempt to write the file out to disk
                    using (FileStream writer = new FileStream(CachePath + "/" + filename, FileMode.Create))
                    {
                        BinaryFormatter formatter = new BinaryFormatter( );
                        StreamWriter    mapStream = new StreamWriter(writer);
                        formatter.Serialize(mapStream.BaseStream, buffer);

                        mapStream.Dispose( );

                        // store in our cachemap the filename and when we wrote it.
                        // Don't ever add it twice. If it exists, remove it
                        if (CacheMap.Contains(filename))
                        {
                            CacheMap.Remove(filename);
                            Rock.Mobile.Util.Debug.WriteLine(string.Format("{0} is already cached. Updating time to {1}", filename, DateTime.Now));
                        }
                        else
                        {
                            Rock.Mobile.Util.Debug.WriteLine(string.Format("Adding {0} to cache. Time {1}", filename, DateTime.Now));
                        }

                        if (expirationTime.HasValue == false)
                        {
                            expirationTime = CacheFileDefaultExpiration;
                        }

                        // and now it'll be added a second time.
                        CacheMap.Add(filename, DateTime.Now + expirationTime.Value);

                        // if an exception occurs we won't set result to true
                        result = true;

                        writer.Close( );
                        writer.Dispose( );
                    }
                }
                catch (Exception e)
                {
                    Rock.Mobile.Util.Debug.WriteLine(e.Message);
                }

                // return the result
                return(result);
            }
        }
 public void RemoveFile(string filename)
 {
     // take our lock
     lock ( locker )
     {
         // delete the entry
         File.Delete(CachePath + "/" + filename);
         CacheMap.Remove(filename);
     }
 }
        /// <summary>
        /// Scans the cache hashtable and removes any entries that are expired. Additionally, it deletes the file
        /// from the cache folder.
        /// CAUTION: If you pass true, all files will automatically be erased
        /// </summary>
        public void CleanUp(bool forceEraseAll = false)
        {
            lock ( locker )
            {
                Rock.Mobile.Util.Debug.WriteLine("Running cleanup");

                List <DictionaryEntry> expiredItems = new List <DictionaryEntry>( );

                // scan our cache and remove anything older than the expiration time.
                foreach (DictionaryEntry entry in CacheMap)
                {
                    DateTime entryValue = (DateTime)entry.Value;

                    // if it's older than our expiration time, delete it
                    TimeSpan deltaTime = (DateTime.Now - entryValue);
                    if (DateTime.Now >= entryValue || forceEraseAll == true)
                    {
                        // delete the entry
                        File.Delete(CachePath + "/" + entry.Key);

                        expiredItems.Add(entry);

                        Rock.Mobile.Util.Debug.WriteLine(string.Format("{0} expired. Age: {1} minutes past expiration.", (string)entry.Key, deltaTime.TotalMinutes));
                    }
                    else
                    {
                        Rock.Mobile.Util.Debug.WriteLine(string.Format("{0} still fresh NOT REMOVING.", (string)entry.Key));
                    }
                }

                // remove all entries that have been expired
                foreach (DictionaryEntry entry in expiredItems)
                {
                    CacheMap.Remove(entry.Key);
                }

                Rock.Mobile.Util.Debug.WriteLine("Cleanup complete");
            }
        }