diff --git a/pkg/modules/avatar/initials/initials.go b/pkg/modules/avatar/initials/initials.go index 55676cb6b..8ef07cf98 100644 --- a/pkg/modules/avatar/initials/initials.go +++ b/pkg/modules/avatar/initials/initials.go @@ -22,6 +22,7 @@ import ( "github.com/disintegration/imaging" "strconv" "strings" + "sync" "bytes" "github.com/golang/freetype/truetype" @@ -51,8 +52,10 @@ var ( } // Contain the created avatars with a size of defaultSize - cache = map[int64]*image.RGBA64{} - cacheResized = map[string][]byte{} + cache = map[int64]*image.RGBA64{} + cacheLock = sync.Mutex{} + cacheResized = map[string][]byte{} + cacheResizedLock = sync.Mutex{} ) func init() { @@ -132,7 +135,9 @@ func getAvatarForUser(u *user.User) (fullSizeAvatar *image.RGBA64, err error) { if err != nil { return nil, err } + cacheLock.Lock() cache[u.ID] = fullSizeAvatar + cacheLock.Unlock() } return fullSizeAvatar, err @@ -157,7 +162,9 @@ func (p *Provider) GetAvatar(u *user.User, size int64) (avatar []byte, mimeType return nil, "", err } avatar = buf.Bytes() + cacheResizedLock.Lock() cacheResized[cacheKey] = avatar + cacheResizedLock.Unlock() } else { log.Debugf("Serving initials avatar for user %d and size %d from cache", u.ID, size) }