/
RevitDocumentAggregateCache.cs
77 lines (68 loc) · 2.04 KB
/
RevitDocumentAggregateCache.cs
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
#nullable enable
using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;
using RevitSharedResources.Interfaces;
namespace ConnectorRevit.Storage;
/// <summary>
/// Simple conversion cache to store elements that are retrieved and may be needed again throughout the conversion operation
/// </summary>
public sealed class RevitDocumentAggregateCache : IRevitDocumentAggregateCache
{
private readonly Dictionary<Type, IRevitObjectCache> objectCaches;
private readonly UIDocumentProvider uiDocumentProvider;
public Document Document => uiDocumentProvider.Entity.Document;
public RevitDocumentAggregateCache(UIDocumentProvider uiDocumentProvider)
{
this.uiDocumentProvider = uiDocumentProvider;
this.objectCaches = new();
}
public IRevitObjectCache<T> GetOrInitializeEmptyCacheOfType<T>(out bool isExistingCache)
{
return GetOrInitializeCacheOfTypeNullable<T>(null, out isExistingCache);
}
public IRevitObjectCache<T> GetOrInitializeCacheOfType<T>(
Action<IRevitObjectCache<T>> initializer,
out bool isExistingCache
)
{
return GetOrInitializeCacheOfTypeNullable<T>(initializer, out isExistingCache);
}
private IRevitObjectCache<T> GetOrInitializeCacheOfTypeNullable<T>(
Action<IRevitObjectCache<T>>? initializer,
out bool isExistingCache
)
{
if (!objectCaches.TryGetValue(typeof(T), out var singleCache))
{
isExistingCache = false;
singleCache = new RevitObjectCache<T>(this);
if (initializer != null)
{
initializer((IRevitObjectCache<T>)singleCache);
}
objectCaches.Add(typeof(T), singleCache);
}
else
{
isExistingCache = true;
}
return (IRevitObjectCache<T>)singleCache;
}
public IRevitObjectCache<T>? TryGetCacheOfType<T>()
{
if (!objectCaches.TryGetValue(typeof(T), out var singleCache))
{
return null;
}
return singleCache as IRevitObjectCache<T>;
}
public void Invalidate<T>()
{
objectCaches.Remove(typeof(T));
}
public void InvalidateAll()
{
objectCaches.Clear();
}
}