Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
AttachmentCacheService.cs
5
7{
11 [Singleton]
12 internal sealed class AttachmentCacheService : LoadableService, IAttachmentCacheService
13 {
14 private static readonly TimeSpan defaultExpiry = TimeSpan.FromHours(24);
15 private readonly FileCacheManager cacheManager;
16
20 public AttachmentCacheService()
21 {
22 this.cacheManager = new FileCacheManager("Attachments", defaultExpiry);
23 }
24
26 public override async Task Load(bool IsResuming, CancellationToken CancellationToken)
27 {
28 if (this.BeginLoad(IsResuming, CancellationToken))
29 {
30 try
31 {
32 if (!IsResuming)
33 await this.cacheManager.EvictOldEntries();
34
35 this.EndLoad(true);
36 }
37 catch (Exception)
38 {
39 this.EndLoad(false);
40 throw;
41 }
42 }
43 }
44
46 public Task<(byte[]? Data, string ContentType)> TryGet(string Url)
47 {
48 return this.cacheManager.TryGet(Url);
49 }
50
52 public Task Add(string Url, string ParentId, bool Permanent, byte[] Data, string ContentType)
53 {
54 return this.cacheManager.AddOrUpdate(Url, ParentId, Permanent, Data, ContentType);
55 }
56
58 public Task<bool> Remove(string Url)
59 {
60 return this.cacheManager.Remove(Url);
61 }
62
64 public async Task<bool> RemoveAttachments(Attachment[]? Attachments)
65 {
66 if (Attachments is null)
67 return false;
68
69 bool Removed = false;
70 foreach (Attachment Attachment in Attachments)
71 {
72 if (Attachment.ContentType.StartsWith("image/", StringComparison.OrdinalIgnoreCase))
73 {
74 bool EntryRemoved = await this.cacheManager.Remove(Attachment.Url);
75 if (EntryRemoved)
76 Removed = true;
77 }
78 }
79
80 return Removed;
81 }
82
84 public Task MakeTemporary(string ParentId)
85 {
86 return this.cacheManager.MakeTemporary(ParentId);
87 }
88
90 public Task MakePermanent(string ParentId)
91 {
92 return this.cacheManager.MakePermanent(ParentId);
93 }
94 }
95}
bool BeginLoad(bool IsResuming, CancellationToken CancellationToken)
Sets the IsLoading flag if the service isn't already loading.
void EndLoad(bool isLoaded)
Sets the IsLoading and IsLoaded flags and fires an event representing the current load state of the s...
bool IsResuming
If App is resuming service.
Contains a reference to an attachment assigned to a legal object.
Definition: Attachment.cs:10
string ContentType
Internet Content Type of binary attachment.
Definition: Attachment.cs:48
string Url
URL to retrieve attachment, if provided.
Definition: Attachment.cs:66