avatar: avoid concurrent map writes
This commit is contained in:
@@ -25,6 +25,8 @@ import (
|
||||
"github.com/grafana/grafana/pkg/log"
|
||||
"github.com/grafana/grafana/pkg/setting"
|
||||
"gopkg.in/macaron.v1"
|
||||
|
||||
gocache "github.com/patrickmn/go-cache"
|
||||
)
|
||||
|
||||
var gravatarSource string
|
||||
@@ -92,7 +94,7 @@ func (this *Avatar) Update() (err error) {
|
||||
|
||||
type CacheServer struct {
|
||||
notFound *Avatar
|
||||
cache map[string]*Avatar
|
||||
cache *gocache.Cache
|
||||
}
|
||||
|
||||
func (this *CacheServer) mustInt(r *http.Request, defaultValue int, keys ...string) (v int) {
|
||||
@@ -110,7 +112,9 @@ func (this *CacheServer) Handler(ctx *macaron.Context) {
|
||||
|
||||
var avatar *Avatar
|
||||
|
||||
if avatar, _ = this.cache[hash]; avatar == nil {
|
||||
if obj, exist := this.cache.Get(hash); exist {
|
||||
avatar = obj.(*Avatar)
|
||||
} else {
|
||||
avatar = New(hash)
|
||||
}
|
||||
|
||||
@@ -124,7 +128,7 @@ func (this *CacheServer) Handler(ctx *macaron.Context) {
|
||||
if avatar.notFound {
|
||||
avatar = this.notFound
|
||||
} else {
|
||||
this.cache[hash] = avatar
|
||||
this.cache.Add(hash, avatar, gocache.DefaultExpiration)
|
||||
}
|
||||
|
||||
ctx.Resp.Header().Add("Content-Type", "image/jpeg")
|
||||
@@ -146,7 +150,7 @@ func NewCacheServer() *CacheServer {
|
||||
|
||||
return &CacheServer{
|
||||
notFound: newNotFound(),
|
||||
cache: make(map[string]*Avatar),
|
||||
cache: gocache.New(time.Hour, time.Hour*2),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user