fix cache invalidation

This commit is contained in:
Massaki Archambault 2024-06-06 22:01:22 -04:00
parent 8293825c2b
commit 7fccd4b91d
2 changed files with 40 additions and 26 deletions

View File

@ -12,11 +12,13 @@ type Group struct {
ID int
Name string
gitlabClient *gitlabClient
mux sync.Mutex
// group content cache
// group content
childGroups map[string]fstree.GroupSource
childRepository map[string]fstree.RepositorySource
childProjects map[string]fstree.RepositorySource
}
func (g *Group) GetGroupID() uint64 {
@ -27,8 +29,15 @@ func (g *Group) InvalidateCache() {
g.mux.Lock()
defer g.mux.Unlock()
// clear child group from cache
for _, childGroup := range g.childGroups {
gid := int(childGroup.GetGroupID())
delete(g.gitlabClient.groupCache, gid)
}
g.childGroups = nil
// clear child repositories from cache
g.childGroups = nil
g.childRepository = nil
}
func (c *gitlabClient) fetchGroup(gid int) (*Group, error) {
@ -52,8 +61,10 @@ func (c *gitlabClient) fetchGroup(gid int) (*Group, error) {
ID: gitlabGroup.ID,
Name: gitlabGroup.Path,
gitlabClient: c,
childGroups: nil,
childRepository: nil,
childProjects: nil,
}
// save in cache
@ -79,8 +90,10 @@ func (c *gitlabClient) newGroupFromGitlabGroup(gitlabGroup *gitlab.Group) (*Grou
ID: gitlabGroup.ID,
Name: gitlabGroup.Path,
gitlabClient: c,
childGroups: nil,
childRepository: nil,
childProjects: nil,
}
// save in cache
@ -95,9 +108,9 @@ func (c *gitlabClient) fetchGroupContent(group *Group) (map[string]fstree.GroupS
// Get cached data if available
// TODO: cache cache invalidation?
if group.childGroups == nil || group.childRepository == nil {
groupCache := make(map[string]fstree.GroupSource)
projectCache := make(map[string]fstree.RepositorySource)
if group.childGroups == nil || group.childProjects == nil {
childGroups := make(map[string]fstree.GroupSource)
childProjects := make(map[string]fstree.RepositorySource)
// List subgroups in path
ListGroupsOpt := &gitlab.ListSubgroupsOptions{
@ -114,7 +127,7 @@ func (c *gitlabClient) fetchGroupContent(group *Group) (map[string]fstree.GroupS
}
for _, gitlabGroup := range gitlabGroups {
group, _ := c.newGroupFromGitlabGroup(gitlabGroup)
groupCache[group.Name] = group
childGroups[group.Name] = group
}
if response.CurrentPage >= response.TotalPages {
break
@ -136,7 +149,7 @@ func (c *gitlabClient) fetchGroupContent(group *Group) (map[string]fstree.GroupS
}
for _, gitlabProject := range gitlabProjects {
project := c.newProjectFromGitlabProject(gitlabProject)
projectCache[project.Name] = &project
childProjects[project.Name] = &project
}
if response.CurrentPage >= response.TotalPages {
break
@ -145,8 +158,8 @@ func (c *gitlabClient) fetchGroupContent(group *Group) (map[string]fstree.GroupS
listProjectOpt.Page = response.NextPage
}
group.childGroups = groupCache
group.childRepository = projectCache
group.childGroups = childGroups
group.childProjects = childProjects
}
return group.childGroups, group.childRepository, nil
return group.childGroups, group.childProjects, nil
}

View File

@ -14,8 +14,8 @@ type User struct {
mux sync.Mutex
// user content cache
projectCache map[string]fstree.RepositorySource
// user content
childProjects map[string]fstree.RepositorySource
}
func (u *User) GetGroupID() uint64 {
@ -26,7 +26,8 @@ func (u *User) InvalidateCache() {
u.mux.Lock()
defer u.mux.Unlock()
u.projectCache = nil
// clear child repositories from cache
u.childProjects = nil
}
func (c *gitlabClient) fetchUser(uid int) (*User, error) {
@ -49,7 +50,7 @@ func (c *gitlabClient) fetchUser(uid int) (*User, error) {
ID: gitlabUser.ID,
Name: gitlabUser.Username,
projectCache: nil,
childProjects: nil,
}
// save in cache
@ -68,7 +69,7 @@ func (c *gitlabClient) fetchCurrentUser() (*User, error) {
ID: gitlabUser.ID,
Name: gitlabUser.Username,
projectCache: nil,
childProjects: nil,
}
c.currentUserCache = &newUser
}
@ -81,8 +82,8 @@ func (c *gitlabClient) fetchUserContent(user *User) (map[string]fstree.GroupSour
// Get cached data if available
// TODO: cache cache invalidation?
if user.projectCache == nil {
projectCache := make(map[string]fstree.RepositorySource)
if user.childProjects == nil {
childProjects := make(map[string]fstree.RepositorySource)
// Fetch the user repositories
listProjectOpt := &gitlab.ListProjectsOptions{
@ -97,7 +98,7 @@ func (c *gitlabClient) fetchUserContent(user *User) (map[string]fstree.GroupSour
}
for _, gitlabProject := range gitlabProjects {
project := c.newProjectFromGitlabProject(gitlabProject)
projectCache[project.Name] = &project
childProjects[project.Name] = &project
}
if response.CurrentPage >= response.TotalPages {
break
@ -106,7 +107,7 @@ func (c *gitlabClient) fetchUserContent(user *User) (map[string]fstree.GroupSour
listProjectOpt.Page = response.NextPage
}
user.projectCache = projectCache
user.childProjects = childProjects
}
return make(map[string]fstree.GroupSource), user.projectCache, nil
return make(map[string]fstree.GroupSource), user.childProjects, nil
}