Files
gdulai 238361b066
Go Tests / test (push) Successful in 1m3s
Refactor parsing & select order by impl (#6)
Reviewed-on: #6
2026-06-05 15:22:41 +00:00

48 lines
824 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
}