Class FileCacheService
Service for caching files locally so that they do not need to be downloaded repeatedly
Implements
Namespace: i5.Toolkit.Core.Caching
Assembly: cs.temp.dll.dll
Syntax
public class FileCacheService : IService, IFileCache
Constructors
FileCacheService(Boolean, Boolean, String, Single, IDirectoryAccessor)
Creates a new file cache service
Declaration
public FileCacheService(bool sessionPersistence = false, bool useSafeMode = true, string cacheLocationOverride = null, float daysValid = 365F, IDirectoryAccessor directoryAccessor = null)
Parameters
Type | Name | Description |
---|---|---|
Boolean | sessionPersistence | If true, the tracked cache is stored and recovered in future application startups |
Boolean | useSafeMode | If true, files are hashed, meaning that cached content cannot be switched out by external influences |
String | cacheLocationOverride | If set, the cache will be stored in the given place instead of the default path |
Single | daysValid | Number of days that an entry in the cache should stay valid before requiring a re-download |
IDirectoryAccessor | directoryAccessor |
Fields
persistentCacheFileName
The name under which the cache registry is stored
Declaration
public const string persistentCacheFileName = "i5cache.json"
Field Value
Type | Description |
---|---|
String |
Properties
CacheLocation
Points to the folder where the cached files are stored and where the cache registry can be found
Declaration
public string CacheLocation { get; }
Property Value
Type | Description |
---|---|
String |
ContentLoader
Module that should be used for fetching the file's content
Declaration
public IContentLoader<string> ContentLoader { get; set; }
Property Value
Type | Description |
---|---|
IContentLoader<String> |
DaysValid
The amount of days for which a cached file is valid
Declaration
public float DaysValid { get; }
Property Value
Type | Description |
---|---|
Single |
DirectoryAccessor
Module should should be used for accessing directories
Declaration
public IDirectoryAccessor DirectoryAccessor { get; set; }
Property Value
Type | Description |
---|---|
IDirectoryAccessor |
FileAccessor
Module that should be used for accessing files
Declaration
public IFileAccessor FileAccessor { get; set; }
Property Value
Type | Description |
---|---|
IFileAccessor |
FileCount
Number of files in which are tracked in the cache
Declaration
public int FileCount { get; }
Property Value
Type | Description |
---|---|
Int32 |
FileHasher
Module for generating hash ids for files
Declaration
public IFileHasher FileHasher { get; set; }
Property Value
Type | Description |
---|---|
IFileHasher |
SessionPersistence
If true, cache entries are stored persistently and read again on application startup
Declaration
public bool SessionPersistence { get; }
Property Value
Type | Description |
---|---|
Boolean |
UseSafeMode
If true, cached files are hashed so that they cannot be switched out
Declaration
public bool UseSafeMode { get; }
Property Value
Type | Description |
---|---|
Boolean |
Methods
AddOrUpdateInCacheAsync(String)
Caches a file and registers it in the cache. If the file is already cached, this method forces the download of the file again and updates the cache entry
Declaration
public Task<string> AddOrUpdateInCacheAsync(string path)
Parameters
Type | Name | Description |
---|---|---|
String | path | The path to the remote file resource, most likely an URL |
Returns
Type | Description |
---|---|
Task<String> | Returns the path in local storage where the file was stored |
Cleanup()
Called when the service is shut down
Declaration
public void Cleanup()
GetCachedFileLocation(String)
Returns the local file path where the file of the given URL path is cached If the file is not cached, it will return an empty string
Declaration
public string GetCachedFileLocation(string path)
Parameters
Type | Name | Description |
---|---|---|
String | path | The path to the remote file, probably an URL |
Returns
Type | Description |
---|---|
String | Returns the path in local storage to the cached version; if no version is cached, an empty string is returned |
Initialize(IServiceManager)
Called when the service is registered at the service manager
Declaration
public void Initialize(IServiceManager owner)
Parameters
Type | Name | Description |
---|---|---|
IServiceManager | owner | The service manager which now owns the service |
IsFileInCache(String)
Checks whether the remote file at the given URL path is already cached
Declaration
public bool IsFileInCache(string path)
Parameters
Type | Name | Description |
---|---|---|
String | path | The path to the remote file, probably an URL |
Returns
Type | Description |
---|---|
Boolean | Returns whether the file is already cached |