ability to flush user repo cache

This commit is contained in:
Brad Rydzewski 2016-06-14 13:07:05 -07:00
parent f723e8f84c
commit 08a4bf06a5
5 changed files with 41 additions and 1 deletions

5
cache/cache.go vendored
View file

@ -12,6 +12,7 @@ import (
type Cache interface {
Get(string) (interface{}, error)
Set(string, interface{}) error
Delete(string) error
}
func Get(c context.Context, key string) (interface{}, error) {
@ -22,6 +23,10 @@ func Set(c context.Context, key string, value interface{}) error {
return FromContext(c).Set(key, value)
}
func Delete(c context.Context, key string) error {
return FromContext(c).Delete(key)
}
// Default creates an in-memory cache with the default
// 30 minute expiration period.
func Default() Cache {

9
cache/helper.go vendored
View file

@ -52,3 +52,12 @@ func GetRepos(c context.Context, user *model.User) ([]*model.RepoLite, error) {
Set(c, key, repos)
return repos, nil
}
// DeleteRepos evicts the cached user repositories from the cache associated
// with the current context.
func DeleteRepos(c context.Context, user *model.User) error {
key := fmt.Sprintf("repos:%s",
user.Login,
)
return Delete(c, key)
}

15
cache/helper_test.go vendored
View file

@ -84,6 +84,21 @@ func TestHelper(t *testing.T) {
g.Assert(p == nil).IsTrue()
g.Assert(err).Equal(fakeErr)
})
g.It("Should evict repos", func() {
key := fmt.Sprintf("repos:%s",
fakeUser.Login,
)
Set(c, key, fakeRepos)
repos, err := Get(c, key)
g.Assert(repos != nil).IsTrue()
g.Assert(err == nil).IsTrue()
DeleteRepos(c, fakeUser)
repos, err = Get(c, key)
g.Assert(repos == nil).IsTrue()
})
})
}

View file

@ -103,6 +103,7 @@ func Update(c *gin.Context) {
job.Error = work.Job.Error
if build.Status == model.StatusPending {
build.Started = work.Job.Started
build.Status = model.StatusRunning
store.UpdateBuild(c, build)
}

View file

@ -3,7 +3,9 @@ package server
import (
"encoding/base32"
"net/http"
"strconv"
log "github.com/Sirupsen/logrus"
"github.com/gin-gonic/gin"
"github.com/gorilla/securecookie"
@ -33,7 +35,15 @@ func GetFeed(c *gin.Context) {
}
func GetRepos(c *gin.Context) {
repos, err := cache.GetRepos(c, session.User(c))
user := session.User(c)
flush, _ := strconv.ParseBool(c.Query("flush"))
if flush {
log.Debugf("Evicting repository cache for user %s.", user.Login)
cache.DeleteRepos(c, user)
}
repos, err := cache.GetRepos(c, user)
if err != nil {
c.String(500, "Error fetching repository list. %s", err)
return