/// <summary> /// Instantiates a ShardedDiskStorage that will use the directory to /// save a map between keys and files. The version is very important /// if clients change the format saved in those files. /// ShardedDiskStorage will assure that files saved with different /// version will be never used and eventually removed. /// </summary> /// <param name="rootDirectory"> /// Root directory to create all content under. /// </param> /// <param name="version"> /// Version of the format used in the files. If passed a different /// version files saved with the previous value will not be read and /// will be purged eventually. /// </param> /// <param name="cacheErrorLogger">Logger for various events.</param> /// <param name="clock">Optional parameter for unit test.</param> public DefaultDiskStorage( FileSystemInfo rootDirectory, int version, ICacheErrorLogger cacheErrorLogger, Clock clock = null) { Preconditions.CheckNotNull(rootDirectory); _rootDirectory = rootDirectory; // Phong Cao: Checking external storage requires 'Removable devices' // permission in the app manifest, skip it for now. _isExternal = false; // CheckExternal(rootDirectory, cacheErrorLogger); // _versionDirectory's name identifies: // - the cache structure's version (sharded) // - the content's version (version value) // if structure changes, prefix will change... if content changes version // will be different the ideal would be asking _sharding its name, but it's // created receiving the directory _versionDirectory = new DirectoryInfo(Path.Combine(_rootDirectory.FullName, GetVersionSubdirectoryName(version))); _cacheErrorLogger = cacheErrorLogger; RecreateDirectoryIfVersionChanges(); _clock = clock ?? SystemClock.Get(); }
public DiskStorageWithReadFailures( int version, ISupplier <FileSystemInfo> baseDirectoryPathSupplier, string baseDirectoryName, ICacheErrorLogger cacheErrorLogger, Clock clock = null) : base( version, baseDirectoryPathSupplier, baseDirectoryName, cacheErrorLogger, clock) { }
/// <summary> /// Instantiates the <see cref="DynamicDefaultDiskStorage"/>. /// </summary> public DynamicDefaultDiskStorage( int version, ISupplier <FileSystemInfo> baseDirectoryPathSupplier, string baseDirectoryName, ICacheErrorLogger cacheErrorLogger, Clock clock = null) { _version = version; _cacheErrorLogger = cacheErrorLogger; _baseDirectoryPathSupplier = baseDirectoryPathSupplier; _baseDirectoryName = baseDirectoryName; _currentState = new State(null, null); _clock = clock; }
internal DiskCacheConfig(Builder builder) { _version = builder._version; _baseDirectoryName = Preconditions.CheckNotNull(builder._baseDirectoryName); _baseDirectoryPathSupplier = Preconditions.CheckNotNull(builder._baseDirectoryPathSupplier); _defaultSizeLimit = builder._maxCacheSize; _lowDiskSpaceSizeLimit = builder._maxCacheSizeOnLowDiskSpace; _minimumSizeLimit = builder._maxCacheSizeOnVeryLowDiskSpace; _entryEvictionComparatorSupplier = Preconditions.CheckNotNull(builder._entryEvictionComparatorSupplier); _cacheErrorLogger = builder._cacheErrorLogger ?? NoOpCacheErrorLogger.Instance; _cacheEventListener = builder._cacheEventListener ?? NoOpCacheEventListener.Instance; _diskTrimmableRegistry = builder._diskTrimmableRegistry ?? NoOpDiskTrimmableRegistry.Instance; _indexPopulateAtStartupEnabled = builder._indexPopulateAtStartupEnabled; }
private static bool CheckExternal(FileSystemInfo directory, ICacheErrorLogger cacheErrorLogger) { try { // Get the logical root folder for all external storage devices. StorageFolder externalDevices = KnownFolders.RemovableDevices; if (directory.FullName.Contains(externalDevices.Path)) { return(true); } } catch (Exception) { cacheErrorLogger.LogError( CacheErrorCategory.OTHER, typeof(DefaultDiskStorage), "Failed to read folder to check if external: " + directory.Name); } return(false); }
/// <summary> /// The logger that is used to log errors made by the cache. /// </summary> public Builder SetCacheErrorLogger(ICacheErrorLogger cacheErrorLogger) { _cacheErrorLogger = cacheErrorLogger; return(this); }
/// <summary> /// Instantiates the <see cref="DiskStorageCache"/>. /// </summary> public DiskStorageCache( IDiskStorage diskStorage, IEntryEvictionComparatorSupplier entryEvictionComparatorSupplier, Params parameters, ICacheEventListener cacheEventListener, ICacheErrorLogger cacheErrorLogger, IDiskTrimmableRegistry diskTrimmableRegistry, bool indexPopulateAtStartupEnabled, Clock clock = null) { _lowDiskSpaceCacheSizeLimit = parameters.LowDiskSpaceCacheSizeLimit; _defaultCacheSizeLimit = parameters.DefaultCacheSizeLimit; _cacheSizeLimit = parameters.DefaultCacheSizeLimit; _statFsHelper = StatFsHelper.Instance; _storage = diskStorage; _entryEvictionComparatorSupplier = entryEvictionComparatorSupplier; _cacheSizeLastUpdateTime = default(DateTime); _cacheEventListener = cacheEventListener; _cacheSizeLimitMinimum = parameters.CacheSizeLimitMinimum; _cacheErrorLogger = cacheErrorLogger; _cacheStats = new CacheStats(); if (diskTrimmableRegistry != null) { diskTrimmableRegistry.RegisterDiskTrimmable(this); } _clock = clock ?? SystemClock.Get(); _indexPopulateAtStartupEnabled = indexPopulateAtStartupEnabled; _resourceIndex = new HashSet <string>(); if (_indexPopulateAtStartupEnabled) { Task.Run(() => { try { lock (_lock) { MaybeUpdateFileCacheSize(); } } finally { _countdownEvent.Signal(); } }); } else { _countdownEvent.Reset(0); } }