use gorm cache

This commit is contained in:
Pascal Fischer
2025-10-13 16:29:04 +02:00
parent 0d2e67983a
commit f0e8cd578d
15 changed files with 498 additions and 17 deletions

48
management/server/store/cache/memory.go vendored Normal file
View File

@@ -0,0 +1,48 @@
package cache
import (
"context"
"sync"
"github.com/go-gorm/caches/v4"
)
type MemoryCacher struct {
store *sync.Map
}
func (c *MemoryCacher) init() {
if c.store == nil {
c.store = &sync.Map{}
}
}
func (c *MemoryCacher) Get(ctx context.Context, key string, q *caches.Query[any]) (*caches.Query[any], error) {
c.init()
val, ok := c.store.Load(key)
if !ok {
return nil, nil
}
if err := q.Unmarshal(val.([]byte)); err != nil {
return nil, err
}
return q, nil
}
func (c *MemoryCacher) Store(ctx context.Context, key string, val *caches.Query[any]) error {
c.init()
res, err := val.Marshal()
if err != nil {
return err
}
c.store.Store(key, res)
return nil
}
func (c *MemoryCacher) Invalidate(ctx context.Context) error {
c.store = &sync.Map{}
return nil
}