private void InitializeCache(string cacheFilePath, int cacheSizeLimit) { this._cacheSizeLimit = cacheSizeLimit; try { using (var fileStream = new FileStream(cacheFilePath, FileMode.OpenOrCreate)) { InitializeCacheHelper(fileStream); } } catch { using (var fileStream = new FileStream(defaultCacheFileFath, FileMode.OpenOrCreate)) { InitializeCacheHelper(fileStream); } } //In case the deserialized OrderedDictionary had more contents than the limit, we need to shrink it to make its size equal to the limit if (this._cacheSizeLimit != CACHE_SIZE_NO_LIMIT && this._cache.Keys.Count > this._cacheSizeLimit) { int difference = this._cache.Keys.Count - this._cacheSizeLimit; for (int i = 0; i < difference; i++) { _cache.RemoveAt(0); } } }
private void initializeCache(string cacheFilePath, int cacheSizeLimit) { this.cacheSizeLimit = cacheSizeLimit; using (FileStream fileStream = new FileStream(cacheFilePath, FileMode.Open)) { IFormatter bf = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); this.cache = (System.Collections.Specialized.OrderedDictionary)bf.Deserialize(fileStream); fileStream.Close(); } //In case the deserialized OrderedDictionary had more contents than the limit, we need to shrink it to make its size equal to the limit if (this.cacheSizeLimit != CACHE_SIZE_NO_LIMIT && this.cache.Keys.Count > this.cacheSizeLimit) { int difference = this.cache.Keys.Count - this.cacheSizeLimit; for (int i = 0; i < difference; i++) { cache.RemoveAt(0); } } }