@@ -69,7 +69,28 @@ public void AddUnique(ProcessCategories category, string key)
69
69
if ( _items . ContainsKey ( key ) )
70
70
return ;
71
71
72
- _items . Add ( key , new ProcessItem { Id = key , Category = category } ) ;
72
+ _items . Add ( key , new ProcessItem {
73
+ Id = key ,
74
+ AddedUTC = DateTime . UtcNow ,
75
+ Category = category } ) ;
76
+
77
+ _log . LogInformation ( $ "Created process, category { category } , id { key } , no lifespan limit.") ;
78
+ }
79
+ }
80
+
81
+ public void AddUnique ( ProcessCategories category , string key , string metadata )
82
+ {
83
+ lock ( _items )
84
+ {
85
+ if ( _items . ContainsKey ( key ) )
86
+ return ;
87
+
88
+ _items . Add ( key , new ProcessItem {
89
+ Id = key ,
90
+ AddedUTC = DateTime . UtcNow ,
91
+ Category = category ,
92
+ Metadata = metadata } ) ;
93
+
73
94
_log . LogInformation ( $ "Created process, category { category } , id { key } , no lifespan limit.") ;
74
95
}
75
96
}
@@ -81,7 +102,14 @@ public void AddUnique(ProcessCategories category, string key, string metadata, T
81
102
if ( _items . ContainsKey ( key ) )
82
103
return ;
83
104
84
- _items . Add ( key , new ProcessItem { Id = key , Metadata = metadata , AddedUTC = DateTime . UtcNow , MaxLifespan = timespan , Category = category } ) ;
105
+ _items . Add ( key , new ProcessItem {
106
+ Id = key ,
107
+ Metadata = metadata ,
108
+ AddedUTC = DateTime . UtcNow ,
109
+ KeepAliveUtc = DateTime . UtcNow ,
110
+ MaxLifespan = timespan ,
111
+ Category = category } ) ;
112
+
85
113
_log . LogInformation ( $ "Created process, category { category } , id { key } , metadata { metadata } , forced lifespan { timespan } .") ;
86
114
}
87
115
}
@@ -93,7 +121,13 @@ public void AddUnique(ProcessCategories category, string key, TimeSpan timespan)
93
121
if ( _items . ContainsKey ( key ) )
94
122
return ;
95
123
96
- _items . Add ( key , new ProcessItem { Id = key , AddedUTC = DateTime . UtcNow , MaxLifespan = timespan , Category = category } ) ;
124
+ _items . Add ( key , new ProcessItem {
125
+ Id = key ,
126
+ AddedUTC = DateTime . UtcNow ,
127
+ KeepAliveUtc = DateTime . UtcNow ,
128
+ MaxLifespan = timespan ,
129
+ Category = category } ) ;
130
+
97
131
_log . LogInformation ( $ "Created process, category { category } , id { key } , forced lifespan { timespan } .") ;
98
132
}
99
133
}
@@ -133,25 +167,28 @@ public void KeepAlive(string key)
133
167
if ( ! _items . ContainsKey ( key ) )
134
168
return ;
135
169
136
- _items [ key ] . AddedUTC = DateTime . UtcNow ;
170
+ _items [ key ] . KeepAliveUtc = DateTime . UtcNow ;
137
171
}
138
172
}
139
173
140
174
public void ClearExpired ( )
141
175
{
142
- for ( int i = 0 ; i < _items . Count ; i ++ )
176
+ lock ( _items )
143
177
{
144
- string key = _items . Keys . ElementAt ( _items . Count - 1 - i ) ;
145
- if ( ! _items [ key ] . AddedUTC . HasValue || ! _items [ key ] . MaxLifespan . HasValue )
146
- continue ;
178
+ for ( int i = 0 ; i < _items . Count ; i ++ )
179
+ {
180
+ string key = _items . Keys . ElementAt ( _items . Count - 1 - i ) ;
181
+ if ( ! _items [ key ] . KeepAliveUtc . HasValue || ! _items [ key ] . MaxLifespan . HasValue )
182
+ continue ;
147
183
148
- TimeSpan ts = _items [ key ] . MaxLifespan . Value ;
149
- DateTime dt = _items [ key ] . AddedUTC . Value ;
150
- if ( DateTime . UtcNow - dt < ts )
151
- continue ;
184
+ TimeSpan ts = _items [ key ] . MaxLifespan . Value ;
185
+ DateTime dt = _items [ key ] . KeepAliveUtc . Value ;
186
+ if ( DateTime . UtcNow - dt < ts )
187
+ continue ;
152
188
153
- _items . Remove ( key ) ;
154
- _log . LogInformation ( $ "Processes id { key } timed out and removed.") ;
189
+ _items . Remove ( key ) ;
190
+ _log . LogInformation ( $ "Processes id { key } timed out and removed.") ;
191
+ }
155
192
}
156
193
}
157
194
0 commit comments