From 6861e55e89f05bfff2122fe56d766077d026d92a Mon Sep 17 00:00:00 2001 From: kolaente Date: Sun, 1 Mar 2020 20:47:01 +0100 Subject: [PATCH] Add config option for caching avatars --- config.yml.sample | 2 ++ docs/content/doc/setup/config.md | 2 ++ pkg/config/config.go | 2 ++ pkg/modules/avatar/gravatar/gravatar.go | 3 ++- 4 files changed, 8 insertions(+), 1 deletion(-) diff --git a/config.yml.sample b/config.yml.sample index 1b44369d0..8affea75b 100644 --- a/config.yml.sample +++ b/config.yml.sample @@ -30,6 +30,8 @@ service: timezone: GMT # Whether task comments should be enabled or not enabletaskcomments: true + # The duration in seconds until a cached gravatar user avatar expires + gravatarexpiration: 3600 database: # Database type to use. Supported types are mysql, postgres and sqlite. diff --git a/docs/content/doc/setup/config.md b/docs/content/doc/setup/config.md index bfff15fa3..923c005d3 100644 --- a/docs/content/doc/setup/config.md +++ b/docs/content/doc/setup/config.md @@ -73,6 +73,8 @@ service: timezone: GMT # Whether task comments should be enabled or not enabletaskcomments: true + # The duration in seconds until a cached gravatar user avatar expires + gravatarexpiration: 3600 database: # Database type to use. Supported types are mysql, postgres and sqlite. diff --git a/pkg/config/config.go b/pkg/config/config.go index 43811de3a..98f4d41fa 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -46,6 +46,7 @@ const ( ServiceEnableTaskAttachments Key = `service.enabletaskattachments` ServiceTimeZone Key = `service.timezone` ServiceEnableTaskComments Key = `service.enabletaskcomments` + ServiceGravatarExpiration Key = `service.gravatarexpiration` DatabaseType Key = `database.type` DatabaseHost Key = `database.host` @@ -173,6 +174,7 @@ func InitDefaultConfig() { ServiceEnableTaskAttachments.setDefault(true) ServiceTimeZone.setDefault("GMT") ServiceEnableTaskComments.setDefault(true) + ServiceGravatarExpiration.setDefault(3600) // Database DatabaseType.setDefault("sqlite") diff --git a/pkg/modules/avatar/gravatar/gravatar.go b/pkg/modules/avatar/gravatar/gravatar.go index 6aba62662..b95edcd52 100644 --- a/pkg/modules/avatar/gravatar/gravatar.go +++ b/pkg/modules/avatar/gravatar/gravatar.go @@ -17,6 +17,7 @@ package gravatar import ( + "code.vikunja.io/api/pkg/config" "code.vikunja.io/api/pkg/log" "code.vikunja.io/api/pkg/user" "io/ioutil" @@ -51,7 +52,7 @@ func (g *Provider) GetAvatar(user *user.User, size int64) ([]byte, string, error // elaped is alway < 0 so the next check would always succeed. // To have it make sense, we flip that. elapsed := a.loadedAt.Sub(time.Now()) * -1 - needsRefetch = elapsed > 10*time.Second + needsRefetch = elapsed > time.Duration(config.ServiceGravatarExpiration.GetInt64())*time.Second if needsRefetch { log.Debugf("Refetching avatar for user %d after %v", user.ID, elapsed) } else {