1 |
#Region "Microsoft.VisualBasic::ca137f63aa68b780a3b551952327a377, Microsoft.VisualBasic.Core\ComponentModel\DataSource\Repository\QueryCache.vb"
|
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 |
#End Region
|
46 |
|
47 |
Imports System.Runtime.CompilerServices
|
48 |
Imports Microsoft.VisualBasic.Language
|
49 |
Imports Microsoft.VisualBasic.Language.Default
|
50 |
Imports Microsoft.VisualBasic.Linq
|
51 |
|
52 |
Namespace ComponentModel.DataSourceModel.Repository
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
Public MustInherit Class QueryCacheFactory(Of T As IKeyedEntity(Of String))
|
59 |
Implements IRepositoryRead(Of String, T)
|
60 |
|
61 |
Dim cache As New Dictionary(Of String, T)
|
62 |
Dim factory As Func(Of String, T)
|
63 |
Dim assertIsNothing As Assert(Of Object)
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
<param name="factory"></param>
|
69 |
<param name="cache"></param>
|
70 |
<param name="assertIsNothing">如果这个函数返回的结果是True,说明目标为空值,这个主要是针对于structure类型而言的</param>
|
71 |
Sub New(factory As Func(Of String, T), Optional cache As IReadOnlyDictionary(Of String, T) = Nothing, Optional assertIsNothing As Assert(Of Object) = Nothing)
|
72 |
Me.factory = factory
|
73 |
Me.cache = cache.SafeQuery.ToDictionary
|
74 |
Me.assertIsNothing = assertIsNothing Or defaultAssert
|
75 |
End Sub
|
76 |
|
77 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
78 |
Public Overrides Function ToString() As String
|
79 |
Return $"{cache.Count} entity was cached. (keys={cache.Keys.Take(5).JoinBy("; ")}...)"
|
80 |
End Function
|
81 |
|
82 |
|
83 |
Clear the cache memory
|
84 |
|
85 |
|
86 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
87 |
Public Overridable Sub Clear()
|
88 |
Call cache.Clear()
|
89 |
End Sub
|
90 |
|
91 |
Public Overridable Function Exists(key As String) As Boolean Implements IRepositoryRead(Of String, T).Exists
|
92 |
If cache.ContainsKey(key) Then
|
93 |
Return True
|
94 |
Else
|
95 |
Return Not assertIsNothing(GetByKey(key))
|
96 |
End If
|
97 |
End Function
|
98 |
|
99 |
|
100 |
Load by <see cref="factory"/> or read from <see cref="cache"/>.
|
101 |
|
102 |
<param name="key"></param>
|
103 |
<returns></returns>
|
104 |
Public Overridable Function GetByKey(key As String) As T Implements IRepositoryRead(Of String, T).GetByKey
|
105 |
If cache.ContainsKey(key) Then
|
106 |
|
107 |
Return cache(key)
|
108 |
Else
|
109 |
Dim entity As T = factory(key)
|
110 |
|
111 |
If assertIsNothing(entity) = True Then
|
112 |
Return Nothing
|
113 |
Else
|
114 |
cache.Add(entity.Key, entity)
|
115 |
End If
|
116 |
|
117 |
Return entity
|
118 |
End If
|
119 |
End Function
|
120 |
|
121 |
|
122 |
Only works on cache
|
123 |
|
124 |
<param name="clause"></param>
|
125 |
<returns></returns>
|
126 |
Public Overridable Function GetWhere(clause As Func(Of T, Boolean)) As IReadOnlyDictionary(Of String, T) Implements IRepositoryRead(Of String, T).GetWhere
|
127 |
Return cache.Values.Where(clause).ToDictionary(Function(t) t.Key)
|
128 |
End Function
|
129 |
|
130 |
|
131 |
Only works on cache
|
132 |
|
133 |
<returns></returns>
|
134 |
Public Overridable Function GetAll() As IReadOnlyDictionary(Of String, T) Implements IRepositoryRead(Of String, T).GetAll
|
135 |
Return New Dictionary(Of String, T)(cache)
|
136 |
End Function
|
137 |
End Class
|
138 |
End Namespace
|