Add cache locks

This commit is contained in:
kolaente 2020-08-02 15:01:11 +02:00
parent 92ea31a3aa
commit 878c171cef
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
1 changed files with 9 additions and 2 deletions

View File

@ -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)
}