add cache

This commit is contained in:
Aine
2022-08-29 15:28:37 +03:00
parent 1a87929567
commit 5a5a649cba
4 changed files with 14 additions and 0 deletions

View File

@@ -6,6 +6,7 @@ import (
"regexp" "regexp"
"sync" "sync"
"git.sr.ht/~xn/cache/v2"
"github.com/getsentry/sentry-go" "github.com/getsentry/sentry-go"
"gitlab.com/etke.cc/go/logger" "gitlab.com/etke.cc/go/logger"
"gitlab.com/etke.cc/linkpearl" "gitlab.com/etke.cc/linkpearl"
@@ -24,6 +25,7 @@ type Bot struct {
allowedAdmins []*regexp.Regexp allowedAdmins []*regexp.Regexp
commands commandList commands commandList
rooms sync.Map rooms sync.Map
cfg cache.Cache[settings]
log *logger.Logger log *logger.Logger
lp *linkpearl.Linkpearl lp *linkpearl.Linkpearl
mu map[id.RoomID]*sync.Mutex mu map[id.RoomID]*sync.Mutex
@@ -47,6 +49,7 @@ func New(
allowedUsers: allowedUsers, allowedUsers: allowedUsers,
allowedAdmins: allowedAdmins, allowedAdmins: allowedAdmins,
rooms: sync.Map{}, rooms: sync.Map{},
cfg: cache.NewLRU[settings](1000),
log: log, log: log,
lp: lp, lp: lp,
mu: map[id.RoomID]*sync.Mutex{}, mu: map[id.RoomID]*sync.Mutex{},

View File

@@ -81,6 +81,11 @@ func (b *Bot) migrateSettings(roomID id.RoomID) {
} }
func (b *Bot) getSettings(roomID id.RoomID) (settings, error) { func (b *Bot) getSettings(roomID id.RoomID) (settings, error) {
cfg := b.cfg.Get(roomID.String())
if cfg != nil {
return cfg, nil
}
config := settings{} config := settings{}
err := b.lp.GetClient().GetRoomAccountData(roomID, settingskey, &config) err := b.lp.GetClient().GetRoomAccountData(roomID, settingskey, &config)
if err != nil { if err != nil {
@@ -90,11 +95,14 @@ func (b *Bot) getSettings(roomID id.RoomID) (settings, error) {
// In such cases, just return a default (empty) settings object. // In such cases, just return a default (empty) settings object.
err = nil err = nil
} }
} else {
b.cfg.Set(roomID.String(), config)
} }
return config, utils.UnwrapError(err) return config, utils.UnwrapError(err)
} }
func (b *Bot) setSettings(roomID id.RoomID, cfg settings) error { func (b *Bot) setSettings(roomID id.RoomID, cfg settings) error {
b.cfg.Set(roomID.String(), cfg)
return utils.UnwrapError(b.lp.GetClient().SetRoomAccountData(roomID, settingskey, cfg)) return utils.UnwrapError(b.lp.GetClient().SetRoomAccountData(roomID, settingskey, cfg))
} }

1
go.mod
View File

@@ -3,6 +3,7 @@ module gitlab.com/etke.cc/postmoogle
go 1.18 go 1.18
require ( require (
git.sr.ht/~xn/cache/v2 v2.0.0
github.com/emersion/go-smtp v0.15.0 github.com/emersion/go-smtp v0.15.0
github.com/getsentry/sentry-go v0.13.0 github.com/getsentry/sentry-go v0.13.0
github.com/jhillyerd/enmime v0.10.0 github.com/jhillyerd/enmime v0.10.0

2
go.sum
View File

@@ -1,3 +1,5 @@
git.sr.ht/~xn/cache/v2 v2.0.0 h1:aYzwGDyVIzjCl2yqcxZjprnu++Q3BmUQeK2agqvcQt8=
git.sr.ht/~xn/cache/v2 v2.0.0/go.mod h1:HIPSMiDudQ483tRDup586e0YZdwMySIZFWXMPwYMuV8=
github.com/DATA-DOG/go-sqlmock v1.5.0 h1:Shsta01QNfFxHCfpW6YH2STWB0MudeXXEWMr20OEh60= github.com/DATA-DOG/go-sqlmock v1.5.0 h1:Shsta01QNfFxHCfpW6YH2STWB0MudeXXEWMr20OEh60=
github.com/cention-sany/utf7 v0.0.0-20170124080048-26cad61bd60a h1:MISbI8sU/PSK/ztvmWKFcI7UGb5/HQT7B+i3a2myKgI= github.com/cention-sany/utf7 v0.0.0-20170124080048-26cad61bd60a h1:MISbI8sU/PSK/ztvmWKFcI7UGb5/HQT7B+i3a2myKgI=
github.com/cention-sany/utf7 v0.0.0-20170124080048-26cad61bd60a/go.mod h1:2GxOXOlEPAMFPfp014mK1SWq8G8BN8o7/dfYqJrVGn8= github.com/cention-sany/utf7 v0.0.0-20170124080048-26cad61bd60a/go.mod h1:2GxOXOlEPAMFPfp014mK1SWq8G8BN8o7/dfYqJrVGn8=