Repository navigation
Expand file tree
/
Copy pathObservableCacheEx.cs
More file actions
640 lines (568 loc) · 29.9 KB
/
Copy pathObservableCacheEx.cs
File metadata and controls
640 lines (568 loc) · 29.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
#region Usings
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reactive.Concurrency;
using System.Reactive.Disposables;
using System.Reactive.Linq;
using System.Threading;
using DynamicData.Controllers;
using DynamicData.Internal;
using DynamicData.Kernel;
#endregion
namespace DynamicData
{
/// <summary>
/// The entry point for the dynamic data sub system
/// </summary>
public static class ObservableCacheEx
{
#region Populate changetset from observables
/// <summary>
/// Converts the observable to an observable changeset. The items are uniquely identified by the hashcode
/// </summary>
/// <typeparam name="TObject">The type of the object.</typeparam>
/// <param name="source">The source.</param>
/// <param name="expireAfter">Specify on a per object level the maximum time before an object expires from a cache</param>
/// <param name="limitSizeTo">Remove the oldest items when the size has reached this limit</param>
/// <param name="scheduler">The scheduler (only used for time expiry).</param>
/// <returns></returns>
/// <exception cref="System.ArgumentNullException">source
/// or
/// keySelector</exception>
public static IObservable<IChangeSet<TObject, int>> ToObservableChangeSet<TObject>(
this IObservable<TObject> source,
Func<TObject, TimeSpan?> expireAfter = null,
int limitSizeTo = -1,
IScheduler scheduler = null)
{
return source.ToObservableChangeSet(t => t.GetHashCode(), expireAfter, limitSizeTo, scheduler);
}
/// <summary>
/// Converts the observable to an observable changeset
/// </summary>
/// <typeparam name="TObject">The type of the object.</typeparam>
/// <typeparam name="TKey">The type of the key.</typeparam>
/// <param name="source">The source.</param>
/// <param name="keySelector">The key selector.</param>
/// <param name="expireAfter">Specify on a per object level the maximum time before an object expires from a cache</param>
/// <param name="limitSizeTo">Remove the oldest items when the size has reached this limit</param>
/// <param name="scheduler">The scheduler (only used for time expiry).</param>
/// <returns></returns>
/// <exception cref="System.ArgumentNullException">source
/// or
/// keySelector</exception>
public static IObservable<IChangeSet<TObject, TKey>> ToObservableChangeSet<TObject, TKey>(this IObservable<TObject> source, Func<TObject,TKey> keySelector,
Func<TObject, TimeSpan?> expireAfter=null,
int limitSizeTo = -1,
IScheduler scheduler=null)
{
if (source == null) throw new ArgumentNullException("source");
if (keySelector == null) throw new ArgumentNullException("keySelector");
return Observable.Create<IChangeSet<TObject, TKey>>(observer =>
{
var cache = new SourceCache<TObject, TKey>(keySelector);
var sourceSubscriber = source.Subscribe(cache.AddOrUpdate);
var expirer = expireAfter !=null
? cache.ExpireAfter(expireAfter, scheduler ?? Scheduler.Default).Subscribe()
: Disposable.Empty;
var sizeLimiter = limitSizeTo > 0
? cache.LimitSizeTo(limitSizeTo, scheduler).Subscribe()
: Disposable.Empty;
var notifier = cache.Connect().SubscribeSafe(observer);
return new CompositeDisposable(cache, sourceSubscriber, notifier, expirer, sizeLimiter);
});
}
/// <summary>
/// Converts the observable to an observable changeset
/// </summary>
/// <typeparam name="TObject">The type of the object.</typeparam>
/// <param name="source">The source.</param>
/// <param name="expireAfter">Specify on a per object level the maximum time before an object expires from a cache</param>
/// <param name="limitSizeTo">Remove the oldest items when the size has reached this limit</param>
/// <param name="scheduler">The scheduler (only used for time expiry).</param>
/// <returns>An observable changeset</returns>
/// <exception cref="System.ArgumentNullException">source
/// or
/// keySelector</exception>
public static IObservable<IChangeSet<TObject, int>> ToObservableChangeSet<TObject>(
this IObservable<IEnumerable<TObject>> source,
Func<TObject, TimeSpan?> expireAfter = null,
int limitSizeTo = -1,
IScheduler scheduler = null)
{
return source.ToObservableChangeSet(t => t.GetHashCode(), expireAfter, limitSizeTo, scheduler);
}
/// <summary>
/// Converts the observable to an observable changeset
/// </summary>
/// <typeparam name="TObject">The type of the object.</typeparam>
/// <typeparam name="TKey">The type of the key.</typeparam>
/// <param name="source">The source.</param>
/// <param name="keySelector">The key selector.</param>
/// <param name="expireAfter">Specify on a per object level the maximum time before an object expires from a cache</param>
/// <param name="limitSizeTo">Remove the oldest items when the size has reached this limit</param>
/// <param name="scheduler">The scheduler (only used for time expiry).</param>
/// <returns>An observable changeset</returns>
/// <exception cref="System.ArgumentNullException">source
/// or
/// keySelector</exception>
public static IObservable<IChangeSet<TObject, TKey>> ToObservableChangeSet<TObject, TKey>(this IObservable<IEnumerable<TObject>> source, Func<TObject, TKey> keySelector,
Func<TObject, TimeSpan?> expireAfter=null,
int limitSizeTo = -1,
IScheduler scheduler=null)
{
if (source == null) throw new ArgumentNullException("source");
if (keySelector == null) throw new ArgumentNullException("keySelector");
return Observable.Create<IChangeSet<TObject, TKey>>(observer =>
{
var cache = new SourceCache<TObject, TKey>(keySelector);
var sourceSubscriber = source.Subscribe(cache.AddOrUpdate);
var expirer = expireAfter != null
? cache.ExpireAfter(expireAfter,scheduler ?? Scheduler.Default).Subscribe((kvp) => { },observer.OnError)
: Disposable.Empty;
var sizeLimiter = limitSizeTo > 0
? cache.LimitSizeTo(limitSizeTo, scheduler).Subscribe((kvp) => { }, observer.OnError)
: Disposable.Empty;
var notifier = cache.Connect().SubscribeSafe(observer);
return new CompositeDisposable(cache, sourceSubscriber, notifier, expirer, sizeLimiter);
});
}
#endregion
#region Populate into an observable cache
/// <summary>
/// Populates a source into the specified cache.
/// </summary>
/// <typeparam name="TObject">The type of the object.</typeparam>
/// <typeparam name="TKey">The type of the key.</typeparam>
/// <param name="source">The source.</param>
/// <param name="detination">The detination.</param>
/// <returns></returns>
/// <exception cref="System.ArgumentNullException">
/// source
/// or
/// detination
/// </exception>
public static IDisposable PopulateInto<TObject, TKey>(this IObservable<IChangeSet<TObject, TKey>> source, ISourceCache<TObject, TKey> detination)
{
if (source == null) throw new ArgumentNullException("source");
if (detination == null) throw new ArgumentNullException("detination");
return source.Subscribe(changes => detination.BatchUpdate(updater => updater.Update(changes)));
}
/// <summary>
/// Populates a source into the specified cache
/// </summary>
/// <typeparam name="TObject">The type of the object.</typeparam>
/// <typeparam name="TKey">The type of the key.</typeparam>
/// <param name="source">The source.</param>
/// <param name="detination">The detination.</param>
/// <returns></returns>
/// <exception cref="System.ArgumentNullException">source
/// or
/// detination</exception>
public static IDisposable PopulateInto<TObject, TKey>(this IObservable<IChangeSet<TObject, TKey>> source, IIntermediateCache<TObject, TKey> detination)
{
if (source == null) throw new ArgumentNullException("source");
if (detination == null) throw new ArgumentNullException("detination");
return source.Subscribe(changes => detination.BatchUpdate(updater => updater.Update(changes)));
}
/// <summary>
/// Populate a cache from an obserable stream.
/// </summary>
/// <typeparam name="TObject">The type of the object.</typeparam>
/// <typeparam name="TKey">The type of the key.</typeparam>
/// <param name="source">The source.</param>
/// <param name="observable">The observable.</param>
/// <returns></returns>
/// <exception cref="System.ArgumentNullException">
/// source
/// or
/// keySelector
/// </exception>
public static IDisposable PopulateFrom<TObject, TKey>(this ISourceCache<TObject, TKey> source, IObservable<IEnumerable<TObject>> observable)
{
if (source == null) throw new ArgumentNullException("source");
return observable.Subscribe(source.AddOrUpdate);
}
/// <summary>
/// Populate a cache from an obserable stream.
/// </summary>
/// <typeparam name="TObject">The type of the object.</typeparam>
/// <typeparam name="TKey">The type of the key.</typeparam>
/// <param name="source">The source.</param>
/// <param name="observable">The observable.</param>
/// <returns></returns>
/// <exception cref="System.ArgumentNullException">
/// source
/// or
/// keySelector
/// </exception>
public static IDisposable PopulateFrom<TObject, TKey>(this ISourceCache<TObject, TKey> source, IObservable<TObject> observable)
{
if (source == null) throw new ArgumentNullException("source");
return observable.Subscribe(source.AddOrUpdate);
}
#endregion
#region AsObservableCache /Connect
/// <summary>
/// Converts the source to an read only observable cache
/// </summary>
/// <typeparam name="TObject">The type of the object.</typeparam>
/// <typeparam name="TKey">The type of the key.</typeparam>
/// <param name="source">The source.</param>
/// <returns></returns>
/// <exception cref="System.ArgumentNullException">source</exception>
public static IObservableCache<TObject, TKey> AsObservableCache<TObject, TKey>(this IObservableCache<TObject, TKey> source)
{
if (source == null) throw new ArgumentNullException("source");
return new AnomynousObservableCache<TObject, TKey>(source);
}
/// <summary>
/// Converts the source to an read only observable cache
/// </summary>
/// <typeparam name="TObject">The type of the object.</typeparam>
/// <typeparam name="TKey">The type of the key.</typeparam>
/// <param name="source">The source.</param>
/// <returns></returns>
/// <exception cref="System.ArgumentNullException">source</exception>
public static IObservableCache<TObject, TKey> AsObservableCache<TObject, TKey>(this IObservable<IChangeSet<TObject, TKey>> source)
{
if (source == null) throw new ArgumentNullException("source");
return new AnomynousObservableCache<TObject, TKey>(source);
}
/// <summary>
/// Creates a stream using the specified controlled filter.
/// The controlled filter enables dynamic inline changing of the filter.
/// </summary>
/// <typeparam name="TObject">The type of the object.</typeparam>
/// <typeparam name="TKey">The type of the key.</typeparam>
/// <param name="source">The source.</param>
/// <param name="filterController">The controlled filter.</param>
/// <returns></returns>
/// <exception cref="System.ArgumentNullException">filterController</exception>
public static IObservable<IChangeSet<TObject, TKey>> Connect<TObject, TKey>(this IObservableCache<TObject, TKey> source, FilterController<TObject> filterController)
{
if (filterController == null) throw new ArgumentNullException("filterController");
return source.Connect().Filter(filterController);
}
#endregion
#region Size / time limiters
/// <summary>
/// Limits the number of records in the cache to the size specified. When the size is reached
/// the oldest items are removed from the cache
/// </summary>
/// <typeparam name="TObject">The type of the object.</typeparam>
/// <typeparam name="TKey">The type of the key.</typeparam>
/// <param name="source">The source.</param>
/// <param name="sizeLimit">The size limit.</param>
/// <param name="scheduler">The scheduler.</param>
/// <returns></returns>
/// <exception cref="System.ArgumentNullException">source</exception>
/// <exception cref="System.ArgumentException">Size limit must be greater than zero</exception>
public static IObservable<IEnumerable<KeyValuePair<TKey, TObject>>> LimitSizeTo<TObject, TKey>(this ISourceCache<TObject, TKey> source,
int sizeLimit, IScheduler scheduler = null)
{
if (source == null) throw new ArgumentNullException("source");
if (sizeLimit <= 0) throw new ArgumentException("Size limit must be greater than zero");
return Observable.Create< IEnumerable<KeyValuePair<TKey, TObject>>>(observer =>
{
long orderItemWasAdded = -1;
var sizeLimiter = new SizeLimiter<TObject, TKey>(sizeLimit);
return source.Connect()
.FinallySafe(observer.OnCompleted)
.ObserveOn(scheduler ?? Scheduler.Default)
.Transform((t, v) =>
{
var index = Interlocked.Increment(ref orderItemWasAdded);
return new ExpirableItem<TObject, TKey>(t, v, DateTime.Now, index);
})
.Subscribe(changes =>
{
var result = sizeLimiter.CloneAndReturnExpiredOnly (changes);
if (result.Count == 0) return;
source.BatchUpdate(updater => result.ForEach(c => updater.Remove(c.Key)));
});
});
}
/// <summary>
/// Automatically removes items from the cache after the time specified by
/// the time selector elapses.
/// </summary>
/// <typeparam name="TObject">The type of the object.</typeparam>
/// <typeparam name="TKey">The type of the key.</typeparam>
/// <param name="source">The cache.</param>
/// <param name="timeSelector">The time selector. Return null if the item should never be removed</param>
/// <param name="scheduler">The scheduler to perform the work on.</param>
/// <returns>An observable of anumerable of the kev values which has been removed</returns>
/// <exception cref="System.ArgumentNullException">source
/// or
/// timeSelector</exception>
public static IObservable<IEnumerable<KeyValuePair<TKey,TObject>>> ExpireAfter<TObject, TKey>(this ISourceCache<TObject, TKey> source,
Func<TObject, TimeSpan?> timeSelector, IScheduler scheduler = null)
{
return source.ExpireAfter(timeSelector, null, scheduler);
}
/// <summary>
/// Automatically removes items from the cache after the time specified by
/// the time selector elapses.
/// </summary>
/// <typeparam name="TObject">The type of the object.</typeparam>
/// <typeparam name="TKey">The type of the key.</typeparam>
/// <param name="source">The cache.</param>
/// <param name="timeSelector">The time selector. Return null if the item should never be removed</param>
/// <param name="interval">A polling interval. Since multiple timer subscriptions can be expensive,
/// it may be worth setting the interval .
/// </param>
/// <returns>An observable of anumerable of the kev values which has been removed</returns>
/// <exception cref="System.ArgumentNullException">source
/// or
/// timeSelector</exception>
public static IObservable<IEnumerable<KeyValuePair<TKey,TObject>>> ExpireAfter<TObject, TKey>(this ISourceCache<TObject, TKey> source,
Func<TObject, TimeSpan?> timeSelector,TimeSpan? interval=null)
{
return ExpireAfter(source, timeSelector, interval, Scheduler.Default);
}
/// <summary>
/// Automatically removes items from the cache after the time specified by
/// the time selector elapses.
/// </summary>
/// <typeparam name="TObject">The type of the object.</typeparam>
/// <typeparam name="TKey">The type of the key.</typeparam>
/// <param name="source">The cache.</param>
/// <param name="timeSelector">The time selector. Return null if the item should never be removed</param>
/// <param name="pollingInterval">A polling interval. Since multiple timer subscriptions can be expensive,
/// it may be worth setting the interval.
/// </param>
/// <param name="scheduler">The scheduler.</param>
/// <returns>An observable of anumerable of the kev values which has been removed</returns>
/// <exception cref="System.ArgumentNullException">source
/// or
/// timeSelector</exception>
public static IObservable<IEnumerable<KeyValuePair<TKey,TObject>>> ExpireAfter<TObject, TKey>(this ISourceCache<TObject, TKey> source,
Func<TObject, TimeSpan?> timeSelector, TimeSpan? pollingInterval, IScheduler scheduler)
{
if (source == null) throw new ArgumentNullException("source");
if (timeSelector == null) throw new ArgumentNullException("timeSelector");
return Observable.Create<IEnumerable<KeyValuePair<TKey,TObject>>>(observer =>
{
scheduler = scheduler ?? Scheduler.Default;
return source.Connect()
.ForExpiry(timeSelector, pollingInterval, scheduler)
.FinallySafe(observer.OnCompleted)
.Subscribe(toRemove =>
{
try
{
//remove from cache and notify which items have been auto removed
var keyValuePairs = toRemove as KeyValuePair<TKey, TObject>[] ?? toRemove.ToArray();
if (keyValuePairs.Length == 0) return;
source.Remove(keyValuePairs.Select(kv => kv.Key));
observer.OnNext(keyValuePairs);
}
catch (Exception ex)
{
observer.OnError(ex);
}
});
});
}
#endregion
#region Convenient update methods
/// <summary>
/// Adds or updates the cache with the specified item.
/// </summary>
/// <typeparam name="TObject">The type of the object.</typeparam>
/// <typeparam name="TKey">The type of the key.</typeparam>
/// <param name="source">The source.</param>
/// <param name="item">The item.</param>
/// <exception cref="System.ArgumentNullException">source</exception>
public static void AddOrUpdate<TObject, TKey>(this ISourceCache<TObject, TKey> source, TObject item)
{
if (source == null) throw new ArgumentNullException("source");
source.BatchUpdate(updater=>updater.AddOrUpdate(item));
}
/// <summary>
/// <summary>
/// Adds or updates the cache with the specified items.
/// </summary>
/// </summary>
/// <typeparam name="TObject">The type of the object.</typeparam>
/// <typeparam name="TKey">The type of the key.</typeparam>
/// <param name="source">The source.</param>
/// <param name="items">The items.</param>
/// <exception cref="System.ArgumentNullException">source</exception>
public static void AddOrUpdate<TObject, TKey>(this ISourceCache<TObject, TKey> source, IEnumerable<TObject> items)
{
if (source == null) throw new ArgumentNullException("source");
source.BatchUpdate(updater => updater.AddOrUpdate(items));
}
/// <summary>
/// Removes the specified item from the cache.
///
/// If the item is not contained in the cache then the operation does nothing.
/// </summary>
/// <typeparam name="TObject">The type of the object.</typeparam>
/// <typeparam name="TKey">The type of the key.</typeparam>
/// <param name="source">The source.</param>
/// <param name="item">The item.</param>
/// <exception cref="System.ArgumentNullException">source</exception>
public static void Remove<TObject, TKey>(this ISourceCache<TObject, TKey> source, TObject item)
{
if (source == null) throw new ArgumentNullException("source");
source.BatchUpdate(updater => updater.Remove(item));
}
/// <summary>
/// Removes the specified key from the cache.
/// If the item is not contained in the cache then the operation does nothing.
/// </summary>
/// <typeparam name="TObject">The type of the object.</typeparam>
/// <typeparam name="TKey">The type of the key.</typeparam>
/// <param name="source">The source.</param>
/// <param name="key">The key.</param>
/// <exception cref="System.ArgumentNullException">source</exception>
public static void Remove<TObject, TKey>(this ISourceCache<TObject, TKey> source, TKey key)
{
if (source == null) throw new ArgumentNullException("source");
source.BatchUpdate(updater => updater.Remove(key));
}
/// <summary>
/// Removes the specified key from the cache.
/// If the item is not contained in the cache then the operation does nothing.
/// </summary>
/// <typeparam name="TObject">The type of the object.</typeparam>
/// <typeparam name="TKey">The type of the key.</typeparam>
/// <param name="source">The source.</param>
/// <param name="key">The key.</param>
/// <exception cref="System.ArgumentNullException">source</exception>
public static void RemoveKey<TObject, TKey>(this ISourceCache<TObject, TKey> source, TKey key)
{
if (source == null) throw new ArgumentNullException("source");
source.BatchUpdate(updater => updater.RemoveKey(key));
}
/// <summary>
/// Removes the specified items from the cache.
///
/// Any items not contained in the cache are ignored
/// </summary>
/// <typeparam name="TObject">The type of the object.</typeparam>
/// <typeparam name="TKey">The type of the key.</typeparam>
/// <param name="source">The source.</param>
/// <param name="items">The items.</param>
/// <exception cref="System.ArgumentNullException">source</exception>
public static void Remove<TObject, TKey>(this ISourceCache<TObject, TKey> source, IEnumerable<TObject> items)
{
if (source == null) throw new ArgumentNullException("source");
source.BatchUpdate(updater => updater.Remove(items));
}
/// <summary>
/// Removes the specified keys from the cache.
///
/// Any keys not contained in the cache are ignored
/// </summary>
/// <typeparam name="TObject">The type of the object.</typeparam>
/// <typeparam name="TKey">The type of the key.</typeparam>
/// <param name="source">The source.</param>
/// <param name="keys">The keys.</param>
/// <exception cref="System.ArgumentNullException">source</exception>
public static void Remove<TObject, TKey>(this ISourceCache<TObject, TKey> source, IEnumerable<TKey> keys)
{
if (source == null) throw new ArgumentNullException("source");
source.BatchUpdate(updater => updater.Remove(keys));
}
/// <summary>
/// Removes the specified keys from the cache.
///
/// Any keys not contained in the cache are ignored
/// </summary>
/// <typeparam name="TObject">The type of the object.</typeparam>
/// <typeparam name="TKey">The type of the key.</typeparam>
/// <param name="source">The source.</param>
/// <param name="keys">The keys.</param>
/// <exception cref="System.ArgumentNullException">source</exception>
public static void RemoveKeys<TObject, TKey>(this ISourceCache<TObject, TKey> source, IEnumerable<TKey> keys)
{
if (source == null) throw new ArgumentNullException("source");
source.BatchUpdate(updater => updater.RemoveKeys(keys));
}
/// <summary>
/// Clears all data
/// </summary>
/// <typeparam name="TObject">The type of the object.</typeparam>
/// <typeparam name="TKey">The type of the key.</typeparam>
/// <param name="source">The source.</param>
/// <exception cref="System.ArgumentNullException">source</exception>
public static void Clear<TObject, TKey>(this ISourceCache<TObject, TKey> source)
{
if (source == null) throw new ArgumentNullException("source");
source.BatchUpdate(updater => updater.Clear());
}
/// <summary>
/// Signal observers to re-evaluate the specified item.
/// </summary>
/// <typeparam name="TObject">The type of the object.</typeparam>
/// <typeparam name="TKey">The type of the key.</typeparam>
/// <param name="source">The source.</param>
/// <param name="item">The item.</param>
/// <exception cref="System.ArgumentNullException">source</exception>
public static void Evaluate<TObject, TKey>(this ISourceCache<TObject, TKey> source, TObject item)
{
if (source == null) throw new ArgumentNullException("source");
source.BatchUpdate(updater => updater.Evaluate(item));
}
/// <summary>
/// Signal observers to re-evaluate the specified items.
/// </summary>
/// <typeparam name="TObject">The type of the object.</typeparam>
/// <typeparam name="TKey">The type of the key.</typeparam>
/// <param name="source">The source.</param>
/// <param name="items">The items.</param>
/// <exception cref="System.ArgumentNullException">source</exception>
public static void Evaluate<TObject, TKey>(this ISourceCache<TObject, TKey> source, IEnumerable<TObject> items)
{
if (source == null) throw new ArgumentNullException("source");
source.BatchUpdate(updater => updater.Evaluate(items));
}
/// <summary>
/// Removes the specified key from the cache.
/// If the item is not contained in the cache then the operation does nothing.
/// </summary>
/// <typeparam name="TObject">The type of the object.</typeparam>
/// <typeparam name="TKey">The type of the key.</typeparam>
/// <param name="source">The source.</param>
/// <param name="key">The key.</param>
/// <exception cref="System.ArgumentNullException">source</exception>
public static void Remove<TObject, TKey>(this IIntermediateCache<TObject, TKey> source, TKey key)
{
if (source == null) throw new ArgumentNullException("source");
source.BatchUpdate(updater => updater.Remove(key));
}
/// <summary>
/// Removes the specified keys from the cache.
///
/// Any keys not contained in the cache are ignored
/// </summary>
/// <typeparam name="TObject">The type of the object.</typeparam>
/// <typeparam name="TKey">The type of the key.</typeparam>
/// <param name="source">The source.</param>
/// <param name="keys">The keys.</param>
/// <exception cref="System.ArgumentNullException">source</exception>
public static void Remove<TObject, TKey>(this IIntermediateCache<TObject, TKey> source, IEnumerable<TKey> keys)
{
if (source == null) throw new ArgumentNullException("source");
source.BatchUpdate(updater => updater.Remove(keys));
}
/// <summary>
/// Clears all items from the cache
/// </summary>
/// <typeparam name="TObject">The type of the object.</typeparam>
/// <typeparam name="TKey">The type of the key.</typeparam>
/// <param name="source">The source.</param>
/// <exception cref="System.ArgumentNullException">source</exception>
public static void Clear<TObject, TKey>(this IIntermediateCache<TObject, TKey> source)
{
if (source == null) throw new ArgumentNullException("source");
source.BatchUpdate(updater => updater.Clear());
}
#endregion
}
}