add cache
This commit is contained in:
@@ -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{},
|
||||
|
||||
@@ -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
1
go.mod
@@ -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
2
go.sum
@@ -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=
|
||||
|
||||
Reference in New Issue
Block a user