1
1
namespace NRedisStack . Graph
2
2
{
3
- internal class GraphCacheList
3
+ internal sealed class GraphCacheList
4
4
{
5
- protected readonly string GraphName ;
6
- protected readonly string Procedure ;
7
- private string [ ] _data ;
5
+ private readonly string _graphName ;
6
+ private readonly string _procedure ;
8
7
9
- protected readonly GraphCommands graph ;
10
- protected readonly GraphCommandsAsync asyncGraph ;
11
- private bool asyncGraphUsed ;
8
+ private string [ ] _data = Array . Empty < string > ( ) ;
9
+
10
+ private readonly GraphCommandsAsync _redisGraph ;
12
11
13
12
private readonly object _locker = new object ( ) ;
14
13
15
- internal GraphCacheList ( string graphName , string procedure , GraphCommands redisGraph ) : this ( graphName , procedure )
14
+ /// <summary>
15
+ /// Constructs a <see cref="GraphCacheList"/> for providing cached information about the graph.
16
+ /// </summary>
17
+ /// <param name="graphName">The name of the graph to cache information for.</param>
18
+ /// <param name="procedure">The saved procedure to call to populate cache. Must be a `read` procedure.</param>
19
+ /// <param name="redisGraph">The graph used for the calling the <paramref name="procedure"/>.</param>
20
+ internal GraphCacheList ( string graphName , string procedure , GraphCommands redisGraph )
16
21
{
17
- graph = redisGraph ;
18
- asyncGraphUsed = false ;
19
- }
22
+ _graphName = graphName ;
23
+ _procedure = procedure ;
20
24
21
- internal GraphCacheList ( string graphName , string procedure , GraphCommandsAsync redisGraph ) : this ( graphName , procedure )
22
- {
23
- asyncGraph = redisGraph ;
24
- asyncGraphUsed = true ;
25
+ _redisGraph = redisGraph ;
25
26
}
26
27
27
- private GraphCacheList ( string graphName , string procedure )
28
+ /// <summary>
29
+ /// Constructs a <see cref="GraphCacheList"/> for providing cached information about the graph.
30
+ /// </summary>
31
+ /// <param name="graphName">The name of the graph to cache information for.</param>
32
+ /// <param name="procedure">The saved procedure to call to populate cache. Must be a `read` procedure.</param>
33
+ /// <param name="redisGraph">The graph used for the calling the <paramref name="procedure"/>.</param>
34
+ internal GraphCacheList ( string graphName , string procedure , GraphCommandsAsync redisGraphAsync )
28
35
{
29
- GraphName = graphName ;
30
- Procedure = procedure ;
36
+ _graphName = graphName ;
37
+ _procedure = procedure ;
38
+
39
+ _redisGraph = redisGraphAsync ;
31
40
}
32
41
33
42
// TODO: Change this to use Lazy<T>?
34
43
internal string GetCachedData ( int index )
35
44
{
36
- if ( _data == null || index >= _data . Length )
45
+ if ( index >= _data . Length )
37
46
{
38
47
lock ( _locker )
39
48
{
40
- if ( _data == null || index >= _data . Length )
49
+ if ( index >= _data . Length )
41
50
{
42
- GetProcedureInfo ( ) ;
51
+ _data = GetProcedureInfo ( ) ;
43
52
}
44
53
}
45
54
}
46
55
47
56
return _data . ElementAtOrDefault ( index ) ;
48
57
}
49
58
50
- private void GetProcedureInfo ( )
59
+ private string [ ] GetProcedureInfo ( )
51
60
{
52
- var resultSet = CallProcedure ( asyncGraphUsed ) ;
53
- var newData = new string [ resultSet . Count ] ;
54
- var i = 0 ;
55
-
56
- foreach ( var record in resultSet )
57
- {
58
- newData [ i ++ ] = record . GetString ( 0 ) ;
59
- }
60
-
61
- _data = newData ;
61
+ var resultSet = CallProcedure ( ) ;
62
+ return resultSet
63
+ . Select ( r => r . GetString ( 0 ) )
64
+ . ToArray ( ) ;
62
65
}
63
66
64
- protected virtual ResultSet CallProcedure ( bool asyncGraphUsed = false )
67
+ private ResultSet CallProcedure ( )
65
68
{
66
- return asyncGraphUsed
67
- ? asyncGraph . CallProcedureAsync ( GraphName , Procedure ) . Result
68
- : graph . CallProcedure ( GraphName , Procedure ) ;
69
+ return _redisGraph is GraphCommands graphSync
70
+ ? graphSync . CallProcedure ( _graphName , _procedure , ProcedureMode . Read )
71
+ : _redisGraph . CallProcedureAsync ( _graphName , _procedure , ProcedureMode . Read ) . Result ;
69
72
}
70
73
}
71
74
}
0 commit comments