@@ -12,7 +12,13 @@ public class VersionCache : SafeDictionary<string, VersionCacheEntry>
1212 private readonly ILogger < VersionCache > _logger ;
1313 private readonly PeriodicTimer ? _refreshTimer ;
1414
15- private ( string Name , long Id , string Path ) ? _cachedProject ;
15+ private Project ? _cachedProject ;
16+
17+ public bool HasProjectInfo => _cachedProject != null ;
18+
19+ public string ProjectName => _cachedProject ! . NameWithNamespace ;
20+ public long ProjectId => _cachedProject ! . Id ;
21+ public string ProjectPath => _cachedProject ! . PathWithNamespace ;
1622
1723 private string ? _latestTag ;
1824
@@ -52,18 +58,13 @@ public VersionCache(IConfiguration config, GitLabService gitlabService, ILogger<
5258 }
5359 }
5460
55- public string ReleaseUrlFormat => $ "{ _gitlabEndpoint . TrimEnd ( '/' ) } /{ _cachedProject ! . Value . Path } /-/releases/{{0}}";
61+ public string ReleaseUrlFormat => $ "{ _gitlabEndpoint . TrimEnd ( '/' ) } /{ ProjectPath } /-/releases/{{0}}";
5662
5763 public void Init ( ProjectId projectId ) => Executor . ExecuteBackgroundAsync ( async ( ) =>
5864 {
5965 try
6066 {
61- if ( ! _cachedProject . HasValue )
62- {
63- var project = await _gl . Client . Projects . GetAsync ( projectId ) ;
64-
65- _cachedProject = ( project . NameWithNamespace , project . Id , project . PathWithNamespace ) ;
66- }
67+ _cachedProject ??= await _gl . Client . Projects . GetAsync ( projectId ) ;
6768 }
6869 catch ( GitLabException e )
6970 {
@@ -73,7 +74,7 @@ public void Init(ProjectId projectId) => Executor.ExecuteBackgroundAsync(async (
7374 return ;
7475 }
7576
76- _logger . LogInformation ( "Initializing version cache for {project}" , _cachedProject ! . Value . Name ) ;
77+ _logger . LogInformation ( "Initializing version cache for {project}" , ProjectName ) ;
7778
7879 await RefreshAsync ( ) ;
7980
@@ -85,12 +86,12 @@ public void Init(ProjectId projectId) => Executor.ExecuteBackgroundAsync(async (
8586
8687 _logger . LogInformation (
8788 "Periodic version cache refreshing is disabled for {project}. It can be refreshed by {means}" ,
88- _cachedProject ! . Value . Name , howToRefresh ) ;
89+ ProjectName , howToRefresh ) ;
8990 return ;
9091 }
9192
9293 _logger . LogInformation ( "Refreshing version cache for {project} every {timePeriod} minutes." ,
93- _cachedProject ! . Value . Name , _refreshTimer . Period . TotalMinutes ) ;
94+ ProjectName , _refreshTimer . Period . TotalMinutes ) ;
9495 while ( await _refreshTimer . WaitForNextTickAsync ( ) )
9596 {
9697 await RefreshAsync ( ) ;
@@ -111,19 +112,19 @@ public void Init(ProjectId projectId) => Executor.ExecuteBackgroundAsync(async (
111112
112113 public async Task RefreshAsync ( )
113114 {
114- _logger . LogInformation ( "Reloading version cache for {project}" , _cachedProject ! . Value . Name ) ;
115+ _logger . LogInformation ( "Reloading version cache for {project}" , ProjectName ) ;
115116
116- _latestTag = ( await _gl . GetLatestReleaseAsync ( _cachedProject . Value . Id ) ) ? . TagName ;
117+ _latestTag = ( await _gl . GetLatestReleaseAsync ( ProjectId ) ) ? . TagName ;
117118
118119 if ( _latestTag is null )
119120 {
120- _logger . LogWarning ( "Latest version for {project} was a 404, aborting." , _cachedProject . Value . Name ) ;
121+ _logger . LogWarning ( "Latest version for {project} was a 404, aborting." , ProjectName ) ;
121122 return ;
122123 }
123124
124125 var sw = Stopwatch . StartNew ( ) ;
125126
126- var releases = await _gl . PageReleases ( _cachedProject . Value . Id )
127+ var releases = await _gl . PageReleases ( ProjectId )
127128 . GetAllAsync ( onNonSuccess :
128129 code => _logger . LogError (
129130 "One of the pagination requests to get all releases returned a non-success status code: {code}" ,
@@ -178,13 +179,13 @@ public async Task RefreshAsync()
178179 if ( Count > 0 )
179180 {
180181 _logger . LogInformation ( "Clearing {entryCount} version cache entries for {project}" , Count ,
181- _cachedProject ! . Value . Name ) ;
182+ ProjectName ) ;
182183 Clear ( ) ;
183184 }
184185
185186 foreach ( var ( tag , entry ) in tempCacheEntries )
186187 {
187- _logger . LogTrace ( "Adding version cache entry {tag} for {project}" , tag , _cachedProject ! . Value . Name ) ;
188+ _logger . LogTrace ( "Adding version cache entry {tag} for {project}" , tag , ProjectName ) ;
188189
189190 this [ tag ] = entry ;
190191 }
@@ -196,7 +197,7 @@ public async Task RefreshAsync()
196197 _semaphore . Release ( ) ;
197198
198199 _logger . LogInformation ( "Loaded {entryCount} version cache entries for {project}; took {time}ms." , Count ,
199- _cachedProject ! . Value . Name , sw . ElapsedMilliseconds ) ;
200+ ProjectName , sw . ElapsedMilliseconds ) ;
200201 }
201202
202203 public static void InitializeVersionCaches ( WebApplication app )
0 commit comments