Example #1
0
        /// <summary> Instantiates a new DocumentCache. </summary>
        /// <param name="cachePath"> Specifies the path to use to store cached items.  </param>
        public DocumentCache(string cachePath, int size)
        {
            // Prepare the folder
            _cachePath = cachePath;

            // Prepare the structures
            _identifiers = new IdentifierIndex();
            _cRC32s      = new Dictionary <string, uint>(size);
            _cache       = new FixedSizeCache <string, string>(size);

            LockDirectory();
            try
            {
                try
                {
                    DateTime indexFileTime = DateTime.MinValue;                         // Init as invalid
                    string   indexFileName = Path.Combine(_cachePath, IndexFileName);
                    string[] cacheFiles    = Directory.GetFiles(_cachePath, "*" + CacheFileExtension);

                    // Load the identifier index
                    if (File.Exists(indexFileName))
                    {
                        indexFileTime = File.GetLastWriteTimeUtc(indexFileName);
                        using (Stream stream = new FileStream(indexFileName, FileMode.Open, FileAccess.Read))
                        {
                            _identifiers.Load(stream);
                        }

                        // Initialize the CRC32s
                        string cRC32TableFileName = Path.Combine(_cachePath, RC32FileName);
                        if (File.Exists(cRC32TableFileName))
                        {
                            if (File.GetLastWriteTimeUtc(cRC32TableFileName) == indexFileTime)
                            {
                                using (FileStream stream = new FileStream(cRC32TableFileName, FileMode.Open, FileAccess.Read))
                                {
                                    StreamUtility.LoadDictionary(stream, _cRC32s, typeof(String), typeof(UInt32));
                                }
                            }
                            else
                            {
                                indexFileTime = DateTime.MinValue;                                      // CRC32 and Index file times doen't match.  Invalid index.
                            }
                        }
                        else
                        {
                            indexFileTime = DateTime.MinValue;                                  // No CRC32 table.  Invalid index.
                        }
                        // Verify that ther are no cache files newer than the index
                        foreach (string fileName in cacheFiles)
                        {
                            if (File.GetLastWriteTimeUtc(fileName) > indexFileTime)
                            {
                                indexFileTime = DateTime.MinValue;
                                break;
                            }
                        }
                    }

                    if (indexFileTime == DateTime.MinValue)                     // If cache invalid, delete the files and clear cache
                    {
                        // Delete any existing cache files in the cache directory and clear the Identifiers
                        foreach (string fileName in Directory.GetFiles(_cachePath, "*." + CacheFileExtension))
                        {
                            if (File.GetLastWriteTimeUtc(fileName) > indexFileTime)
                            {
                                File.Delete(fileName);
                            }
                        }
                        _identifiers.Clear();
                        _cRC32s.Clear();
                    }
                    // Don't bother initializing the fixed sized cache.  We'll let it be populated as requests come in.  As a result, if the cache size has been reduced, it
                    //  will not be effective until the new cache size limit has been reached.
                }
                catch
                {
                    //Prevent future problems by clearing the directory
                    EmptyCacheDirectory();
                    throw;
                }
            }
            catch
            {
                UnlockDirectory(false);
                throw;
            }
        }
Example #2
0
 /// <summary> Loads index entries from XML data contained within a stream. </summary>
 public void Load(Stream stream)
 {
     StreamUtility.LoadDictionary(stream, _table, typeof(String), typeof(String));
 }