Пример #1
0
        /// <summary>
        /// Saves the current cache to disk with the given performanc setting.
        /// </summary>
        /// <param name="performanceSetting">Performance setting that should match the cache contents.</param>

        public void Save(EncodingOptions.Performance performanceSetting)
        {
            string path = GetFilePath(performanceSetting);

            LogClient.Instance.Logger.Info("Saving encoding cache [{0} items] to disk at {1}", cache.Count, path);

            try
            {
                FileInfo fInfo = new FileInfo(path);

                if (!fInfo.Directory.Exists)
                {
                    fInfo.Directory.Create();
                }

                using (FileStream fs = new FileStream(path, FileMode.Create))
                {
                    using (var deflate = new DeflateStream(fs, CompressionMode.Compress))
                    {
                        // Construct a BinaryFormatter and use it to serialize the data to the stream.
                        BinaryFormatter formatter = new BinaryFormatter();

                        formatter.Serialize(deflate, cache);
                    }
                }
            }
            catch (SerializationException e)
            {
                LogClient.Instance.Logger.Error("Serialization error: {0}", LogClient.GetAllExceptions(e));
            }
            catch (Exception ex)
            {
                LogClient.Instance.Logger.Error("Saving generic error: {0}", LogClient.GetAllExceptions(ex));
            }
        }
Пример #2
0
        /// <summary>
        /// Loads a cache from disk for the given performance setting.
        /// </summary>
        /// <param name="performanceSetting">Desired performance setting cache to load</param>

        public void Load(EncodingOptions.Performance performanceSetting)
        {
            if (cache != null && cache.Count > 0 && performanceSetting == currentPerformance)
            {
                LogClient.Instance.Logger.Info("Encoding cache already loaded [{0} items]", cache.Count);
                return;
            }

            string path = GetFilePath(performanceSetting);

            LogClient.Instance.Logger.Info("Loading encoding cache from disk at {0}", path);

            try
            {
                Clear();

                if (File.Exists(path))
                {
                    using (FileStream fs = new FileStream(path, FileMode.Open))
                    {
                        using (var deflate = new DeflateStream(fs, CompressionMode.Decompress))
                        {
                            BinaryFormatter formatter = new BinaryFormatter();

                            // Deserialize the cache from the file and
                            // assign the reference to the local variable.
                            if (deflate.CanSeek)
                            {
                                deflate.Seek(0, SeekOrigin.Begin);
                            }
                            cache = (Dictionary <string, EncodingCacheItem>)formatter.Deserialize(deflate);

                            // load up lrulist with all keys so they are insync
                            foreach (var key in cache.Keys)
                            {
                                lruList.AddLast(key);
                            }

                            // successful, so set now
                            currentPerformance = performanceSetting;

                            LogClient.Instance.Logger.Info("Encoding cache loaded successfully with {0} items", cache.Count);
                        }
                    }
                }
            }
            catch (SerializationException e)
            {
                LogClient.Instance.Logger.Error("Deserialization error: {0}", LogClient.GetAllExceptions(e));
            }
            catch (Exception ex)
            {
                LogClient.Instance.Logger.Error("Loading generic error: {0}", LogClient.GetAllExceptions(ex));
            }
        }
Пример #3
0
        /// <summary>
        /// Gets the file path for the given performance setting.
        /// </summary>
        /// <param name="performanceSetting">Desired performance setting</param>
        /// <returns></returns>

        private static string GetFilePath(EncodingOptions.Performance performanceSetting)
        {
            return(Path.Combine(ApplicationPaths.CacheDirectory, string.Format("encodings_{0}.cache", performanceSetting)));
        }