11namespace NRedisStack . Graph
22{
3- internal class GraphCacheList
3+ internal sealed class GraphCacheList
44 {
5- protected readonly string GraphName ;
6- protected readonly string Procedure ;
7- private string [ ] _data ;
5+ private readonly string _graphName ;
6+ private readonly string _procedure ;
87
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 ;
1211
1312 private readonly object _locker = new object ( ) ;
1413
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 )
1621 {
17- graph = redisGraph ;
18- asyncGraphUsed = false ;
19- }
22+ _graphName = graphName ;
23+ _procedure = procedure ;
2024
21- internal GraphCacheList ( string graphName , string procedure , GraphCommandsAsync redisGraph ) : this ( graphName , procedure )
22- {
23- asyncGraph = redisGraph ;
24- asyncGraphUsed = true ;
25+ _redisGraph = redisGraph ;
2526 }
2627
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 )
2835 {
29- GraphName = graphName ;
30- Procedure = procedure ;
36+ _graphName = graphName ;
37+ _procedure = procedure ;
38+
39+ _redisGraph = redisGraphAsync ;
3140 }
3241
3342 // TODO: Change this to use Lazy<T>?
3443 internal string GetCachedData ( int index )
3544 {
36- if ( _data == null || index >= _data . Length )
45+ if ( index >= _data . Length )
3746 {
3847 lock ( _locker )
3948 {
40- if ( _data == null || index >= _data . Length )
49+ if ( index >= _data . Length )
4150 {
42- GetProcedureInfo ( ) ;
51+ _data = GetProcedureInfo ( ) ;
4352 }
4453 }
4554 }
4655
4756 return _data . ElementAtOrDefault ( index ) ;
4857 }
4958
50- private void GetProcedureInfo ( )
59+ private string [ ] GetProcedureInfo ( )
5160 {
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 ( ) ;
6265 }
6366
64- protected virtual ResultSet CallProcedure ( bool asyncGraphUsed = false )
67+ private ResultSet CallProcedure ( )
6568 {
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 ;
6972 }
7073 }
7174}
0 commit comments