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;
2using System.Collections.Generic;
3using System.Linq;
4using System.Threading;
5using System.Threading.Tasks;
6using Waher.Events;
11
13{
17 public abstract class CachedEventHandler : EventHandler
18 {
19 private static readonly Cache<string, Dictionary<string, CachedEventHandler>> eventHandlers = new Cache<string, Dictionary<string, CachedEventHandler>>(int.MaxValue, TimeSpan.FromDays(1), TimeSpan.FromHours(8));
20 private static readonly SemaphoreSlim synchObj = new SemaphoreSlim(1);
21
26 : base()
27 {
28 }
29
39 : base(Arguments, EventIndex)
40 {
41 }
42
46 public async Task AddToCache()
47 {
48 await synchObj.WaitAsync();
49 try
50 {
51 if (!eventHandlers.TryGetValue(this.EventType, out Dictionary<string, CachedEventHandler> Handlers))
52 {
53 Handlers = await LoadEventHandlers(this.EventType);
54
55 if (!Handlers.ContainsKey(this.ObjectId))
56 Handlers[this.ObjectId] = this;
57
58 eventHandlers[this.EventType] = Handlers;
59 }
60 else if (Handlers is null)
61 {
62 Handlers = new Dictionary<string, CachedEventHandler>()
63 {
64 { this.ObjectId, this }
65 };
66
67 eventHandlers[this.EventType] = Handlers;
68 }
69 else
70 Handlers[this.ObjectId] = this;
71 }
72 finally
73 {
74 synchObj.Release();
75 }
76 }
77
81 public async Task RemoveFromCache()
82 {
83 await synchObj.WaitAsync();
84 try
85 {
86 if (eventHandlers.TryGetValue(this.EventType, out Dictionary<string, CachedEventHandler> Handlers) &&
87 !(Handlers is null) &&
88 Handlers.Remove(this.ObjectId) &&
89 Handlers.Count == 0)
90 {
91 eventHandlers[this.EventType] = null;
92 }
93 }
94 finally
95 {
96 synchObj.Release();
97 }
98 }
99
105 public static async Task<CachedEventHandler[]> GetEventHandlers(string EventType)
106 {
107 await synchObj.WaitAsync();
108 try
109 {
110 if (!eventHandlers.TryGetValue(EventType, out Dictionary<string, CachedEventHandler> Handlers))
111 {
112 Dictionary<string, CachedEventHandler> List = await LoadEventHandlers(EventType);
113 Handlers = List.Count == 0 ? null : List;
114 eventHandlers[EventType] = Handlers;
115 }
116
117 if (Handlers is null)
118 return null;
119
120 int c = Handlers.Count;
121 CachedEventHandler[] Result = new CachedEventHandler[c];
122 Handlers.Values.CopyTo(Result, 0);
123
124 return Result;
125 }
126 finally
127 {
128 synchObj.Release();
129 }
130 }
131
132 private static async Task<Dictionary<string, CachedEventHandler>> LoadEventHandlers(string EventType)
133 {
134 IEnumerable<CachedEventHandler> Handlers = await Database.Find<CachedEventHandler>(new FilterFieldEqualTo("EventType", EventType));
135 Dictionary<string, CachedEventHandler> Result = new Dictionary<string, CachedEventHandler>();
136
137 foreach (CachedEventHandler Handler in Handlers)
138 Result[Handler.ObjectId] = Handler;
139
140 return Result;
141 }
142
143 }
144}
Static interface for database persistence. In order to work, a database provider has to be assigned t...
Definition: Database.cs:19
static Task< IEnumerable< object > > Find(string Collection, params string[] SortOrder)
Finds objects in a given collection.
Definition: Database.cs:247
This filter selects objects that have a named field equal to a given value.
Implements an in-memory cache.
Definition: Cache.cs:15
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.
EventType
Type of event.
Definition: EventType.cs:7