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

Cancelling invalidation of cache #923

Merged
merged 1 commit into from
Nov 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/GitHub.Api/Cache/CacheInterfaces.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public interface IManagedCache

bool ValidateData();
void InvalidateData();
void ResetInvalidation();

DateTimeOffset LastUpdatedAt { get; }
CacheType CacheType { get; }
Expand Down Expand Up @@ -91,7 +92,7 @@ public interface IBranchCache : IManagedCache
ILocalConfigBranchDictionary LocalConfigBranches { get; }
IRemoteConfigBranchDictionary RemoteConfigBranches { get; }
IConfigRemoteDictionary ConfigRemotes { get; }

void SetRemotes(Dictionary<string, ConfigRemote> remoteConfigs, Dictionary<string, Dictionary<string, ConfigBranch>> configBranches, GitRemote[] gitRemotes, GitBranch[] gitBranches);
void SetLocals(Dictionary<string, ConfigBranch> configBranches, GitBranch[] gitBranches);
}
Expand Down
36 changes: 28 additions & 8 deletions src/GitHub.Api/Git/Repository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -208,39 +208,50 @@ private void CacheHasBeenInvalidated(CacheType cacheType)
switch (cacheType)
{
case CacheType.Branches:
repositoryManager?.UpdateBranches().Start();
repositoryManager?.UpdateBranches().Catch(ex => InvalidationFailed(ex, cacheType)).Start();
break;

case CacheType.GitLog:
repositoryManager?.UpdateGitLog().Start();
repositoryManager?.UpdateGitLog().Catch(ex => InvalidationFailed(ex, cacheType)).Start();
break;

case CacheType.GitAheadBehind:
repositoryManager?.UpdateGitAheadBehindStatus().Start();
repositoryManager?.UpdateGitAheadBehindStatus().Catch(ex => InvalidationFailed(ex, cacheType)).Start();
break;

case CacheType.GitLocks:
if (CurrentRemote != null)
repositoryManager?.UpdateLocks().Start();
{
repositoryManager?.UpdateLocks().Catch(ex => InvalidationFailed(ex, cacheType)).Start();
}
break;

case CacheType.GitUser:
// user handles its own invalidation event
break;

case CacheType.RepositoryInfo:
repositoryManager?.UpdateRepositoryInfo().Start();
repositoryManager?.UpdateRepositoryInfo().Catch(ex => InvalidationFailed(ex, cacheType)).Start();
break;

case CacheType.GitStatus:
repositoryManager?.UpdateGitStatus().Start();
repositoryManager?.UpdateGitStatus().Catch(ex => InvalidationFailed(ex, cacheType)).Start();
break;

default:
throw new ArgumentOutOfRangeException(nameof(cacheType), cacheType, null);
}
}

private bool InvalidationFailed(Exception ex, CacheType cacheType)
{
Logger.Warning(ex, "Error invalidating {0}", cacheType);
var managedCache = cacheContainer.GetCache(cacheType);
managedCache.ResetInvalidation();
return false;
}


private void RepositoryManagerOnCurrentBranchUpdated(ConfigBranch? branch, ConfigRemote? remote, string head)
{
taskManager.RunInUI(() =>
Expand All @@ -254,7 +265,7 @@ private void RepositoryManagerOnCurrentBranchUpdated(ConfigBranch? branch, Confi
name = null;
cloneUrl = null;
cacheContainer.RepositoryInfoCache.UpdateData(data);

// force refresh of the Name and CloneUrl propertys
var n = Name;
});
Expand Down Expand Up @@ -485,6 +496,7 @@ private void UpdateUserAndEmail()
}

gitClient.GetConfigUserAndEmail()
.Catch(InvalidationFailed)
.ThenInUI((success, value) =>
{
if (success)
Expand All @@ -494,7 +506,15 @@ private void UpdateUserAndEmail()
}
}).Start();
}


private bool InvalidationFailed(Exception ex)
{
Logger.Warning(ex, "Error invalidating user cache");
var managedCache = cacheContainer.GetCache(CacheType.GitUser);
managedCache.ResetInvalidation();
return false;
}

public string Name
{
get { return cacheContainer.GitUserCache.Name; }
Expand Down
22 changes: 12 additions & 10 deletions src/UnityExtension/Assets/Editor/GitHub.Unity/ApplicationCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -213,12 +213,14 @@ public void InvalidateData()

private void Invalidate()
{
if (!isInvalidating)
{
isInvalidating = true;
LastUpdatedAt = DateTimeOffset.MinValue;
CacheInvalidated.SafeInvoke(CacheType);
}
isInvalidating = true;
LastUpdatedAt = DateTimeOffset.MinValue;
CacheInvalidated.SafeInvoke(CacheType);
}

public void ResetInvalidation()
{
isInvalidating = false;
}

protected void SaveData(DateTimeOffset now, bool isChanged)
Expand Down Expand Up @@ -463,25 +465,25 @@ public void UpdateData(IRepositoryInfoCacheData data)
isUpdated = true;
}

if (forcedInvalidation ||!Nullable.Equals(currentGitBranch, data.CurrentGitBranch))
if (forcedInvalidation || !Nullable.Equals(currentGitBranch, data.CurrentGitBranch))
{
currentGitBranch = data.CurrentGitBranch ?? GitBranch.Default;
isUpdated = true;
}

if (forcedInvalidation ||!Nullable.Equals(currentConfigRemote, data.CurrentConfigRemote))
if (forcedInvalidation || !Nullable.Equals(currentConfigRemote, data.CurrentConfigRemote))
{
currentConfigRemote = data.CurrentConfigRemote ?? ConfigRemote.Default;
isUpdated = true;
}

if (forcedInvalidation ||!Nullable.Equals(currentConfigBranch, data.CurrentConfigBranch))
if (forcedInvalidation || !Nullable.Equals(currentConfigBranch, data.CurrentConfigBranch))
{
currentConfigBranch = data.CurrentConfigBranch ?? ConfigBranch.Default;
isUpdated = true;
}

if (forcedInvalidation ||!String.Equals(currentHead, data.CurrentHead))
if (forcedInvalidation || !String.Equals(currentHead, data.CurrentHead))
{
currentHead = data.CurrentHead;
isUpdated = true;
Expand Down
22 changes: 15 additions & 7 deletions src/tests/IntegrationTests/CachingClasses.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ abstract class ManagedCacheBase<T> : ScriptObjectSingleton<T> where T : class, I
private DateTimeOffset? initializedAtValue;

private bool isInvalidating;
protected bool forcedInvalidation;

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

public void InvalidateData()
{
if (!isInvalidating)
{
Logger.Trace("Invalidate");
isInvalidating = true;
LastUpdatedAt = DateTimeOffset.MinValue;
CacheInvalidated.SafeInvoke(CacheType);
}
forcedInvalidation = true;
Invalidate();
}

private void Invalidate()
{
isInvalidating = true;
LastUpdatedAt = DateTimeOffset.MinValue;
CacheInvalidated.SafeInvoke(CacheType);
}

public void ResetInvalidation()
{
isInvalidating = false;
}

protected void SaveData(DateTimeOffset now, bool isChanged)
Expand Down