5using System.Threading.Tasks;
13 internal class FileCacheManager
15 private readonly
string folderPath;
16 private readonly TimeSpan defaultExpiry;
18 private static int evictionPerformed = 0;
21 public FileCacheManager(
string folderName, TimeSpan defaultExpiry)
23 this.folderPath = Path.Combine(FileSystem.CacheDirectory, folderName);
24 this.defaultExpiry = defaultExpiry;
26 if (!Directory.Exists(
this.folderPath))
27 Directory.CreateDirectory(this.folderPath);
30 public async Task EvictOldEntries()
37 if (Interlocked.Exchange(ref evictionPerformed, 1) == 1)
55 public async Task<(
byte[]?, string)> TryGet(
string Key)
57 if (
string.IsNullOrEmpty(Key))
58 return (
null,
string.Empty);
66 if (Entry is not
null)
73 return (
null,
string.Empty);
77 if (DateTime.UtcNow >= Entry.
Expires)
78 return (
null,
string.Empty);
87 public async Task<(
byte[]? Data,
string ContentType,
bool IsExpired)> TryGetWithExpiry(
string Key)
89 if (
string.IsNullOrEmpty(Key))
90 return (
null,
string.Empty,
false);
96 return (
null,
string.Empty,
false);
103 return (
null,
string.Empty,
false);
106 bool Expired = DateTime.UtcNow >= Entry.Expires && Entry.Expires != DateTime.MaxValue;
110 public async Task AddOrUpdate(
string key,
string parentId,
bool permanent,
111 byte[] data,
string contentType)
113 if (
string.IsNullOrEmpty(key) || data is
null ||
string.IsNullOrEmpty(contentType))
116 DateTime Expires = permanent
118 : DateTime.UtcNow + this.defaultExpiry;
124 string Filename = Path.Combine(this.folderPath, $
"{Guid.NewGuid():N}.{Ext}");
126 await File.WriteAllBytesAsync(Filename, data);
128 if (Existing is
null)
133 ParentId = parentId ??
string.Empty,
134 LocalFileName = Filename,
144 Existing.LocalFileName = Filename;
145 Existing.ContentType = contentType;
146 Existing.ParentId = parentId ??
string.Empty;
147 Existing.Expires = Expires;
155 public async Task<bool> Remove(
string Key)
157 if (
string.IsNullOrEmpty(Key))
178 public async Task MakeTemporary(
string parentId)
183 if (Entry.
Expires == DateTime.MaxValue)
185 Entry.Expires = DateTime.UtcNow + this.defaultExpiry;
192 public async Task MakePermanent(
string parentId)
197 if (Entry.
Expires != DateTime.MaxValue)
199 Entry.Expires = DateTime.MaxValue;
206 public async Task<int> RemoveByParentId(
string parentId)
Contains information about a file in the local cache.
CaseInsensitiveString LocalFileName
Local file name.
CaseInsensitiveString ContentType
Content-Type
DateTime Expires
Timestamp of entry
Static class managing encoding and decoding of internet content.
static string GetFileExtension(string ContentType)
Gets the file extension of an item, given its content type. It uses the TryGetFileExtension to see if...
Static interface for database persistence. In order to work, a database provider has to be assigned t...
static Task< IEnumerable< object > > FindDelete(string Collection, params string[] SortOrder)
Finds objects in a given collection and deletes them in the same atomic operation.
static IDatabaseProvider Provider
Registered database provider.
static async Task Update(object Object)
Updates an object in the database.
static async Task Delete(object Object)
Deletes an object in the database.
static Task< IEnumerable< object > > Find(string Collection, params string[] SortOrder)
Finds objects in a given collection.
static async Task Insert(object Object)
Inserts an object into the default collection of the database.
This filter selects objects that have a named field equal to a given value.
This filter selects objects that have a named field lesser or equal to a given value.
Task Flush()
Persists any pending changes.
ContentType
DTLS Record content type.