Files
simpleorm/cache/cache.go
T
gdulai ffd9cd004e
Go Tests / test (push) Failing after 6s
Introduce Exec interface and implementers (#1)
Reviewed-on: #1
2026-05-11 06:03:40 +00:00

48 lines
822 B
Go

package cache
import (
"sync"
"git.gdulai.com/gdulai/simpleorm/schema"
)
type SchemaCache struct {
mu sync.RWMutex
data map[string]*schema.Table
}
func NewSchemaCache(tables []*schema.Table) *SchemaCache {
cache := &SchemaCache{
data: make(map[string]*schema.Table),
}
for _, table := range tables {
cache.add(table)
}
return cache
}
func (o *SchemaCache) add(table *schema.Table) {
o.mu.Lock()
defer o.mu.Unlock()
o.data[table.Type.Name()] = table
}
func (o *SchemaCache) Get(typeName string) (*schema.Table, bool) {
o.mu.RLock()
defer o.mu.RUnlock()
table, ok := o.data[typeName]
return table, ok
}
func (o *SchemaCache) GetAll() []*schema.Table {
o.mu.RLock()
defer o.mu.RUnlock()
var result []*schema.Table
for _, v := range o.data {
result = append(result, v)
}
return result
}