fix cache invalidation

This commit is contained in:
Massaki Archambault 2024-06-06 22:01:22 -04:00
parent 4667c12e47
commit e26f0ae865
2 changed files with 40 additions and 26 deletions

View File

@ -12,11 +12,13 @@ type Group struct {
ID int ID int
Name string Name string
gitlabClient *gitlabClient
mux sync.Mutex mux sync.Mutex
// group content cache // group content
childGroups map[string]fstree.GroupSource childGroups map[string]fstree.GroupSource
childRepository map[string]fstree.RepositorySource childProjects map[string]fstree.RepositorySource
} }
func (g *Group) GetGroupID() uint64 { func (g *Group) GetGroupID() uint64 {
@ -27,8 +29,15 @@ func (g *Group) InvalidateCache() {
g.mux.Lock() g.mux.Lock()
defer g.mux.Unlock() 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.childGroups = nil
g.childRepository = nil
} }
func (c *gitlabClient) fetchGroup(gid int) (*Group, error) { func (c *gitlabClient) fetchGroup(gid int) (*Group, error) {
@ -52,8 +61,10 @@ func (c *gitlabClient) fetchGroup(gid int) (*Group, error) {
ID: gitlabGroup.ID, ID: gitlabGroup.ID,
Name: gitlabGroup.Path, Name: gitlabGroup.Path,
gitlabClient: c,
childGroups: nil, childGroups: nil,
childRepository: nil, childProjects: nil,
} }
// save in cache // save in cache
@ -79,8 +90,10 @@ func (c *gitlabClient) newGroupFromGitlabGroup(gitlabGroup *gitlab.Group) (*Grou
ID: gitlabGroup.ID, ID: gitlabGroup.ID,
Name: gitlabGroup.Path, Name: gitlabGroup.Path,
gitlabClient: c,
childGroups: nil, childGroups: nil,
childRepository: nil, childProjects: nil,
} }
// save in cache // save in cache
@ -95,9 +108,9 @@ func (c *gitlabClient) fetchGroupContent(group *Group) (map[string]fstree.GroupS
// Get cached data if available // Get cached data if available
// TODO: cache cache invalidation? // TODO: cache cache invalidation?
if group.childGroups == nil || group.childRepository == nil { if group.childGroups == nil || group.childProjects == nil {
groupCache := make(map[string]fstree.GroupSource) childGroups := make(map[string]fstree.GroupSource)
projectCache := make(map[string]fstree.RepositorySource) childProjects := make(map[string]fstree.RepositorySource)
// List subgroups in path // List subgroups in path
ListGroupsOpt := &gitlab.ListSubgroupsOptions{ ListGroupsOpt := &gitlab.ListSubgroupsOptions{
@ -114,7 +127,7 @@ func (c *gitlabClient) fetchGroupContent(group *Group) (map[string]fstree.GroupS
} }
for _, gitlabGroup := range gitlabGroups { for _, gitlabGroup := range gitlabGroups {
group, _ := c.newGroupFromGitlabGroup(gitlabGroup) group, _ := c.newGroupFromGitlabGroup(gitlabGroup)
groupCache[group.Name] = group childGroups[group.Name] = group
} }
if response.CurrentPage >= response.TotalPages { if response.CurrentPage >= response.TotalPages {
break break
@ -136,7 +149,7 @@ func (c *gitlabClient) fetchGroupContent(group *Group) (map[string]fstree.GroupS
} }
for _, gitlabProject := range gitlabProjects { for _, gitlabProject := range gitlabProjects {
project := c.newProjectFromGitlabProject(gitlabProject) project := c.newProjectFromGitlabProject(gitlabProject)
projectCache[project.Name] = &project childProjects[project.Name] = &project
} }
if response.CurrentPage >= response.TotalPages { if response.CurrentPage >= response.TotalPages {
break break
@ -145,8 +158,8 @@ func (c *gitlabClient) fetchGroupContent(group *Group) (map[string]fstree.GroupS
listProjectOpt.Page = response.NextPage listProjectOpt.Page = response.NextPage
} }
group.childGroups = groupCache group.childGroups = childGroups
group.childRepository = projectCache 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 mux sync.Mutex
// user content cache // user content
projectCache map[string]fstree.RepositorySource childProjects map[string]fstree.RepositorySource
} }
func (u *User) GetGroupID() uint64 { func (u *User) GetGroupID() uint64 {
@ -26,7 +26,8 @@ func (u *User) InvalidateCache() {
u.mux.Lock() u.mux.Lock()
defer u.mux.Unlock() defer u.mux.Unlock()
u.projectCache = nil // clear child repositories from cache
u.childProjects = nil
} }
func (c *gitlabClient) fetchUser(uid int) (*User, error) { func (c *gitlabClient) fetchUser(uid int) (*User, error) {
@ -49,7 +50,7 @@ func (c *gitlabClient) fetchUser(uid int) (*User, error) {
ID: gitlabUser.ID, ID: gitlabUser.ID,
Name: gitlabUser.Username, Name: gitlabUser.Username,
projectCache: nil, childProjects: nil,
} }
// save in cache // save in cache
@ -68,7 +69,7 @@ func (c *gitlabClient) fetchCurrentUser() (*User, error) {
ID: gitlabUser.ID, ID: gitlabUser.ID,
Name: gitlabUser.Username, Name: gitlabUser.Username,
projectCache: nil, childProjects: nil,
} }
c.currentUserCache = &newUser c.currentUserCache = &newUser
} }
@ -81,8 +82,8 @@ func (c *gitlabClient) fetchUserContent(user *User) (map[string]fstree.GroupSour
// Get cached data if available // Get cached data if available
// TODO: cache cache invalidation? // TODO: cache cache invalidation?
if user.projectCache == nil { if user.childProjects == nil {
projectCache := make(map[string]fstree.RepositorySource) childProjects := make(map[string]fstree.RepositorySource)
// Fetch the user repositories // Fetch the user repositories
listProjectOpt := &gitlab.ListProjectsOptions{ listProjectOpt := &gitlab.ListProjectsOptions{
@ -97,7 +98,7 @@ func (c *gitlabClient) fetchUserContent(user *User) (map[string]fstree.GroupSour
} }
for _, gitlabProject := range gitlabProjects { for _, gitlabProject := range gitlabProjects {
project := c.newProjectFromGitlabProject(gitlabProject) project := c.newProjectFromGitlabProject(gitlabProject)
projectCache[project.Name] = &project childProjects[project.Name] = &project
} }
if response.CurrentPage >= response.TotalPages { if response.CurrentPage >= response.TotalPages {
break break
@ -106,7 +107,7 @@ func (c *gitlabClient) fetchUserContent(user *User) (map[string]fstree.GroupSour
listProjectOpt.Page = response.NextPage 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
} }