Skip to content
This repository was archived by the owner on Dec 5, 2024. It is now read-only.

Commit e20054e

Browse files
StanleyGoldmanshana
authored andcommitted
Functionality to cancel cache invalidation
Correctly handling errors with tasks Cleanup Adding an additional warning message Avoid creating a new lambda for every invalidation
1 parent 6c9eee6 commit e20054e

File tree

4 files changed

+57
-26
lines changed

4 files changed

+57
-26
lines changed

src/GitHub.Api/Cache/CacheInterfaces.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public interface IManagedCache
4040

4141
bool ValidateData();
4242
void InvalidateData();
43+
void ResetInvalidation();
4344

4445
DateTimeOffset LastUpdatedAt { get; }
4546
CacheType CacheType { get; }
@@ -91,7 +92,7 @@ public interface IBranchCache : IManagedCache
9192
ILocalConfigBranchDictionary LocalConfigBranches { get; }
9293
IRemoteConfigBranchDictionary RemoteConfigBranches { get; }
9394
IConfigRemoteDictionary ConfigRemotes { get; }
94-
95+
9596
void SetRemotes(Dictionary<string, ConfigRemote> remoteConfigs, Dictionary<string, Dictionary<string, ConfigBranch>> configBranches, GitRemote[] gitRemotes, GitBranch[] gitBranches);
9697
void SetLocals(Dictionary<string, ConfigBranch> configBranches, GitBranch[] gitBranches);
9798
}

src/GitHub.Api/Git/Repository.cs

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -208,39 +208,50 @@ private void CacheHasBeenInvalidated(CacheType cacheType)
208208
switch (cacheType)
209209
{
210210
case CacheType.Branches:
211-
repositoryManager?.UpdateBranches().Start();
211+
repositoryManager?.UpdateBranches().Catch(ex => InvalidationFailed(ex, cacheType)).Start();
212212
break;
213213

214214
case CacheType.GitLog:
215-
repositoryManager?.UpdateGitLog().Start();
215+
repositoryManager?.UpdateGitLog().Catch(ex => InvalidationFailed(ex, cacheType)).Start();
216216
break;
217217

218218
case CacheType.GitAheadBehind:
219-
repositoryManager?.UpdateGitAheadBehindStatus().Start();
219+
repositoryManager?.UpdateGitAheadBehindStatus().Catch(ex => InvalidationFailed(ex, cacheType)).Start();
220220
break;
221221

222222
case CacheType.GitLocks:
223223
if (CurrentRemote != null)
224-
repositoryManager?.UpdateLocks().Start();
224+
{
225+
repositoryManager?.UpdateLocks().Catch(ex => InvalidationFailed(ex, cacheType)).Start();
226+
}
225227
break;
226228

227229
case CacheType.GitUser:
228230
// user handles its own invalidation event
229231
break;
230232

231233
case CacheType.RepositoryInfo:
232-
repositoryManager?.UpdateRepositoryInfo().Start();
234+
repositoryManager?.UpdateRepositoryInfo().Catch(ex => InvalidationFailed(ex, cacheType)).Start();
233235
break;
234236

235237
case CacheType.GitStatus:
236-
repositoryManager?.UpdateGitStatus().Start();
238+
repositoryManager?.UpdateGitStatus().Catch(ex => InvalidationFailed(ex, cacheType)).Start();
237239
break;
238240

239241
default:
240242
throw new ArgumentOutOfRangeException(nameof(cacheType), cacheType, null);
241243
}
242244
}
243245

246+
private bool InvalidationFailed(Exception ex, CacheType cacheType)
247+
{
248+
Logger.Warning(ex, "Error invalidating {0}", cacheType);
249+
var managedCache = cacheContainer.GetCache(cacheType);
250+
managedCache.ResetInvalidation();
251+
return false;
252+
}
253+
254+
244255
private void RepositoryManagerOnCurrentBranchUpdated(ConfigBranch? branch, ConfigRemote? remote, string head)
245256
{
246257
taskManager.RunInUI(() =>
@@ -254,7 +265,7 @@ private void RepositoryManagerOnCurrentBranchUpdated(ConfigBranch? branch, Confi
254265
name = null;
255266
cloneUrl = null;
256267
cacheContainer.RepositoryInfoCache.UpdateData(data);
257-
268+
258269
// force refresh of the Name and CloneUrl propertys
259270
var n = Name;
260271
});
@@ -485,6 +496,7 @@ private void UpdateUserAndEmail()
485496
}
486497

