Merge pull request #1677 from bradrydzewski/master

ability to flush user repo cache
This commit is contained in:
Brad Rydzewski 2016-06-14 13:18:13 -07:00 committed by GitHub
commit 9753d6f64c
5 changed files with 41 additions and 1 deletions

5
cache/cache.go vendored
View file

@ -12,6 +12,7 @@ import (
type Cache interface { type Cache interface {
Get(string) (interface{}, error) Get(string) (interface{}, error)
Set(string, interface{}) error Set(string, interface{}) error
Delete(string) error
} }
func Get(c context.Context, key string) (interface{}, 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) 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 // Default creates an in-memory cache with the default
// 30 minute expiration period. // 30 minute expiration period.
func Default() Cache { 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) Set(c, key, repos)
return repos, nil 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(p == nil).IsTrue()
g.Assert(err).Equal(fakeErr) 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 job.Error = work.Job.Error
if build.Status == model.StatusPending { if build.Status == model.StatusPending {
build.Started = work.Job.Started
build.Status = model.StatusRunning build.Status = model.StatusRunning
store.UpdateBuild(c, build) store.UpdateBuild(c, build)
} }

View file

@ -3,7 +3,9 @@ package server
import ( import (
"encoding/base32" "encoding/base32"
"net/http" "net/http"
"strconv"
log "github.com/Sirupsen/logrus"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/gorilla/securecookie" "github.com/gorilla/securecookie"
@ -33,7 +35,15 @@ func GetFeed(c *gin.Context) {
} }
func GetRepos(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 { if err != nil {
c.String(500, "Error fetching repository list. %s", err) c.String(500, "Error fetching repository list. %s", err)
return return