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"
"sync"
"git.sr.ht/~xn/cache/v2"
"github.com/getsentry/sentry-go"
"gitlab.com/etke.cc/go/logger"
"gitlab.com/etke.cc/linkpearl"
@@ -24,6 +25,7 @@ type Bot struct {
allowedAdmins []*regexp.Regexp
commands commandList
rooms sync.Map
cfg cache.Cache[settings]
log *logger.Logger
lp *linkpearl.Linkpearl
mu map[id.RoomID]*sync.Mutex
@@ -47,6 +49,7 @@ func New(
allowedUsers: allowedUsers,
allowedAdmins: allowedAdmins,
rooms: sync.Map{},
cfg: cache.NewLRU[settings](1000),
log: log,
lp: lp,
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) {
cfg := b.cfg.Get(roomID.String())
if cfg != nil {
return cfg, nil
}
config := settings{}
err := b.lp.GetClient().GetRoomAccountData(roomID, settingskey, &config)
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.
err = nil
}
} else {
b.cfg.Set(roomID.String(), config)
}
return config, utils.UnwrapError(err)
}
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))
}

1
go.mod
View File

@@ -3,6 +3,7 @@ module gitlab.com/etke.cc/postmoogle
go 1.18
require (
git.sr.ht/~xn/cache/v2 v2.0.0
github.com/emersion/go-smtp v0.15.0
github.com/getsentry/sentry-go v0.13.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/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=