Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
CachedEventHandler.cs
1using System;
3using System.Threading;
4using System.Threading.Tasks;
9
11{
15 public abstract class CachedEventHandler : EventHandler
16 {
17 private static readonly Cache<string, Dictionary<string, CachedEventHandler>> eventHandlers = new Cache<string, Dictionary<string, CachedEventHandler>>(int.MaxValue, TimeSpan.FromDays(1), TimeSpan.FromHours(8));
18 private static readonly SemaphoreSlim synchObj = new SemaphoreSlim(1);
19
24 : base()
25 {
26 }
27
37 : base(Arguments, EventIndex)
38 {
39 }
40
44 public async Task AddToCache()
45 {
46 await synchObj.WaitAsync();
47 try
48 {
49 if (!eventHandlers.TryGetValue(this.EventType, out Dictionary<string, CachedEventHandler> Handlers))
50 {
51 Handlers = await LoadEventHandlers(this.EventType);
52
53 if (!Handlers.ContainsKey(this.ObjectId))
54 Handlers[this.ObjectId] = this;
55
56 eventHandlers[this.EventType] = Handlers;
57 }
58 else if (Handlers is null)
59 {
60 Handlers = new Dictionary<string, CachedEventHandler>()
61 {
62 { this.ObjectId, this }
63 };
64
65 eventHandlers[this.EventType] = Handlers;
66 }
67 else
68 Handlers[this.ObjectId] = this;
69 }
70 finally
71 {
72 synchObj.Release();
73 }
74 }
75
79 public async Task RemoveFromCache()
80 {
81 await synchObj.WaitAsync();
82 try
83 {
84 if (eventHandlers.TryGetValue(this.EventType, out Dictionary<string, CachedEventHandler> Handlers) &&
85 !(Handlers is null) &&
86 Handlers.Remove(this.ObjectId) &&
87 Handlers.Count == 0)
88 {
89 eventHandlers[this.EventType] = null;
90 }
91 }
92 finally
93 {
94 synchObj.Release();
95 }
96 }
97
103 public static async Task<CachedEventHandler[]> GetEventHandlers(string EventType)
104 {
105 await synchObj.WaitAsync();
106 try
107 {
108 if (!eventHandlers.TryGetValue(EventType, out Dictionary<string, CachedEventHandler> Handlers))
109 {
110 Dictionary<string, CachedEventHandler> List = await LoadEventHandlers(EventType);
111 Handlers = List.Count == 0 ? null : List;
112 eventHandlers[EventType] = Handlers;
113 }
114
115 if (Handlers is null)
116 return null;
117
118 int c = Handlers.Count;
119 CachedEventHandler[] Result = new CachedEventHandler[c];
120 Handlers.Values.CopyTo(Result, 0);
121
122 return Result;
123 }
124 finally
125 {
126 synchObj.Release();
127 }
128 }
129
130 private static async Task<Dictionary<string, CachedEventHandler>> LoadEventHandlers(string EventType)
131 {
132 IEnumerable<CachedEventHandler> Handlers = await Database.Find<CachedEventHandler>(new FilterFieldEqualTo("EventType", EventType));
133 Dictionary<string, CachedEventHandler> Result = new Dictionary<string, CachedEventHandler>();
134
135 foreach (CachedEventHandler Handler in Handlers)
136 Result[Handler.ObjectId] = Handler;
137
138 return Result;
139 }
140
141 }
142}
Static interface for database persistence. In order to work, a database provider has to be assigned t...
Definition: Database.cs:21
static Task< IEnumerable< object > > Find(string Collection, params string[] SortOrder)
Finds objects in a given collection.
Definition: Database.cs:238
This filter selects objects that have a named field equal to a given value.
Implements an in-memory cache.
Definition: Cache.cs:17
CachedEventHandler(EvaluationArguments Arguments, int EventIndex)
Event handler for entry events.
async Task AddToCache()
Removes the event handler from the cache.
static async Task< CachedEventHandler[]> GetEventHandlers(string EventType)
Gets registered event handlers of a given event type.
CachedEventHandler()
Abstract base class for cached event handlers.
async Task RemoveFromCache()
Removes the event handler from the cache.
Abstract base class for persisted state-machine event handlers.
Definition: EventHandler.cs:18
int EventIndex
Zero-based index of event handler in state.
Definition: EventHandler.cs:60
Contains information required for evaluating script in a state-machine.