mirror of
https://github.com/woodpecker-ci/woodpecker.git
synced 2025-10-22 09:33:49 +00:00
improve and simplify repository caching
This commit is contained in:
79
cache/helper.go
vendored
79
cache/helper.go
vendored
@@ -4,72 +4,51 @@ import (
|
||||
"fmt"
|
||||
|
||||
"github.com/drone/drone/model"
|
||||
"github.com/drone/drone/remote"
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
// GetRepos returns the user permissions to the named repository
|
||||
// from the cache associated with the current context.
|
||||
func GetPerms(c context.Context, user *model.User, owner, name string) *model.Perm {
|
||||
// GetPerm returns the user permissions repositories from the cache
|
||||
// associated with the current repository.
|
||||
func GetPerms(c context.Context, user *model.User, owner, name string) (*model.Perm, error) {
|
||||
key := fmt.Sprintf("perms:%s:%s/%s",
|
||||
user.Login,
|
||||
owner,
|
||||
name,
|
||||
)
|
||||
val, err := FromContext(c).Get(key)
|
||||
if err != nil {
|
||||
return nil
|
||||
// if we fetch from the cache we can return immediately
|
||||
val, err := Get(c, key)
|
||||
if err == nil {
|
||||
return val.(*model.Perm), nil
|
||||
}
|
||||
return val.(*model.Perm)
|
||||
}
|
||||
|
||||
// SetRepos adds the listof user permissions to the named repsotiory
|
||||
// to the cache assocaited with the current context.
|
||||
func SetPerms(c context.Context, user *model.User, perm *model.Perm, owner, name string) {
|
||||
key := fmt.Sprintf("perms:%s:%s/%s",
|
||||
user.Login,
|
||||
owner,
|
||||
name,
|
||||
)
|
||||
FromContext(c).Set(key, perm)
|
||||
// else we try to grab from the remote system and
|
||||
// populate our cache.
|
||||
perm, err := remote.Perm(c, user, owner, name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
Set(c, key, perm)
|
||||
return perm, nil
|
||||
}
|
||||
|
||||
// GetRepos returns the list of user repositories from the cache
|
||||
// associated with the current context.
|
||||
func GetRepos(c context.Context, user *model.User) []*model.RepoLite {
|
||||
func GetRepos(c context.Context, user *model.User) ([]*model.RepoLite, error) {
|
||||
key := fmt.Sprintf("repos:%s",
|
||||
user.Login,
|
||||
)
|
||||
val, err := FromContext(c).Get(key)
|
||||
if err != nil {
|
||||
return nil
|
||||
// if we fetch from the cache we can return immediately
|
||||
val, err := Get(c, key)
|
||||
if err == nil {
|
||||
return val.([]*model.RepoLite), nil
|
||||
}
|
||||
// else we try to grab from the remote system and
|
||||
// populate our cache.
|
||||
repos, err := remote.Repos(c, user)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return val.([]*model.RepoLite)
|
||||
}
|
||||
|
||||
// SetRepos adds the listof user repositories to the cache assocaited
|
||||
// with the current context.
|
||||
func SetRepos(c context.Context, user *model.User, repos []*model.RepoLite) {
|
||||
key := fmt.Sprintf("repos:%s",
|
||||
user.Login,
|
||||
)
|
||||
FromContext(c).Set(key, repos)
|
||||
Set(c, key, repos)
|
||||
return repos, nil
|
||||
}
|
||||
|
||||
// GetSetRepos is a helper function that attempts to get the
|
||||
// repository list from the cache first. If no data is in the
|
||||
// cache or it is expired, it will remotely fetch the list of
|
||||
// repositories and populate the cache.
|
||||
// func GetSetRepos(c context.Context, user *model.User) ([]*model.RepoLite, error) {
|
||||
// cache := FromContext(c).Repos()
|
||||
// repos := FromContext(c).Repos().Get(user)
|
||||
// if repos != nil {
|
||||
// return repos, nil
|
||||
// }
|
||||
// var err error
|
||||
// repos, err = remote.FromContext(c).Repos(user)
|
||||
// if err != nil {
|
||||
// return nil, err
|
||||
// }
|
||||
// cache.Set(user, repos)
|
||||
// return repos, nil
|
||||
// }
|
||||
|
Reference in New Issue
Block a user