From 4f8a2b0d38b36a5a98bf0fb18b4fece13639203d Mon Sep 17 00:00:00 2001 From: kolaente Date: Fri, 27 Jun 2025 14:07:22 +0200 Subject: [PATCH] chore(user): refactor invalidating upload avatar cache --- pkg/modules/avatar/upload/upload.go | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/pkg/modules/avatar/upload/upload.go b/pkg/modules/avatar/upload/upload.go index 71c0754d3..113dd92db 100644 --- a/pkg/modules/avatar/upload/upload.go +++ b/pkg/modules/avatar/upload/upload.go @@ -36,10 +36,11 @@ import ( type Provider struct { } +const CacheKeyPrefix = "avatar_upload_" + // FlushCache removes cached avatars for a user func (p *Provider) FlushCache(u *user.User) error { - InvalidateCache(u) - return nil + return keyvalue.Del(CacheKeyPrefix + strconv.Itoa(int(u.ID))) } // CachedAvatar represents a cached avatar with its content and mime type @@ -51,7 +52,7 @@ type CachedAvatar struct { // GetAvatar returns an uploaded user avatar func (p *Provider) GetAvatar(u *user.User, size int64) (avatar []byte, mimeType string, err error) { - cacheKey := "avatar_upload_" + strconv.Itoa(int(u.ID)) + cacheKey := CacheKeyPrefix + strconv.Itoa(int(u.ID)) var cached map[int64]*CachedAvatar exists, err := keyvalue.GetWithValue(cacheKey, &cached) @@ -112,13 +113,6 @@ func (p *Provider) GetAvatar(u *user.User, size int64) (avatar []byte, mimeType return avatar, mimeType, err } -// InvalidateCache invalidates the avatar cache for a user -func InvalidateCache(u *user.User) { - if err := keyvalue.Del("avatar_upload_" + strconv.Itoa(int(u.ID))); err != nil { - log.Errorf("Could not invalidate upload avatar cache for user %d, error was %s", u.ID, err) - } -} - func StoreAvatarFile(s *xorm.Session, u *user.User, src io.Reader) (err error) { // Remove the old file if one exists @@ -144,7 +138,10 @@ func StoreAvatarFile(s *xorm.Session, u *user.User, src io.Reader) (err error) { return } - InvalidateCache(u) + err = (&Provider{}).FlushCache(u) + if err != nil { + log.Errorf("Could not invalidate upload avatar cache for user %d, error was %s", u.ID, err) + } // Save the file f, err := files.CreateWithMime(buf, "avatar.png", uint64(buf.Len()), u, "image/png")