487498
gitClient.GetConfigUserAndEmail()
499+
.Catch(InvalidationFailed)
488500
.ThenInUI((success, value) =>
489501
{
490502
if (success)
@@ -494,7 +506,15 @@ private void UpdateUserAndEmail()
494506
}
495507
}).Start();
496508
}
497-
509+
510+
private bool InvalidationFailed(Exception ex)
511+
{
512+
Logger.Warning(ex, "Error invalidating user cache");
513+
var managedCache = cacheContainer.GetCache(CacheType.GitUser);
514+
managedCache.ResetInvalidation();
515+
return false;
516+
}
517+
498518
public string Name
499519
{
500520
get { return cacheContainer.GitUserCache.Name; }

src/UnityExtension/Assets/Editor/GitHub.Unity/ApplicationCache.cs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -213,12 +213,14 @@ public void InvalidateData()
213213

214214
private void Invalidate()
215215
{
216-
if (!isInvalidating)
217-
{
218-
isInvalidating = true;
219-
LastUpdatedAt = DateTimeOffset.MinValue;
220-
CacheInvalidated.SafeInvoke(CacheType);
221-
}
216+
isInvalidating = true;
217+
LastUpdatedAt = DateTimeOffset.MinValue;
218+
CacheInvalidated.SafeInvoke(CacheType);
219+
}
220+
221+
public void ResetInvalidation()
222+
{
223+
isInvalidating = false;
222224
}
223225

224226
protected void SaveData(DateTimeOffset now, bool isChanged)
@@ -463,25 +465,25 @@ public void UpdateData(IRepositoryInfoCacheData data)
463465
isUpdated = true;
464466
}
465467

466-
if (forcedInvalidation ||!Nullable.Equals(currentGitBranch, data.CurrentGitBranch))
468+
if (forcedInvalidation || !Nullable.Equals(currentGitBranch, data.CurrentGitBranch))
467469
{
468470
currentGitBranch = data.CurrentGitBranch ?? GitBranch.Default;
469471
isUpdated = true;
470472
}
471473

472-
if (forcedInvalidation ||!Nullable.Equals(currentConfigRemote, data.CurrentConfigRemote))
474+
if (forcedInvalidation || !Nullable.Equals(currentConfigRemote, data.CurrentConfigRemote))
473475
{
474476
currentConfigRemote = data.CurrentConfigRemote ?? ConfigRemote.Default;
475477
isUpdated = true;
476478
}
477479

478-
if (forcedInvalidation ||!Nullable.Equals(currentConfigBranch, data.CurrentConfigBranch))
480+
if (forcedInvalidation || !Nullable.Equals(currentConfigBranch, data.CurrentConfigBranch))
479481
{
480482
currentConfigBranch = data.CurrentConfigBranch ?? ConfigBranch.Default;
481483
isUpdated = true;
482484
}
483485

484-
if (forcedInvalidation ||!String.Equals(currentHead, data.CurrentHead))
486+
if (forcedInvalidation || !String.Equals(currentHead, data.CurrentHead))
485487
{
486488
currentHead = data.CurrentHead;
487489
isUpdated = true;

src/tests/IntegrationTests/CachingClasses.cs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ abstract class ManagedCacheBase<T> : ScriptObjectSingleton<T> where T : class, I
109109
private DateTimeOffset? initializedAtValue;
110110

111111
private bool isInvalidating;
112+
protected bool forcedInvalidation;
112113

113114
public event Action<CacheType> CacheInvalidated;
114115
public event Action<CacheType, DateTimeOffset> CacheUpdated;
@@ -134,13 +135,20 @@ public bool ValidateData()
134135

135136
public void InvalidateData()
136137
{
137-
if (!isInvalidating)
138-
{
139-
Logger.Trace("Invalidate");
140-
isInvalidating = true;
141-
LastUpdatedAt = DateTimeOffset.MinValue;
142-
CacheInvalidated.SafeInvoke(CacheType);
143-
}
138+
forcedInvalidation = true;
139+
Invalidate();
140+
}
141+
142+
private void Invalidate()
143+
{
144+
isInvalidating = true;
145+
LastUpdatedAt = DateTimeOffset.MinValue;
146+
CacheInvalidated.SafeInvoke(CacheType);
147+
}
148+
149+
public void ResetInvalidation()
150+
{
151+
isInvalidating = false;
144152
}
145153

146154
protected void SaveData(DateTimeOffset now, bool isChanged)

0 commit comments

Comments
 (0)